您好,登錄后才能下訂單哦!
//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{ @Override public 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="睡覺(jué)" /> //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{ @Override public 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(); } } }
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。