Android中RadionButton與CheckBox的應用

來源:互聯網
上載者:User

標籤:android radiobutton checkbox

//RadioGroup中xml檔案的配置<RadioGroup                 android:id="@+id/radiogroupid"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:orientation="horizontal"        >        <RadioButton             android:id="@+id/femalebutton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="female"            />        <RadioButton             android:id="@+id/malebutton"            android:layout_width="wrap_content"            android:layout_height="wrap_content"            android:text="male"            />            </RadioGroup>        //RadioGroup中activity中程式碼片段    public class MainActivity extends Activity {private RadioGroup radiogroup;private RadioButton femalebutton,malebutton;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        radiogroup=(RadioGroup)findViewById(R.id.radiogroupid);        femalebutton=(RadioButton)findViewById(R.id.femalebutton);        malebutton=(RadioButton)findViewById(R.id.malebutton);        RadioGroupLis r=new RadioGroupLis();        radiogroup.setOnCheckedChangeListener(r);    }            class RadioGroupLis implements OnCheckedChangeListener{@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {if(checkedId==femalebutton.getId()){Toast.makeText(getApplicationContext(), "選中female", Toast.LENGTH_LONG).show();}else if(checkedId==malebutton.getId()){Toast.makeText(getApplicationContext(), "選中male", Toast.LENGTH_SHORT).show();}}                }        CheckBox中xml檔案    <CheckBox     android:id="@+id/eatid"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="吃飯"        /><CheckBox     android:id="@+id/playid"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="遊戲"        /><CheckBox     android:id="@+id/sleepid"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:text="睡覺"        />        //checkbox中的activity檔案        public class MainActivity extends Activity {private CheckBox eatbox,sleepbox,playbox;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        eatbox=(CheckBox)findViewById(R.id.eatid);        sleepbox=(CheckBox)findViewById(R.id.sleepid);        playbox=(CheckBox)findViewById(R.id.playid);        onBoxLis listener=new onBoxLis();        eatbox.setOnClickListener(listener);        sleepbox.setOnClickListener(listener);        playbox.setOnClickListener(listener);            }        //onclickListener的使用方法    class onBoxLis implements OnClickListener{@Overridepublic void onClick(View v) {CheckBox box=(CheckBox)v;if(box.isChecked()){Toast.makeText(getApplicationContext(), "被選中",     Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "未被選中", Toast.LENGTH_LONG).show();}}        }


本文出自 “Java大白的戰地” 部落格,請務必保留此出處http://8023java.blog.51cto.com/10117207/1658339

Android中RadionButton與CheckBox的應用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.