主要实现在ListPreference弹出的对话框中的某一项选中时,再弹出一个编辑框,具体代码如下:
效果如图所示
[img]http://dl.iteye.com/upload/attachment/611782/ce3180a3-b083-3950-a5b8-e0104bb3e983.png[/img]
[img]http://dl.iteye.com/upload/attachment/611784/c9a1fc23-c019-3220-abb5-9b2c0075e83f.png[/img]
/**
* @author wu xiang
* @date Dec 26, 2011
* @fileName TestPre.java
*
*/
public class MyListPreference extends ListPreference implements OnPreferenceClickListener {
private Toast myToast;
/**
* @param context
* @param attrs
*/
public MyListPreference(Context context, AttributeSet attrs) {
super(context, attrs);
myToast=Toast.makeText(context, "", Toast.LENGTH_SHORT);
}
public boolean onPreferenceClick(Preference preference) {
// TODO Auto-generated method stub
return false;
}
@Override
protected void onPrepareDialogBuilder(Builder builder) {
int checkItem=getValueIndex(getValue());
builder.setSingleChoiceItems(R.array.pref_search_limit_choices, checkItem, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
if(3==which){
final EditText et=new EditText(MyListPreference.this.getContext());
et.setText(getValue());
new AlertDialog.Builder(MyListPreference.this.getContext())
.setTitle(R.string.setting_title)
.setView(et)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String sValue=et.getText().toString();
if(Pattern.compile("\\d+").matcher(sValue).matches()){
int iValue=Integer.parseInt(sValue);
if(iValue>0&&iValue<50){
setValue(iValue+"");
}
}else{
myToast.setText(R.string.pref_prompt);
myToast.show();
}
}
})
.setNeutralButton(android.R.string.cancel, null)
.create().show();
}else{
setValue(getEntryValues()[which]+"");
}
dialog.dismiss();
}
});
builder.setPositiveButton(null, null);
}
@Override
protected void onDialogClosed(boolean positiveResult) {
}
private int getValueIndex(String value){
int len=getEntryValues().length;
for(int i=0;i<len;i++){
if(value.equals(getEntryValues()[i])){
return i;
}
}
return 3;//选中“自定义” 一项
}
}
效果如图所示
[img]http://dl.iteye.com/upload/attachment/611782/ce3180a3-b083-3950-a5b8-e0104bb3e983.png[/img]
[img]http://dl.iteye.com/upload/attachment/611784/c9a1fc23-c019-3220-abb5-9b2c0075e83f.png[/img]