Android:RadioGroup,RadioButton
RadioGroup:如果需要实现一组可选项中选择一个,可以使用该控件来实现;
RadioButton:继承自CompoundButton,是一个单选按钮;
学习如何使用RadioGroup实现选中,获取选中项:
效果:
源代码:
res\layout\activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <RadioGroup android:id="@+id/radioGroupCategory" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="27dp" > <RadioButton android:id="@+id/radioAndroid" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="Android" /> <RadioButton android:id="@+id/radioSpringMVC" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="SpringMVC" /> <RadioButton android:id="@+id/radioStruts" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Struts" /> <RadioButton android:id="@+id/radioHibernate" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hibernate" /> </RadioGroup> <TextView android:id="@+id/tvChoiceRadio" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/radioGroupCategory" android:layout_marginLeft="27dp" android:text="" /> </RelativeLayout>
MainActivity.java
1 package com.example.helloword; 2 3 import android.app.Activity; 4 import android.app.AlertDialog; 5 import android.content.DialogInterface; 6 import android.content.DialogInterface.OnClickListener; 7 import android.os.Bundle; 8 import android.renderscript.Script.KernelID; 9 import android.util.Log; 10 import android.view.KeyEvent; 11 import android.view.Menu; 12 import android.view.View; 13 import android.widget.Button; 14 import android.widget.EditText; 15 import android.widget.ImageView; 16 import android.widget.RadioButton; 17 import android.widget.RadioGroup; 18 import android.widget.TextView; 19 20 public class MainActivity extends Activity { 21 private RadioGroup radioGroupCategory; 22 private RadioButton radioAndroid, radioSpringMVC, radioStruts, 23 radioHibernate; 24 private TextView tvChoiceRadio; 25 26 @Override 27 protected void onCreate(Bundle savedInstanceState) { 28 super.onCreate(savedInstanceState); 29 setContentView(R.layout.activity_main); 30 31 this.tvChoiceRadio = (TextView) this.findViewById(R.id.tvChoiceRadio); 32 this.radioGroupCategory = (RadioGroup) this 33 .findViewById(R.id.radioGroupCategory); 34 35 this.radioAndroid = (RadioButton) this.findViewById(R.id.radioAndroid); 36 this.radioSpringMVC = (RadioButton) this 37 .findViewById(R.id.radioSpringMVC); 38 this.radioStruts = (RadioButton) this.findViewById(R.id.radioStruts); 39 this.radioHibernate = (RadioButton) this 40 .findViewById(R.id.radioHibernate); 41 42 this.radioStruts.setChecked(true); 43 44 this.radioGroupCategory 45 .setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 46 @Override 47 public void onCheckedChanged(RadioGroup radioGroup, 48 int checkedRadioId) { 49 Log.i("test", "onCheckedChanged event fired..." 50 + checkedRadioId); 51 Log.i("test", "Android" + R.id.radioAndroid); 52 Log.i("test", "SpringMVC" + R.id.radioSpringMVC); 53 Log.i("test", "Struts" + R.id.radioStruts); 54 Log.i("test", "Hibernate" + R.id.radioHibernate); 55 56 switch (checkedRadioId) { 57 case R.id.radioAndroid: 58 tvChoiceRadio.setText("checked value is Android"); 59 break; 60 case R.id.radioSpringMVC: 61 tvChoiceRadio.setText("checked value is SpringMVC"); 62 break; 63 case R.id.radioStruts: 64 tvChoiceRadio.setText("checked value is Struts"); 65 break; 66 case R.id.radioHibernate: 67 tvChoiceRadio.setText("checked value is Hibernate"); 68 break; 69 default: 70 tvChoiceRadio.setText("please choice..."); 71 break; 72 } 73 74 // if (radioAndroid.isChecked()) { 75 // tvChoiceRadio.setText("..checked value is Android"); 76 // } else if (radioSpringMVC.isChecked()) { 77 // tvChoiceRadio 78 // .setText("..checked value is SpringMVC"); 79 // } else if (radioStruts.isChecked()) { 80 // tvChoiceRadio.setText("..checked value is Struts"); 81 // } else if (radioHibernate.isChecked()) { 82 // tvChoiceRadio 83 // .setText("..checked value is Hibernate"); 84 // } 85 } 86 }); 87 88 } 89 90 @Override 91 public boolean onCreateOptionsMenu(Menu menu) { 92 // Inflate the menu; this adds items to the action bar if it is present. 93 getMenuInflater().inflate(R.menu.main, menu); 94 return true; 95 } 96 97 @Override 98 public boolean onKeyUp(int keyCode, KeyEvent event) { 99 // 当点击回退时,弹出该窗口(也就相当于关闭操作) 100 if (keyCode == KeyEvent.KEYCODE_BACK) { 101 new AlertDialog.Builder(this).setTitle("是否退出?") 102 .setPositiveButton("确定", new OnClickListener() { 103 @Override 104 public void onClick(DialogInterface arg0, int arg1) { 105 finish(); 106 } 107 }).setNegativeButton("取消", null).show(); 108 return true; 109 } 110 return super.onKeyUp(keyCode, event); 111 } 112 }
基础才是编程人员应该深入研究的问题,比如:
1)List/Set/Map内部组成原理|区别
2)mysql索引存储结构&如何调优/b-tree特点、计算复杂度及影响复杂度的因素。。。
3)JVM运行组成与原理及调优
4)Java类加载器运行原理
5)Java中GC过程原理|使用的回收算法原理
6)Redis中hash一致性实现及与hash其他区别
7)Java多线程、线程池开发、管理Lock与Synchroined区别
8)Spring IOC/AOP 原理;加载过程的。。。
【+加关注】。