创建LearnComponents
在资源Values目录的Strings.xml中添加代码
<string name="select_country">选择国家string> <string-array name="country_names"> <item>意大利item> <item>波兰item> <item>中国item> <item>澳大利亚item> string-array>
Activity_main.xml中添加如下代码
xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:orientation="vertical" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"> <TextView android:layout_width="wrap_content" android:id="@+id/tvConturyName" android:text="下拉控件Spinner的演示结果:" android:layout_height="wrap_content" /> <RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:text="@string/select_country" android:layout_width="wrap_content" android:layout_alignParentLeft="true" android:id="@+id/contury" android:layout_height="wrap_content" /> <Spinner android:layout_width="0dp" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="@+id/contury" android:id="@+id/spinner" /> RelativeLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:text="日期选择器DatePickerDialog的演示结果" android:id="@+id/tvDate" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择时间" android:id="@+id/btnChooseDate" /> LinearLayout> <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <TextView android:layout_width="wrap_content" android:text="时间选择器DatePickerDialog的演示结果" android:id="@+id/tvTime" android:layout_height="wrap_content" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="选择时间" android:id="@+id/btnChooseTime" /> LinearLayout> <LinearLayout android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="wrap_content"> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1"> <RadioGroup android:layout_width="match_parent" android:id="@+id/rd_Citys" android:layout_height="wrap_content"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="a:上海" android:id="@+id/rb_A" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="b:北京" android:id="@+id/rb_B" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="c:深圳" android:id="@+id/rb_C" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="d:福州" android:id="@+id/rb_D" /> RadioGroup> <Button android:layout_width="wrap_content" android:id="@+id/btnRB_commit" android:text="单选提交" android:layout_height="wrap_content" /> LinearLayout> <LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_weight="1"> <RadioGroup android:layout_width="match_parent" android:id="@+id/ck_Citys" android:layout_height="match_parent"> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="A:Java语言" android:id="@+id/cb_A" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="B:C语言" android:id="@+id/cb_B" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="C:Php语言" android:id="@+id/cb_C" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="D:Jsp语言" android:id="@+id/cb_D" /> RadioGroup> LinearLayout> LinearLayout> <LinearLayout android:layout_width="fill_parent" android:orientation="vertical" android:layout_height="wrap_content"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="单选框的RadioButton的演示结果" android:id="@+id/tvRadioButtonSel" android:textColor="#f53737" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="复选框的CheckBox的演示结果" android:id="@+id/tvCheckBoxSel" /> LinearLayout> LinearLayout>
在MainActivity.java添加如下代码:
public class MainActivity extends AppCompatActivity { private Spinner spinner; ArrayAdapteradapter; TextView tvConturyName; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); tvConturyName= (TextView) findViewById(R.id.tvConturyName); spinner = (Spinner) findViewById(R.id.spinner); adapter=ArrayAdapter.createFromResource(this,R.array.country_names,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> parent, View view, int position, long id) { System.out.println(parent.getItemAtPosition(position).toString()); tvConturyName.setText("国家名称:"+parent.getItemAtPosition(position).toString()); } @Override public void onNothingSelected(AdapterView> parent) { } }); } }
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener { private Spinner spinner; ArrayAdapteradapter; TextView tvConturyName; private TextView tvRadioButtonSel; private CheckBox cb_A,cb_B,cb_C,cb_D; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initSpinner(); //处理下来控件部分 initDate();//处理日期选择器 initTime();//处理时间选择器 initRadioButton();//处理RadioButton initCheckBox(); //处理CheckBox } private void initCheckBox() { cb_A= (CheckBox) findViewById(R.id.cb_A); cb_B= (CheckBox) findViewById(R.id.cb_B); cb_C= (CheckBox) findViewById(R.id.cb_C); cb_D= (CheckBox) findViewById(R.id.cb_D); cb_A.setOnCheckedChangeListener(this); cb_B.setOnCheckedChangeListener(this); cb_C.setOnCheckedChangeListener(this); cb_D.setOnCheckedChangeListener(this); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { TextView tvCheckBoxSel= (TextView) findViewById(R.id.tvCheckBoxSel); String sYouLanguage="你掌握的计算机编程语言有:"; if (cb_A.isChecked()){ sYouLanguage+=cb_A.getText()+","; } if (cb_B.isChecked()){ sYouLanguage+=cb_B.getText()+","; } if (cb_C.isChecked()){ sYouLanguage+=cb_C.getText()+","; } if (cb_D.isChecked()){ sYouLanguage+=cb_D.getText(); } tvCheckBoxSel.setText(sYouLanguage); } private void initRadioButton() { Button btnRB_commit; final RadioButton rb_A; tvRadioButtonSel= (TextView) findViewById(R.id.tvRadioButtonSel); btnRB_commit= (Button) findViewById(R.id.btnRB_commit); rb_A= (RadioButton) findViewById(R.id.rb_A); btnRB_commit.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (rb_A.isChecked()){ tvRadioButtonSel.setText("单选题你选择是正确的"+rb_A.getText()); tvRadioButtonSel.setTextColor(Color.BLUE); }else{ tvRadioButtonSel.setText("单选题你选择是错误的!!!!"); tvRadioButtonSel.setTextColor(Color.RED); } } }); } private void initTime() { final TextView tvTime; Button btnChooseTime; tvTime= (TextView) findViewById(R.id.tvTime); btnChooseTime= (Button) findViewById(R.id.btnChooseTime); btnChooseTime.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) { tvTime.setText(String.format("你选择的时间是:%d-%d",hourOfDay,minute)); } },23,59,true).show(); } }); } private void initDate() { final TextView tvDate; Button btnChoosedate; tvDate= (TextView) findViewById(R.id.tvDate); btnChoosedate= (Button) findViewById(R.id.btnChooseDate); btnChoosedate.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 monthOfYear, int dayOfMonth) { tvDate.setText(String.format("你选择的时间是:%d-%d-%d", year, monthOfYear + 1, dayOfMonth)); } }, 2015, 3, 30).show(); } }); } private void initSpinner() { tvConturyName= (TextView) findViewById(R.id.tvConturyName); spinner = (Spinner) findViewById(R.id.spinner); adapter=ArrayAdapter.createFromResource(this,R.array.country_names,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView> parent, View view, int position, long id) { tvConturyName.setText("你选择的国家是::"+parent.getItemAtPosition(position).toString()); } @Override public void onNothingSelected(AdapterView> parent) { } }); } }