4.单选按钮RadioGroup
RadioGroup提供的只是一个单选按钮的容器,只有在此容器中配置多个按钮组件之后才可以使用,设置单选按钮则需要使用RadioButton类
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFF00"
android:textSize="20pt"
android:text="你的求职意向"
/>
<RadioGroup
android:id="@+id/career"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:checkedButton="@+id/android">
<RadioButton
android:id="@+id/android"
android:text="Android"
/>
<RadioButton
android:id="@+id/Ios"
android:text="Iphone"
/>
</RadioGroup>
</LinearLayout>
5.复选框CheckBox
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFF00"
android:textSize="20pt"
android:text="你的特长:"
/>
<CheckBox
android:id="@+id/c1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Unity3d"/>
<CheckBox
android:id="@+id/c2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="Android"/>
<CheckBox
android:id="@+id/c3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text ="IOS"/>
</LinearLayout>
public class MainActivity extends Activity { private CheckBox cb = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); this.cb = (CheckBox)super.findViewById(R.id.c1); cb.setChecked(true); cb.setText("OpenGL"); } }
6.下拉列表框Spinner
在Android中,可以直接在main.xml文件中定义<Spinner>节点,但是在定义此元素时却不能直接设置其显示的列表项。
1.直接通过资源配置文件配置
定义一个values\pl.xml 定义数据内容需要使用<string-array>元素指定
<resources>
<string-array name="pl_lables">
<item>C++</item>
<item>C#</item>
<item>Java</item>
</string-array>
</resources>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:id="@+id/text1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FFFF00" android:textSize="20pt" android:text="请选择你最喜欢的语言" /> <Spinner android:id="@+id/mypl" android:prompt="@string/pl_prompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:entries="@array/pl_lables" /> </LinearLayout>
如果配置文件没有读取下拉表数组内容配置文件,可以通过程序来控制
public class MainActivity extends Activity { private Spinner sppl = null; private ArrayAdapter<CharSequence> adapterPl= null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); this.sppl = (Spinner)super.findViewById(R.id.mypl); this.adapterPl = ArrayAdapter.createFromResource(this,R.array.pl_lables,android.R.layout.simple_spinner_item); this.adapterPl.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); this.sppl.setAdapter(this.adapterPl); } }
如果不存在下拉列表内容的配置文件,也可以通过程序来实现
public class MainActivity extends Activity { private Spinner sppl = null; private ArrayAdapter<CharSequence> adapterPl= null; private List<CharSequence> dataEdu = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); this.dataEdu = new ArrayList<CharSequence>(); this.dataEdu.add("C++"); this.dataEdu.add("C#"); this.dataEdu.add("Java"); this.sppl = (Spinner)super.findViewById(R.id.mypl); this.adapterPl = new ArrayAdapter<CharSequence>(this,android.R.layout.simple_spinner_item,this.dataEdu); this.adapterPl.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); this.sppl.setAdapter(this.adapterPl); } }
时间选择器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TimePicker android:id="@+id/tp1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <TimePicker android:id="@+id/tp2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
public class MainActivity extends Activity { private TimePicker tp = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.fragment_main); this.tp = (TimePicker)super.findViewById(R.id.tp1); tp.setIs24HourView(true); tp.setCurrentHour(11); tp.setCurrentMinute(42); } }
组件默认的是十二小时制的,可以通过程序设置二十四小时制,并设定时间
日期选择器
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <DatePicker android:id="@+id/dp1" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <DatePicker android:id="@+id/dp2" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout>
原文链接:https://blog.csdn.net/Fatestay_DC/article/details/48516853