在对话框中获取事件(确定,取消)按下按钮(安卓)(Get event on dialog( Ok ,

2019-10-17 19:01发布

在我的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 ,但现在我想在从对话框确定和取消按钮被按下做点什么寿获取事件。 请给我提供的解决方案。

Answer 1:

如果我没有得到你的问题,你要处理的“确定”和“取消”事件,然后执行基于响应一些行动。

// 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();


Answer 2:

您需要创建自定义编辑文本偏好如下。

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



Answer 3:

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();


文章来源: Get event on dialog( Ok ,Cancel) button pressed(Android)