在平常的開發中單選按鍵和下拉按鍵是非常常用的2個點擊事件。首先介紹下單選按鍵
1:單選按鍵,單選的主鍵是radiogroup 這個主鍵也是很重要的
首先介紹下主鍵的佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/myRadioListenerTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20px"
android:text="您選擇的性別是:"/>
<RadioGroup android:id="@+id/myRadioListenerRG"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checkedButton="@+id/myRadioListenerMale">
<RadioButton android:id="@+id/myRadioListenerMale"
android:text="男"/>
<RadioButton android:id="@+id/myRadioListenerFemale"
android:text="女"/>
</RadioGroup>
</LinearLayout>
在單選RadioGroup 中裏面的選擇項主要是由RadioButton 組成,這和一般的主鍵區別不大 radiogroup裏面的屬性就不一一介紹了。
private RadioGroup radioGroup;
private TextView textView;
private RadioButton btMale,btFemale;
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.myradiolistener);
this.radioGroup=(RadioGroup)super.findViewById(R.id.myRadioListenerRG);
this.textView=(TextView)super.findViewById(R.id.myRadioListenerTV);
this.btMale=(RadioButton)super.findViewById(R.id.myRadioListenerMale);
this.btFemale=(RadioButton)super.findViewById(R.id.myRadioListenerFemale);
this.radioGroup.setOnCheckedChangeListener(new CheckedChanegdListener());
}
public RadioGroup getRadioGroup() {
returnradioGroup;
}
publicvoid setRadioGroup(RadioGroup radioGroup) {
this.radioGroup = radioGroup;
}
public TextView getTextView() {
returntextView;
}
publicvoid setTextView(TextView textView) {
this.textView = textView;
}
public RadioButton getBtMale() {
returnbtMale;
}
publicvoid setBtMale(RadioButton btMale) {
this.btMale = btMale;
}
public RadioButton getBtFemale() {
returnbtFemale;
}
publicvoid setBtFemale(RadioButton btFemale) {
this.btFemale = btFemale;
}
this.radioGroup.setOnCheckedChangeListener(new CheckedChanegdListener());
就是單選按鍵事件的重要點,我們不推薦在裏面直接寫內部類的方式
在單選RadioGroup 主要是實現CheckedChanegdListener方法
這個類是實現OnCheckedChangeListener這個接口的
publicvoid onCheckedChanged(RadioGroup group, int checkedId) {
if(group.getId()==R.id.myRadioListenerRG){
String temp="";
myRadioListener my1=(myRadioListener)group.getContext();
if(my1.getBtMale().getId()==checkedId){
temp="您選擇的性別是:"+my1.getBtMale().getText();
}elseif(my1.getBtFemale().getId()==checkedId){
temp="您選擇的性別是:"+my1.getBtFemale().getText();
}
my1.getTextView().setText(temp);
}
在onCheckedChanged 這個方法中有2個參數第一個是當前的單選按鍵,第二個是被選中的radiobutton的id
group.getId()==R.id.myRadioListenerRG 判斷當前是對那個單選按鍵操作
1:下拉列表(Spinner)
下拉列表的的主鍵是Spinner 這個主鍵和其他的主鍵有點區別,主要是數據源的來源,可以在代碼中實現,也可以用配置的方式
下面的程序是用代碼實現數據源的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView android:id="@+id/mySpinnerClickTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="您選擇的顏色是:"/>
<Spinner android:id="@+id/mySpinnerClickSpinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
package com.bruce;
import com.bruce.OnClickListener.ItemClickListenerImpl;
import com.bruce.OnClickListener.ItemSelectedListenerImpl;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
publicclass mySpinnerClick extends Activity{
private TextView tv;
private Spinner spinner;
private ArrayAdapter<String> adapterColor=null;
private String [] colorArray={"紅","黃","藍"};
public TextView getTv() {
returntv;
}
publicvoid setTv(TextView tv) {
this.tv = tv;
}
public Spinner getSpinner() {
returnspinner;
}
publicvoid setSpinner(Spinner spinner) {
this.spinner = spinner;
}
protectedvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.myspinnerclick);
this.tv=(TextView)super.findViewById(R.id.mySpinnerClickTV);
this.spinner=(Spinner)super.findViewById(R.id.mySpinnerClickSpinner);
this.adapterColor=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,colorArray);
this.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapterColor);
this.spinner.setPrompt("您選擇的顏色是:");
this.spinner.setOnItemSelectedListener(new ItemSelectedListenerImpl());
}
}
在下拉列表中主要裝載數據的是ArrayAdapter 這個組件裏面有很多方法在這裏主要用了其中的2個
this.adapterColor=new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,colorArray);
創建一個ArrayAdapter 並把數據和其放在了一起android.R.layout.simple_spinner_item是怎麼在主鍵中展現
his.adapterColor.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);打開下拉列表是用什麼的方式
spinner其自身也有很多的方法比如設置下拉列表頭this.spinner.setPrompt("您選擇的顏色是:");
其選擇事件是this.spinner.setOnItemSelectedListener(new ItemSelectedListenerImpl())
ItemSelectedListenerImpl是實現了OnItemSelectedListener接口要實現2個方法
publicvoid onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
System.out.println(parent.getId());
System.out.println(view.getId());
System.out.println(R.id.mySpinnerClickSpinner);
System.out.println(view.getId() == R.id.mySpinnerClickSpinner);
if(parent.getId()==R.id.mySpinnerClickSpinner){
String value = parent.getItemAtPosition(position).toString();
mySpinnerClick my1 = (mySpinnerClick) view.getContext();
my1.getTv().setText("您選擇的顏色是:" + value);
}
publicvoid onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
onItemSelected有4個參數,parent當前下拉列表,view當前視圖,position下拉列表id,id選擇的下拉列表值
onItemSelected是對下拉列表操作的時候做的事
onNothingSelected 不做任何操作的時候做的事