在对话框中获取事件(确定,取消)按钮pressed(Android版) [英] Get event on dialog( Ok ,Cancel) button pressed(Android)
问题描述
在我的Android应用程序,我显示,其中包含的EditText
的DialogBox的。使用时会显示该DialogBox的 preferenceCategory
。我的 XML
文件看起来像
In my Android application I am displaying the dialogbox which contains edittext
. This dialogbox is displayed using PreferenceCategory
.My xml
file looks like
<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文件看起来像
My Java file looks like
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");
}
有是在显示对话框
没有问题,但现在我想寿获取事件,当从对话框,确定和取消按钮pssed做点什么$ P $。
请给我提供的解决方案。
There is no issue in displaying the dialog
but now i want tho get event when Ok and Cancel button from dialog box is pressed to do something.
Please provide me solution.
推荐答案
您需要创建自定义编辑文本preference如下:
You will need to create your custom edit text preference as follows.
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文件中按如下方式使用它:
Then use it in xml file as follows:
<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应该由实际的包在你的项目被替换在其中创建MyEditText preference
where com.package should be replaced by the actual package in your project where you create MyEditTextPreference
这篇关于在对话框中获取事件(确定,取消)按钮pressed(Android版)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!