android常用组件

1、单选和多选按钮

在使用单选按钮时,注意需要加入的组件有两个:RadioGroup和RadioButton

通过 RadioGroup来分组RadioButton。

      <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton android:text="男" />

        <RadioButton android:text="女" />
        
    </RadioGroup>

如果想加入默认选中,需要使用checked属性。

    <RadioGroup
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <RadioButton android:text="男" android:checked="true" />

        <RadioButton android:text="女" />
        
    </RadioGroup>
但要注意,多选框可以这样使用属性来完成默认选中,但单选一定不要这样写。

单选框的默认选中需要通过Java程序来实现。

<RadioGroup
       <span style="color:#ff0000;"> android:id="@+id/radio"</span>
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >
public class MainActivity extends Activity {

	private RadioGroup radio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置默认使用的布局文件
		setContentView(R.layout.activity_main);

		radio = (RadioGroup) findViewById(R.id.radio);

		// 设置默认选中的方法, <span style="color:#ff0000;">一般不推荐直接使用id来设置选中,而是根据索引下标来选中</span>
		radio.check(radio.getChildAt(0).getId());

	}
}

如果选项并不是写死的,而是动态通过数据获得的,也可以在Java代码来完成选项的创建。

public class MainActivity extends Activity {

	private RadioGroup radio;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置默认使用的布局文件
		setContentView(R.layout.activity_main);

		radio = (RadioGroup) findViewById(R.id.radio);

		// 设置默认选中的方法, 一般不推荐直接使用id来设置选中,而是根据索引下标来选中
		// radio.check(radio.getChildAt(0).getId());
		// 建立一个保存着所有选项数据的数组或集合, 表示该数据是从其他地方取得的.
		String[] allValues = { "男", "女", "泰国" };

		for (int i = 0; i < allValues.length; i++) {
			RadioButton button = new RadioButton(this);
			button.setText(allValues[i]);
			radio.addView(button);
		}
	}
}
对于多选框来说,Android没有提供CheckBoxGroup,这里使用LinearLayout来替代RadioGroup的功能。
<TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="喜欢的颜色: " />

    <LinearLayout
        android:id="@+id/checkbox"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" >

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="红" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="绿" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="蓝" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="黑" />

        <CheckBox
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="紫" />
    </LinearLayout>
注意,CheckBox需要自己设置widthheight
2、Spinner下拉列表

下拉列表中的数据不能直接写死到布局的xml中,而是通过下面两种方式来提供:

1)  使用strings.xml来加入固定的选项

2)  通过Java程序,编写Adapter来完成添加。

<Spinner
        android:id="@+id/spinner"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:<span style="color:#ff0000;">entries</span>="@array/spinner_area" />
如果数据是通过程序查询出来的,就需要在MainActivity类里声明Spinner和ArrayAdapter类,来加入数据。

private String[] allAreaValues = { "南通", "北京", "广州", "深圳", "大连" };

	private Spinner spinner;

	private ArrayAdapter<String> adapter;

		// 处理下拉列表数据
		// 取得下拉列表
		spinner = (Spinner) findViewById(R.id.spinner);

		// 建立Adapter对象, 这里先使用系统提供好的TextView格式来显示下拉列表
		adapter = new ArrayAdapter<String>(this,
				android.R.layout.simple_spinner_item, allAreaValues);

		// 将adapter设置到下拉列表中
		spinner.setAdapter(adapter);
如果想改变选择选项时的格式,可以通过下面的方法实现。

adapter.setDropDownViewResource(android.R.layout.
如果想自己调整下拉列表中的格式,可以自行声明一个xml文件,来控制布局。

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:gravity="left"
    android:textColor="#ff0000"
    android:textSize="20sp" >

</TextView>
需要将这个布局导入到Adapter里。

adapter = new ArrayAdapter<String>(this,
				R.layout.my_spinner_item, allAreaValues);
3、日期时间选择器,滚动面版

如果组件已经显示到了屏幕外,这时需要用户通过滑动操作来显示出这些组件,默认该功能是不支持的,如果想加入滑动,需要为组件设置一个滚动面版(ScrollView)

这个组件内部只能有一个子节点。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scrollbars="none" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical" ><span style="color:#ff0000;">
</span>

日期时间选择

         <DatePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

        <TimePicker
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

































评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值