Receive event in dialog box (OK, Cancel) clicked (Android)

In my Android application, I show a dialog box containing edittext . This dialog box is displayed using the 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> 

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"); } 

There is no problem displaying the dialog , but now I want to get the event when the Ok and Cancel buttons are clicked from the dialog box to do something. Please provide me a solution.

+1
source share
3 answers

If I ask the question correctly, you want to handle the OK and Cancel events, and then perform some action based on the response.

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

You will need to create your custom text editing option 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); } } 

Then use it in the 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" /> 

where com.package should be replaced with the actual package in your project, where you create MyEditTextPreference

+3
source
 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(); 
+1
source

Source: https://habr.com/ru/post/887307/


All Articles