Android 下拉框并添加事件

layout.xml

<!-- 添加下拉框 -->
<TextView android:id="@+id/xuanze" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:text="@string/xuanze"/>

<Spinner android:id="@+id/spinner" android:layout_height="wrap_content"

android:layout_width="wrap_content"/>

Layout.java

private Spinner mSpinner;

private static final String[] status = new String[3];

mSpinner = (Spinner)findViewById(R.id.spinner);
// mSpinner.setOnItemSelectedListener(mFontSizeChanged);
status[0] = "小";
status[1] = "中";
status[2] = "大";
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,status);
mSpinner.setAdapter(adapter) ;
//获取初始值回显
final SharedPreferences preferLoad = getSharedPreferences("settingData",Context.MODE_PRIVATE);
zhuti = preferLoad.getString("zhuti", ResourceManager.getString(getApplicationContext(), "zhuti"));
System.err.println(">>>>>回显值"+zhuti);
mSpinner.setSelection(Integer.parseInt(zhuti));//初始选中的值
mSpinner.setOnItemSelectedListener(new SpinnerSelectedListener());//添加事件

/**
* 下拉框选中后触发事件
*
*/
class SpinnerSelectedListener implements OnItemSelectedListener{
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
System.err.println("选择为>>>>"+status[arg2]);
if(zhixing){
final SharedPreferences preferLoad = getSharedPreferences("settingData",Context.MODE_PRIVATE);
Editor editor=preferLoad.edit();
editor.putString("zhuti", String.valueOf(arg2));
editor.commit();
Intent intent = new Intent(SystemSettingAct.this ,SystemSettingAct.class);
startActivity(intent);
}else{
zhixing = true;
}
}
public void onNothingSelected(AdapterView<?> arg0) {
}
}


你可能感兴趣的:(android)