博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
常用控件
阅读量:6610 次
发布时间:2019-06-24

本文共 7397 字,大约阅读时间需要 24 分钟。

1. 日期选择器 比较丑,可能用的很少

public class MainActivity extends ActionBarActivity {    private Button btn = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn = (Button) this.findViewById(R.id.btnChoose);        btn.setOnClickListener(new View.OnClickListener() {  // 定义了一个按钮,然后点显示日期组件            @Override            public void onClick(View v) {                new DatePickerDialog(MainActivity.this, new DatePickerDialog.OnDateSetListener() {                    @Override                    public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {  // 当日期选择的时候                        String date = String.format("%d-%d-%d", year, month, dayOfMonth);                        btn.setText(date);       // 改变按钮的text                        System.out.print(date);  // 打印                    }                }, 2016, 1, 1).show();            }        });        System.out.println("onCreate");    }}

 2. 时间控件

public class MainActivity extends ActionBarActivity {    private Button btn = null;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn = (Button) this.findViewById(R.id.btnChoose);        btn.setOnClickListener(new View.OnClickListener() {  // 定义了一个按钮,然后点显示时间控件            @Override            public void onClick(View v) {                // 创建一个时间控件               new TimePickerDialog(MainActivity.this, new TimePickerDialog.OnTimeSetListener() {                   @Override                   public void onTimeSet(TimePicker view, int hourOfDay, int minute) {  // 选择时间市,相应                       String s = String.format("%d:%d", hourOfDay, minute);                       btn.setText(s);                   }               }, 0, 0, true).show();            }        });        System.out.println("onCreate");    }    }

 3. 单选按钮

 视图xml文件

Activity

public class MainActivity extends ActionBarActivity {    private Button btn = null;    private RadioButton rbA = null;   // 单选按钮A    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        btn = (Button) this.findViewById(R.id.btnChoose);        rbA = (RadioButton) this.findViewById(R.id.rbA);        btn.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                if(rbA.isChecked()) {   // 当选择是正确的                    // 提示                    Toast.makeText(MainActivity.this, "所选是正确的", Toast.LENGTH_SHORT).show();                } else {                    Toast.makeText(MainActivity.this, "所选是错误的", Toast.LENGTH_SHORT).show();                }            }        });        System.out.println("onCreate");    }}

 4. 多选按钮

视图xml文件

activity 文件

public class MainActivity extends ActionBarActivity        implements CompoundButton.OnCheckedChangeListener {    // 实现 CompoundButton.OnCheckedChangeListener    private CheckBox cb1,cb2,cb3,cb4;  // 定义4个多选按钮    private TextView tv;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        cb1 = (CheckBox) this.findViewById(R.id.cb1);        cb2 = (CheckBox) this.findViewById(R.id.cb2);        cb3 = (CheckBox) this.findViewById(R.id.cb3);        cb4 = (CheckBox) this.findViewById(R.id.cb4);        tv = (TextView) this.findViewById(R.id.tvLove);        cb1.setOnCheckedChangeListener(this);        cb2.setOnCheckedChangeListener(this);        cb3.setOnCheckedChangeListener(this);        cb4.setOnCheckedChangeListener(this);        System.out.println("onCreate");    }    @Override    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {  // 当多选按钮变化的时候        String str = "你喜欢";        if (cb1.isChecked()) {            str += cb1.getText() + ", ";        }        if (cb2.isChecked()) {            str += cb2.getText() + ", ";        }        if (cb3.isChecked()) {            str += cb3.getText() + ", ";        }        if (cb4.isChecked()) {            str += cb4.getText() + ", ";        }        tv.setText(str);    //改变文本    }}

 

5. 下拉列表

① 在代码里面生成 下拉的数据

public class MainActivity extends ActionBarActivity{    private static final String[] m = {"A型", "B型", "O型", "AB型", "其他"};    private TextView tv;    private Spinner spinner;    private ArrayAdapter
adapter; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tv = (TextView) this.findViewById(R.id.spinnerText); spinner = (Spinner) this.findViewById(R.id.spinner); // 将可选内容和 ArrayAdapter 连接 adapter = new ArrayAdapter
(this, android.R.layout.simple_spinner_item, m); // 设置下拉风格 adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // 将 adapter添加到apinner中 spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView
parent, View view, int position, long id) { tv.setText("你的血型是 " + m[position]); } @Override public void onNothingSelected(AdapterView
parent) {} }); //设置默认值 spinner.setVisibility(View.VISIBLE); System.out.println("onCreate"); }}

 

② 下拉框的数据用xml文件

A型
B型
O型
AB型
其他
public class MainActivity extends ActionBarActivity{    private TextView tv;    private Spinner spinner;    private ArrayAdapter adapter;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv = (TextView) this.findViewById(R.id.spinnerText);        spinner = (Spinner) this.findViewById(R.id.spinner);        // 这里创建的adapter的数据是 apinnerArray.xml 里面的数据         adapter = ArrayAdapter.createFromResource(this, R.array.plantes, android.R.layout.simple_spinner_item);        // 设置下拉风格        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);        // 将 adapter添加到apinner中        spinner.setAdapter(adapter);        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {            @Override            public void onItemSelected(AdapterView
parent, View view, int position, long id) { tv.setText("你的血型是 " + adapter.getItem(position)); // 这里获取值的话 就从adpter中获取 } @Override public void onNothingSelected(AdapterView
parent) {} }); //设置默认值 spinner.setVisibility(View.VISIBLE); System.out.println("onCreate"); }}

 

转载于:https://www.cnblogs.com/shaoshao/p/5886722.html

你可能感兴趣的文章
XMLHttpRequest.status状态码
查看>>
提示错误 Call to undefined function imagepng() in …
查看>>
如何创建指定大小的数组/字符串
查看>>
参考MSDN学习《迭代器》
查看>>
查询端口占用
查看>>
字符编码06
查看>>
django之表单及模板渲染、全局钩子、局部钩子函数-67
查看>>
SQL总结(二)连表查询
查看>>
【转载】FPGA功耗的那些事儿
查看>>
delphi 获取可执行文件的当前路径
查看>>
求圆的面积
查看>>
自定义控件
查看>>
HDOJ-4006/(大连网赛1006)- The kth great number 剖析
查看>>
[Codeforces Round #452 (Div. 2)] Splitting in Teams
查看>>
leetcode 51: Generate Parentheses
查看>>
IE6 for WIN8
查看>>
Java gc机制
查看>>
python xml.etree ElementTree解析 编辑 xml
查看>>
复利计算5.0-----结对编程
查看>>
hdu 1255 覆盖的面积(求覆盖至少两次以上的面积)
查看>>