checkBox:
xml中的聲明
<CheckBox<br /> android:id="@+id/myCheckBox"<br /> android:layout_width="97px"<br /> android:layout_height="wrap_content"<br /> android:text="@string/str_agree"<br /> android:layout_x="99px"<br /> android:layout_y="318px"<br /> />
在activity中定義:
myCheckBox = (CheckBox) findViewById(R.id.myCheckBox);
添加監聽事件:
myCheckBox.setOnClickListener(new CheckBox.OnClickListener()<br /> {<br /> @Override<br /> public void onClick(View v)<br /> {<br /> // TODO Auto-generated method stub<br /> if(myCheckBox.isChecked())<br /> {<br /> /*設定Button為不能選擇對象*/<br /> myButton.setEnabled(true);<br /> myTextView2.setText("");<br /> }<br /> else<br /> {<br /> /*設定Button為可以選擇對象*/<br /> myButton.setEnabled(false);<br /> myTextView1.setText(R.string.text1);<br /> /*在TextView2裡顯示出"請勾選我同意"*/<br /> myTextView2.setText(R.string.no);<br /> }<br /> }<br /> });
可以使用多個CheckBox,構造多選項CheckBox。
RadioGroup與RadioButton:
xml聲明,RadioGroup將RadioButton包在裡面:
<RadioGroup<br /> android:id="@+id/myRadioGroup"<br /> android:layout_width="137px"<br /> android:layout_height="216px"<br /> android:orientation="vertical"<br /> android:layout_x="3px"<br /> android:layout_y="54px"<br /> ><br /> <!--第一個RadioButton --><br /> <RadioButton<br /> android:id="@+id/myRadioButton1"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/tr_radio_op1"<br /> /><br /> <!--第二個RadioButton --><br /> <RadioButton<br /> android:id="@+id/myRadioButton2"<br /> android:layout_width="wrap_content"<br /> android:layout_height="wrap_content"<br /> android:text="@string/tr_radio_op2"<br /> /><br /> </RadioGroup>
activity中的定義:
mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup);<br /> mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1);<br /> mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2);
監聽函數:
private RadioGroup.OnCheckedChangeListener mChangeRadio = new<br /> RadioGroup.OnCheckedChangeListener()<br /> {<br /> @Override<br /> public void onCheckedChanged(RadioGroup group, int checkedId)<br /> {<br /> // TODO Auto-generated method stub<br /> if(checkedId==mRadio1.getId())<br /> {<br /> /*把mRadio1的內容傳到mTextView1*/<br /> mTextView1.setText(mRadio1.getText());<br /> }<br /> else if(checkedId==mRadio2.getId())<br /> {<br /> /*把mRadio2的內容傳到mTextView1*/<br /> mTextView1.setText(mRadio2.getText());<br /> }<br /> }<br /> };
通過mRadioGroup1.clearCheck();來取消選擇
ImageView:
關鍵在於個getResources()方法,這個方法負責訪問Resource ID,無論是訪問資源裡的圖片檔案、文字都要用到getResources();在此使用getResources().getDrawable()來載入res/drawable裡的圖片檔案,並將圖片放置在ImageView當中。
mImageView1.setImageDrawable(getResources().getDrawable(R.drawable.right));
通過setAlpha(int a);來設定透明度
另外,建立兩個ImageView,一個為外框、另一個為內框,圖片需要做一個排序堆棧順序,將前景圖放在上方(以AbsoluteLayout),背景圖放在前景圖的下方,這是最簡單的堆棧順序方法。ImageButton也可以做成堆棧的形式,這樣就不僅有背景圖,還有按鈕事件。
Spinner與ArrayAdapter搭配使用,構造Spinner:
//定義ArrayList,用來存放條目<br /> allCountries = new ArrayList<String>();<br /> for (int i = 0; i < countriesStr.length; i++)<br /> {<br /> allCountries.add(countriesStr[i]);<br /> }<br /> // 定義 ArrayAdapter對象並將ArrayList傳入<br /> adapter = new ArrayAdapter<String>(this,<br /> android.R.layout.simple_spinner_item, allCountries);<br /> adapter<br /> .setDropDownViewResource<br /> (android.R.layout.simple_spinner_dropdown_item);</p><p> //將Spinner設定為該adapter<br /> mySpinner.setAdapter(adapter);