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"); } }
时间: 2024-10-08 09:53:13