<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ThinkChat2.0新版上線,更智能更精彩,支持會話、畫圖、視頻、閱讀、搜索等,送10W Token,即刻開啟你的AI之旅 廣告
                單選按鈕(RadioButton)和復選框(CheckBox),狀態開關按鈕(ToggleButton),開關(Switch)都是普通的UI組件,都繼承了Button類,因此都可以用Button的各種屬性和方法。 **RadioButton通常要與RadioGroup一起使用,用于定義一組單選按鈕** 對于二者而言,最主要的還是要看他們的監聽器,RadioButton的事件監聽器是: **單選按鈕的監聽接口是OnCheckedChangeListener** ~~~ radiobutton.setOnCheckedChangeListener(new OnCheckedChangeListener(){ ?//單選框的監聽器,OnCheckedChangeListener是單選框的監聽接口,為radiogroup組建的Oncheck事件綁定監聽器 @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub //處理操作 } }); ~~~ 而復選框CheckBox的監聽器是:**注意復選框的監聽接口是CompoundButton.OnCheckedChangeListener** ~~~ //復選框的監聽器,CompoundButton.OnCheckedChangeListener是復選框的監聽接口 checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub //處理操作 } }); ~~~ 例: main.xml ~~~ <?xml version="1.0" encoding="utf-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TableRow android:id="@+id/tableRow1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="性 別:" android:textSize="23dp" /> <!-- RadioButton需要在RadioGroup組件里面 --> <RadioGroup android:id="@+id/radioGroup1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <RadioButton android:id="@+id/radio0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:checked="true" android:text="男" android:textSize="23dp" /> <RadioButton android:id="@+id/radio1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="女" android:textSize="23dp" /> </RadioGroup> </TableRow> <TableRow android:id="@+id/tableRow2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="喜歡的顏色:" android:textSize="23dp" /> <!-- 線性布局組件中有三個復選框 --> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical" > <CheckBox android:id="@+id/checkBox1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="紅色" android:textSize="23dp"/> <CheckBox android:id="@+id/checkBox2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="藍色" android:textSize="23dp"/> <CheckBox android:id="@+id/checkBox3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="綠色" android:textSize="23dp"/> </LinearLayout> </TableRow> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="23dp" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textSize="20dp" /> </TableLayout> ~~~ MainActivity.java ~~~ package com.hust.radiocheckbuttontest; import android.app.Activity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends Activity { String str=null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //從布局文件中獲取radiogroup組件和textview組件 RadioGroup rg=(RadioGroup) findViewById(R.id.radioGroup1); final TextView showradio=(TextView) findViewById(R.id.textView3); final TextView showcheck=(TextView) findViewById(R.id.textView4); //獲取復選框組件 final CheckBox red=(CheckBox) findViewById(R.id.checkBox1); final CheckBox blue=(CheckBox) findViewById(R.id.checkBox2); final CheckBox green=(CheckBox) findViewById(R.id.checkBox3); //str.append("喜歡的顏色是:"); //單選框的監聽器,OnCheckedChangeListener是單選框的監聽接口,為radiogroup組建的Oncheck事件綁定監聽器 rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // TODO Auto-generated method stub //根據備選按鈕的Id改變s的值 String s=checkedId==R.id.radio0?"您的性別是男人":"您的性別是女人"; //文本框顯示文本 showradio.setText(s); } }); //復選框的監聽器,CompoundButton.OnCheckedChangeListener是復選框的監聽接口 red.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub if(isChecked){ str=(String) red.getText(); showcheck.setText(str); } } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } ~~~ ![](https://box.kancloud.cn/2016-03-10_56e0d9a8918de.jpg)
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看