简体   繁体   中英

How do I test EditText for user content even after it loses focus?

I have a simple UI screen for user to enter data in an EditText line and then enter a date in the second EditText line. I have a ListenerEditText.java file that listens for back button presses. Problem is when the user enters data on the first EditText line and then focus moves to the second EditText line, the code no longer catches the back button presses correctly because it fails to recognize that data was entered on the first EditText line--it appears to only check whether there is data on the second EditText line. When the back button is pressed, I would like the code to recognize there is data on the first EditText line and/or on the second EditText line and then launch a DialogFragment to confirm if the user will lose the data entered. Currently, the code doesn't "see" the data on the first EditText line and if there is no data on the second EditText line, the user is incorrectly brought back to the previous screen.

Activity.java:

import static com.example.jdw.secondscreen.ListenerEditText.KeyImeChange;

public class CardViewActivity extends AppCompatActivity {

   private ListenerEditText myListenerEditText;
   private ListenerEditText dListenerEditText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cardviewinput);

        myListenerEditText = (ListenerEditText)findViewById(R.id.CEditText);
        dListenerEditText = (ListenerEditText) findViewById(R.id.DEditText);

        myListenerEditText.setKeyImeChangeListener(new KeyImeChange() {

        @Override
        public boolean onKeyIme(int keyCode, KeyEvent event) {
         int stringToDo = myListenerEditText.getText().toString().trim().length();

            if(stringToDo>0) {
                FragmentManager fm = getSupportFragmentManager();
                CreateSkycardFragment editNameDialog = new CreateSkycardFragment();
                editNameDialog.show(fm, "skycard_dialog");
                return false;
            }
            // if "dstringToDo" does not have data (the EditText input line is blank)
            // then just cancel the soft keyboard and go to the previous activity.
            else {
                InputMethodManager imm = (InputMethodManager)
                   getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(myListenerEditText.getWindowToken(), 0);
                return false;
            }
        }
    });

        dListenerEditText.setKeyImeChangeListener(new KeyImeChange() {

        @Override
        public boolean onKeyIme(int keyCode, KeyEvent event) {
            int dstringToDo = dListenerEditText.getText().toString().trim().length();
            // If the EditText input line has data ("stringToDo") and the user presses the Back button,
            // then launch the DialogFragment to see if they really want to delete the data and
            // go back to the previous activity.
            if(dstringToDo>0) {
                FragmentManager fm = getSupportFragmentManager();
                CreateSkycardFragment editNameDialog = new CreateSkycardFragment();
                editNameDialog.show(fm, "skycard_dialog");
                return false;
            }
            // if "dstringToDo" does not have data (the EditText input line is blank)56
            // then just cancel the soft keyboard and go to the previous activity.
            else {
                InputMethodManager imm = (InputMethodManager)
                        getSystemService(Context.INPUT_METHOD_SERVICE);
                imm.hideSoftInputFromWindow(dListenerEditText.getWindowToken(), 0);
                return false;
            }
        }
    });

...

you can do something like this,

EditText edit = (EditText)getActivity.findViewById(R.id.your id here);
String abcd=edit.getText().toString();

and do what ever validation, set error and other things on the first edit text.

There is two option which i used First is same as @war_Hero told

First
/ Add validation to your edittext /

        myListenerEditText.setError(null);

        /** Validation of View Widget **/
        if (myListenerEditText.getText().toString().equalsIgnoreCase("")) {
            myListenerEditText.requestFocus();
            myListenerEditText.setError("Please enter your name.");
        }

        // Second option

    myListenerEditText = (EditText) rootView.findViewById(your id);
    //add addTextChangedListener to your editText and define one boolean variable  boolean showPopUp=false
    myListenerEditText.addTextChangedListener(watcher);


    TextWatcher watcher = new TextWatcher() {
    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1,int i2) {
        showPopUp = true;// whenever text changed make showPopUp= true;
    }
    @Override
    public void afterTextChanged(Editable editable) {           
    }
    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {    

    }

};

 //and then override onBackPressed

 @Override
public void onBackPressed() {

    if (showPopUp) {
        showPopUpMessage();// showPopUpMessage will show dialog 

    }else{
        finish();
    }

}

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