1

在我的 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但现在我希望在按下对话框中的“确定”和“取消”按钮执行某些操作时获取事件。请给我解决方案。

4

3 回答 3

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();
于 2013-03-05T06:33:25.050 回答
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 的项目中的实际包

于 2013-03-05T08:50:08.177 回答
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();
于 2013-03-05T06:33:04.347 回答