基本控件(三)

1.单选按钮(RadioGroup)方法

   check(int id);//设置选中的单选按钮编号

   clearCheck();//清空选中状态

   int getCheckedRadioButtonId();//取得选中按钮的RadioButton的ID

   setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener);//设置单选按钮选中的操作事件

  <RadioGroup   //定义单选按钮

      .....................

      android:orientation="horizontal" //组件选项水平排列

      android:checkedButton="@+id/male" //默认选中组件ID

      <RadioButton   //定义选项按钮

         ................

      />

   ></RadioGroup>

2.CheckBox

    CheckBox(Context context);//实例化CheckBox组件

    setChecked(boolean checked);//设置默认选中

    <CheckBox   //定义复选框

     ...............

     android:checked="true"(/"false")  //设置默认选中

     />

3.ImageView

   setMaxHeight(int maxHeight);//定义图片的最大高度

   setMaxWidth(int maxWidth);//定义图片的最大宽度

   setImageResource(int resId);//定义显示图片的ID

  <ImageView    //定义图片显示组件

      android:maxHeight="12px"

      android:maxWidth="12px"

      android:src="@drawable/图片名称"

      ....................

   />

4.ImageButton

 <ImageButton   //定义图片按钮组件

      android:maxHeight="12px"

      android:maxWidth="12px"

      android:src="@drawable/图片名称"

      ....................

   />

5.TimePicker

   Integer getCurrentHour();//返回当前设置的小时

   Integer GetCurrentMinute();//返回当前设置的分钟

   boolean is24HourView();//判断是否是24小时制

   setCurrentHour(Integer currentHour);//设置当前的小时数

   setCurrentMinute(Integer currentMinute);//设置当前的分钟

   setEnabled(boolean enabled);//设置是否可用

   setIs24HourView(Boolean is24HourView);//设置时间为24小时制

   <TimePicker    //定义时间组件

      ................

    />

6.DatePicker

  getYear();//取得设置的年

  getMonth();//取得设置的月

  getDayOfMonth();//取得设置的日

  setEnabled(boolean enabled);//设置组件是否可用

  updateDate(int year,int monthOfYear,int dayOfMonth);//设置一个指定的日期

 <DatePicker  //定义日期选择器

     ................

  />

7.Spinner

   CharSequence getPrompt;//取得提示文字

   setPrompt(CharSequence  prompt);//设置组件的提示文字

   setAdapter(Spinner Adapter adapter);//设置下拉列表项

   CharSequence  getPrompt;//得到提示信息

   setOnItemClickListener(AdapterViewOnItemClickListener);//设置选项单机事件

   <Spinner  //定义下拉列表组件

    android:prompt="选择城市"  //定义提示信息

    android:entried="@array/citys"   //定义使用的字符串的资源

   />

配置列表项

 方式一:直接通过资源文件配置

     <resources>

          <string-array name="citys">

                 <item>.....</item>

          </string-array>

      </resources>

  方式二:通过android.widget.ArrayAdapter类读取资源文件或者是指定具体设置的数据

   ArrayAdapter类的功能:有两个主要功能:读取资源文件中定义的列表项或者是通过List集合设置列表项,

   ArrayAdapter(Context context,int textViewResourceId,List<T>objects);//定义ArrayAdapter对象,同时向里面传入一个Activity实例(this)、列表项的显示风格、List集合数据

    ArrayAdapter(Context context,int textViewResourceId,T[]objects);//定义ArrayAdapter对象,同时向里面传入一个Activity实例(this)、列表项的显示风格、数组数据

    static ArrayAdapter<CharSequence>createFromResource(Context context,int textArrayResId,int textViewResId);//通过静态方法取得ArrayAdapter对象,传入Activity实例、资源文件的id、列表项的显示风格

    setDropDownViewResource(int resource);//设置下拉表项的显示风格

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"   //所有组件垂直摆放
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>

<TextView
android:id="@+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

<Spinner
android:id="@+id/sp2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/p"

></Spinner>

<Spinner
android:id="@+id/sp1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/p"

></Spinner>

</LinearLayout>

原文地址:https://www.cnblogs.com/ch123456/p/6848882.html