在我的Android应用程序中,我显示的对话框包含edittext
。此对话框使用PreferenceCategory
显示。我的xml
文件类似于
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/security_setting_edittext_hint" >
<EditTextPreference
android:dialogTitle="@string/security_setting_button"
android:key="set_password_preference"
android:summary="@string/set_password_summary"
android:title="@string/security_setting_button"
android:inputType="number"
android:icon="@drawable/lock"
/>
</PreferenceCategory>
</PreferenceScreen>
我的Java文件看起来像
public class Settings extends PreferenceActivity {
Dialog setPasswordDialog;
EditText setPassword;
EditTextPreference editPreference;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("Settings");
addPreferencesFromResource(R.xml.preference_authentication);
editPreference=(EditTextPreference) findPreference("set_password_preference");
}
显示dialog
时没有问题,但是现在我想要在按下对话框中的“确定”和“取消”按钮时执行某事。
请给我解决方案。
答案 0 :(得分:3)
如果我正确地提出了您的问题,您希望处理“确定”和“取消”事件,然后根据响应执行某些操作。
// This is using code:
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("TITLE HERE");
alert.setMessage("MESSAGE");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something here where "ok" clicked
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//So sth here when "cancel" clicked.
}
});
alert.show();
答案 1 :(得分:3)
您需要按如下方式创建自定义编辑文字首选项。
public class MyEditTextPreference extends EditTextPreference {
public MyEditTextPreference(Context context) {
super(context);
}
public MyEditTextPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public MyEditTextPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
// Put your logic here for Ok button press
break;
case DialogInterface.BUTTON_NEGATIVE:
// Put your logic here for Cancel button press
break;
}
super.onClick(dialog, which);
}
}
然后在xml文件中使用它,如下所示:
<com.package.MyEditTextPreference
android:dialogTitle="@string/security_setting_button"
android:key="set_password_preference"
android:summary="@string/set_password_summary"
android:title="@string/security_setting_button"
android:inputType="number"
android:icon="@drawable/lock"
/>
其中com.package应替换为您创建MyEditTextPreference的项目中的实际包
答案 2 :(得分:1)
DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
switch (which){
case DialogInterface.BUTTON_POSITIVE:
//Yes button clicked
break;
case DialogInterface.BUTTON_NEGATIVE:
//No button clicked
break;
}
}
};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure?").setPositiveButton("Yes", dialogClickListener)
.setNegativeButton("No", dialogClickListener).show();