国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 系統(tǒng) > Android > 正文

Android 中CheckBox的isChecked的使用實(shí)例詳解

2019-12-12 03:02:09
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

Android 中CheckBox的isChecked的使用實(shí)例詳解

范例說(shuō)明

所有的網(wǎng)絡(luò)服務(wù)在User使用之前,都需要簽署同意條款,在手機(jī)應(yīng)用程序、手機(jī)游戲的設(shè)計(jì)經(jīng)驗(yàn)中,常看見(jiàn)CheckBox在同意條款情境的運(yùn)用,其選取的狀態(tài)有兩種即isChecked=true與isChecked=false。

以下范例將設(shè)計(jì)一個(gè)TextView放入條款文字,在下方配置一個(gè)CheckBox Widget作為選取項(xiàng),通過(guò)Button.onClickListener按鈕事件處理,取得User同意條款的狀態(tài)。

當(dāng)CheckBox.isChecked為true,更改TextView的文字內(nèi)容為“你已接受同意!!”,當(dāng)未選取CheckBox時(shí),Button則不可以被選擇的(被Disabled)。

范例程序

src/irdc.ex04_04/EX04_04.java

利用CheckBox.OnClickListener里的事件來(lái)判斷Button該不該顯示,其方法就是判斷Button.Enabled的值;在一開始時(shí),默認(rèn)參數(shù)為false,當(dāng)有單擊CheckBox時(shí),Button參數(shù)就修改為true。

/* import程序略 */ public class EX04_04 extends Activity{ /** Called when the activity is first created. */  /*聲明 TextView、CheckBox、Button對(duì)象*/ public TextView myTextView1; public TextView myTextView2; public CheckBox myCheckBox; public Button myButton;  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main);  /*取得TextView、CheckBox、Button*/ myTextView1 = (TextView) findViewById(R.id.myTextView1); myTextView2 = (TextView) findViewById(R.id.myTextView2); myCheckBox = (CheckBox) findViewById(R.id.myCheckBox); myButton = (Button) findViewById(R.id.myButton);  /*將CheckBox、Button默認(rèn)為未選擇狀態(tài)*/ myCheckBox.setChecked(false); myButton.setEnabled(false);  myCheckBox.setOnClickListener(new CheckBox.OnClickListener() {  @Override  public void onClick(View v)  {  // TODO Auto-generated method stub  if(myCheckBox.isChecked())  {   /*設(shè)置Button為不能選擇對(duì)象*/   myButton.setEnabled(true);   myTextView2.setText("");  }  else  {   /*設(shè)置Button為可以選擇對(duì)象*/   myButton.setEnabled(false);   myTextView1.setText(R.string.text1);   /*在TextView2里顯示出"請(qǐng)勾選我同意"*/   myTextView2.setText(R.string.no);     }  } });   myButton.setOnClickListener(new Button.OnClickListener() {  // 程序略  });  }}

擴(kuò)展學(xué)習(xí)

CheckBox在默認(rèn)內(nèi)容為空白時(shí)(沒(méi)有任何默認(rèn)的提示文字下),可設(shè)置提示User的文字,其調(diào)用的方法為CheckBox.setHint()方法;在擴(kuò)展學(xué)習(xí)的范例練習(xí),是抓取R.string.hello這個(gè)字符串常數(shù),其與默認(rèn)CheckBox文字的結(jié)果是相同的,你不妨試試看。

 myTextView1 = (TextView) findViewById(R.id.myTextView1);myTextView2 = (TextView) findViewById(R.id.myTextView2);myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);myButton = (Button) findViewById(R.id.myButton);myCheckBox.setChecked(false); /*利用setHIT抓取strings里面的值*/CharSequence hint = getString(R.string.hello);myCheckBox.setHint(hint); /*設(shè)置文字顏色*/myCheckBox.setHintTextColor(Color.RED);

感謝閱讀,希望能幫助到大家,謝謝大家對(duì)本站的支持!

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 武夷山市| 同仁县| 蕉岭县| 安远县| 邓州市| 西和县| 云南省| 永定县| 连城县| 贞丰县| 海兴县| 石楼县| 五台县| 兴化市| 九龙坡区| 米林县| 大悟县| 廉江市| 高安市| 永年县| 呼伦贝尔市| 海城市| 新巴尔虎右旗| 理塘县| 无锡市| 韩城市| 新乡县| 南陵县| 宜宾县| 汾阳市| 渑池县| 钟山县| 泸定县| 西华县| 惠安县| 凤城市| 临颍县| 恩平市| 万宁市| 东丰县| 富宁县|