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

首頁 > 系統 > Android > 正文

Android 中CheckBox的isChecked的使用實例詳解

2019-10-23 18:30:46
字體:
來源:轉載
供稿:網友

Android 中CheckBox的isChecked的使用實例詳解

范例說明

所有的網絡服務在User使用之前,都需要簽署同意條款,在手機應用程序、手機游戲的設計經驗中,常看見CheckBox在同意條款情境的運用,其選取的狀態有兩種即isChecked=true與isChecked=false。

以下范例將設計一個TextView放入條款文字,在下方配置一個CheckBox Widget作為選取項,通過Button.onClickListener按鈕事件處理,取得User同意條款的狀態。

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

范例程序

src/irdc.ex04_04/EX04_04.java

利用CheckBox.OnClickListener里的事件來判斷Button該不該顯示,其方法就是判斷Button.Enabled的值;在一開始時,默認參數為false,當有單擊CheckBox時,Button參數就修改為true。

/* import程序略 */ public class EX04_04 extends Activity{ /** Called when the activity is first created. */  /*聲明 TextView、CheckBox、Button對象*/ 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默認為未選擇狀態*/ 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())  {   /*設置Button為不能選擇對象*/   myButton.setEnabled(true);   myTextView2.setText("");  }  else  {   /*設置Button為可以選擇對象*/   myButton.setEnabled(false);   myTextView1.setText(R.string.text1);   /*在TextView2里顯示出"請勾選我同意"*/   myTextView2.setText(R.string.no);     }  } });   myButton.setOnClickListener(new Button.OnClickListener() {  // 程序略  });  }}

擴展學習

CheckBox在默認內容為空白時(沒有任何默認的提示文字下),可設置提示User的文字,其調用的方法為CheckBox.setHint()方法;在擴展學習的范例練習,是抓取R.string.hello這個字符串常數,其與默認CheckBox文字的結果是相同的,你不妨試試看。

 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); /*設置文字顏色*/myCheckBox.setHintTextColor(Color.RED);

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


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 青海省| 庆元县| 台安县| 荥经县| 抚顺县| 彰化县| 巴里| 辽源市| 霍林郭勒市| 乌兰浩特市| 同德县| 珠海市| 静海县| 大宁县| 东阳市| 承德市| 怀集县| 包头市| 祁阳县| 宁津县| 文登市| 甘泉县| 青州市| 汶川县| 昔阳县| 林周县| 定州市| 泰安市| 九江县| 政和县| 大同县| 兴城市| 新巴尔虎右旗| 堆龙德庆县| 榕江县| 莱芜市| 莱芜市| 岳阳市| 谢通门县| 临泽县| 定边县|