- onclicklistener
- RadioButton
Onclick
点击属性
按钮注册监听事件
<Buttonandroid:id=\"@+id/textView1\"android:layout_width=\"match_parent\"android:layout_height=\"wrap_content\"android:text=\"Button\"android:gravity=\"center\"/>public class MainActivity extends Activity {private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button=(Button)findViewById(R.id.textView1);/**为Button点击事件注册一个监听器*/button.setOnClickListener(new OnClickListener(){public void onClick(View v){//添加逻辑}});}}
onclicklistener
//1种:接口OnClickListener ,在onclick响应public class MainActivity extends Activity implements OnClickListener { //这个类前提要接口OnClickListenerprivate Button receive; //按钮的初始化private Button falsewifiButton;public void init(){receive=(Button)findViewById(R.id.openwifi); //绑定一个xml按钮receive.setOnClickListener(MainActivity.this); //为这个已接口监听器的类放上功能}public void onClick(View v) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.openwifi:openwifi();break; }}简单来说:public class MainActivity extends Activity implements OnClickListener {public void onClick(View v) {}}2. 不接口OnClickListener ,在需要时才新建public class MainActivity{private Button receive; //按钮的初始化private Button falsewifiButton;Button btnCloseAP =(Button)this.findViewById(R.id.btnCloseAP);btnCloseAP.setOnClickListener(new OnClickListener() {public void onClick(View v) {if (isApEnabled()){setWifiApEnabled(false);}}} );}简单来说class A{A.setOnClickListener(new OnClickListener() {public void onClick(View v) {}); }//因为没把OnClickListener做接口,所以A button要使用监听响应功能时就要在参数里新建onclick,并在public void onclick方法里使用
RadioButton
单选按钮,就是只能够选中一个
eg:
<LinearLayout xmlns:android=\"http://schemas.android.com/apk/res/android\"xmlns:tools=\"http://schemas.android.com/tools\"android:id=\"@+id/LinearLayout1\"android:layout_width=\"match_parent\"android:layout_height=\"match_parent\"android:orientation=\"vertical\"tools:context=\".MainActivity\" ><TextViewandroid:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"请选择性别\"android:textSize=\"23dp\"/><RadioGroupandroid:id=\"@+id/radioGroup\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:orientation=\"horizontal\"><RadioButtonandroid:id=\"@+id/btnMan\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"男\"android:checked=\"true\"/><RadioButtonandroid:id=\"@+id/btnWoman\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"女\"/></RadioGroup><Buttonandroid:id=\"@+id/btnpost\"android:layout_width=\"wrap_content\"android:layout_height=\"wrap_content\"android:text=\"提交\"/></LinearLayout>
直接获取选中值
Button btnchange = (Button) findViewById(R.id.btnpost);RadioGroup radgroup = (RadioGroup) findViewById(R.id.radioGroup);//为radioGroup设置一个监听器:setOnCheckedChanged()btnchange.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {for (int i = 0; i < radgroup.getChildCount(); i++) {RadioButton rd = (RadioButton) radgroup.getChildAt(i);if (rd.isChecked()) {Toast.makeText(getApplicationContext(), \"点击提交按钮,获取你选择的是:\" + rd.getText(), Toast.LENGTH_LONG).show();break;}}}});