Android-文本类组件(二)

来源:互联网 发布:超级玛丽源码 编辑:程序博客网 时间:2024/06/05 09:45

1.ToggleButton
ToggleButton组件是Android中提供的一种特殊的按钮控件,在Android中,可以使用两种方法向屏幕中添加ToggleButton组件,使用XML文件或者在Java文件中new出来
ToggleButton支持的XML属性:
Android:textOn 设置控件在选中时显示的文本
Android:textOff 设置控件在未选中时显示的文本

2.AutoCompleteTextView
AutoCompleteTextView组件是Android中提供的一个自动提示组件。
在MainActivity.java文件中,首先定义一个静态字符串数组,用来存储数据源,在onCreate方法中,通过setAdapter方法为AutoCompleteTextView组件设置ArrayAdapter数据源

protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        //创建适配器,设置显示方式为下拉显示        ArrayAdapter<String> adapter=new ArrayAdapter<String>(this,                android.R.layout.simple_dropdown_item_1line,autoInfo);        //找到布局文件中的AutoCompleteTextView控件        AutoCompleteTextView actxTextView=(AutoCompleteTextView) findViewById(R.id.actxt);        //设置数据源        actxTextView.setAdapter(adapter);

3.RadioButton单选按钮
在默认的情况下,单选按钮显示一个圆形图标,并且在该图标旁边放置一些说明性文字,在程序中,一般将多个单选按钮放置在按钮组中,使这些单选按钮表现出某种功能,当选中某个单选按钮后,按钮组中的其他单选按钮将被自动取消选取状态。单选按钮用RadioButton表示,RadioButton类又是Button的子类,所以同样支持Button的各种属性。
1.在res/layout/activity_main.xml文件中添加一个TextView,一个包含两个单选按钮的单选按钮组和一个“提交”按钮

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="horizontal"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView         android:layout_width="wrap_content"        android:layout_height="wrap_content"         android:text="性别:"        android:height="50px" />    <RadioGroup         android:id="@+id/radioGroup1"        android:orientation="horizontal"         android:layout_width="wrap_content"         android:layout_height="wrap_content">        <RadioButton             android:layout_height="wrap_content"             android:id="@+id/radio0"             android:text="男"             android:layout_width="wrap_content"             android:checked="true"/>        <RadioButton             android:layout_height="wrap_content"             android:id="@+id/radio1"             android:text="女"             android:layout_width="wrap_content"/>    </RadioGroup>    <Button         android:text="提交"         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"    /></LinearLayout>
2. 在MainActivity的onCreate方法中,获取布局文件中的单选按钮组,为提交按钮添加监听事件,监听事件获取选中的单选按钮的文本
public void onCreate(Bundle savedInstanceState) {            super.onCreate(savedInstanceState);            setContentView(R.layout.main);            final RadioGroup sex = (RadioGroup) findViewById(R.id.radioGroup1);//获取单选按钮组          //为单选按钮组添加事件监听          sex.setOnCheckedChangeListener(new OnCheckedChangeListener() {            @Override            public void onCheckedChanged(RadioGroup group, int checkedId) {                RadioButton r = (RadioButton) findViewById(checkedId);//获取被选择的单选按钮                Log.i("单选按钮", "您的选择是:" + r.getText());            }          });        Button button = (Button) findViewById(R.id.button1);//获取提交按钮        //为提交按钮添加单击事件监听        button.setOnClickListener(new OnClickListener() {            @Override            public void onClick(View v) {                //通过for循环遍历单选按钮组                for (int i = 0; i < sex.getChildCount(); i++) {                    RadioButton r = (RadioButton) sex.getChildAt(i);                    if (r.isChecked()) {//判断单选按钮是否被选中                        Log.i("单选按钮", "性别:" + r.getText());                        break;//跳出for循环                    }                }            }        });        }
4.  复选按钮组CheckBox在默认的情况下,复选按钮显示一个方块图标,并且在图标旁边放置一些说明性文字。与单选按钮唯一不同的是复选按钮可以进行多选设置,每一个复选按钮都提供“选中”和“不选中”两种状态。CheckBox是Button的子类由于复选框可以选中多项,所以为了确定用户是否选择了某一项,还需要为每一个选项添加SetOnCheckedChangeListener事件监听。
    final OnCheckedChangeListener checkBox_listener=new OnCheckedChangeListener(){                @Override                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {                    // TODO Auto-generated method stub                    if(isChecked){                        Toast.makeText(MainActivity.this, buttonView.getText(), Toast.LENGTH_SHORT).show();                    }                }            };
0 0
原创粉丝点击