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文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="世界上最大的洋?" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView2" /> <!-- 单选组,单选按钮必须要放一个RadioGroup --> <RadioGroup android:layout_width="wrap_content" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A.大西洋" android:id="@+id/rbA" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B.太平洋" android:id="@+id/rbB" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C.印度洋" android:id="@+id/rbC" /> </RadioGroup> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" android:id="@+id/btnChoose" /> </LinearLayout>
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文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="你喜欢吃那些食物?" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView2" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="苹果" android:id="@+id/cb1" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="香蕉" android:id="@+id/cb2" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="桃子" android:id="@+id/cb3" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="西瓜" android:id="@+id/cb4" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="你喜欢" android:id="@+id/tvLove" /> </LinearLayout>
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. 下拉列表
① 在代码里面生成 下拉的数据
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择你的血型" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/spinnerText" /> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinner" /> </LinearLayout>
public class MainActivity extends ActionBarActivity{ private static final String[] m = {"A型", "B型", "O型", "AB型", "其他"}; private TextView tv; private Spinner spinner; private ArrayAdapter<String> 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<String>(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文件
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择你的血型" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/spinnerText" /> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/spinner" /> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?> <!-- 创建apinnerArray.xml --> <resources> <string-array name="plantes"> <item>A型</item> <item>B型</item> <item>O型</item> <item>AB型</item> <item>其他</item> </string-array> </resources>
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"); } }