简体   繁体   中英

android: how do I override Back button press?

I have a number of EditText lines on a UI where the user enters some data. The user enters a Date (from a DatePicker fragment) on the fListenerEditText line. The focus is then returned to the first EditText line which is cListenerEditText. For the default Back button behavior, if the user then presses the Back button, the Activity would close and the Date data would be immediately lost, and the user is returned to the previous Activity.

In my case, I would like to launch a Dialog fragment that asks the user if they want to discard the Date they previously entered at fListenerEditText. If the user clicks the "OK" button, the data is discarded, the Activity closes and the user returns to the previous activity. Note in my case, the soft keyboard is not open when the user presses the Back button key.

How do I use a FocusListener and a Back button listener together? What am I missing here?

public class EditActivity extends AppCompatActivity {

    private ListenerEditText cListenerEditText, dListenerEditText, eListenerEditText, fListenerEditText;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_edit); 

    cListenerEditText = (ListenerEditText) findViewById(id.CEditText);
    dListenerEditText = (ListenerEditText) findViewById(id.DEditText);
    eListenerEditText = (ListenerEditText) findViewById(id.EEditText);
    fListenerEditText = (ListenerEditText) findViewById(id.FEditText);

    final int stringDueDate = fListenerEditText.getText().toString().replace(" ", "").length();

    cListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus && fListenerEditText.getText().length() > 0) {
            // add some type of Back button listener here.    
            }
        }
    });
  1. Override method onBackPressed() and check the input length of field fListenerEditText . If its not 0 then show confirmation dialog otherwise call super.onBackPressed() to finish activity to show previous one from activity-stack.

  2. If your click on OK button, then just finish EditActivity .

Update EditActivity as below:

public class EditActivity extends AppCompatActivity {

    private ListenerEditText cListenerEditText, dListenerEditText, eListenerEditText, fListenerEditText;

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(layout.activity_edit);

        cListenerEditText = (ListenerEditText) findViewById(id.CEditText);
        dListenerEditText = (ListenerEditText) findViewById(id.DEditText);
        eListenerEditText = (ListenerEditText) findViewById(id.EEditText);
        fListenerEditText = (ListenerEditText) findViewById(id.FEditText);

        ..........
        ...............
    }

    @Override
    public void onBackPressed() {

        if (cListenerEditText.hasFocus() && fListenerEditText.getText().toString().length() > 0) {
            // Show dialog
            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Discard?");
            builder.setMessage("Your change will be discarded");

            builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {

                public void onClick(DialogInterface dialog, int which) {
                    // Finish activity
                    finish();
                }

            });

            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    // Do nothing
                    dialog.dismiss();
                }
            });

            AlertDialog alert = builder.create();
            alert.show();

        } else {
            super.onBackPressed();
        }
    }
}

FYI, If you want to do extra operation depending on the focus then use cListenerEditText.hasFocus() to check the focus and to change the focus after picking date just use cListenerEditText.requestFocus() and fListenerEditText.clearFocus() .

Hope this will help~

You can show new Dialog which asks if the user wants to discard the changes or keep them in OnBackpress() and onOptionSelected()

cListenerEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (hasFocus && fListenerEditText.getText().length() > 0) {
            // add some type of Back button listener here.  
              onBackPressed()
            }
        }
    });

@Override
public void onBackPressed() {
    new AlertDialog.Builder(this)
        .setTitle("Your dialog title")
        .setMessage("Do you want to discard the old changes")
        .setNegativeButton("No", new OnClickListener(){
          public void onClick(){
           // your task
          }
        })
        .setPositiveButton("yes", new OnClickListener() {

            public void onClick(DialogInterface arg0, int arg1) {
                EditActivity.super.onBackPressed();
            }
        }).create().show();
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM