简体   繁体   English

即使失去焦点,我如何测试EditText的用户内容?

[英]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. 我有一个简单的UI屏幕,供用户在EditText行中输入数据,然后在第二EditText行中输入日期。 I have a ListenerEditText.java file that listens for back button presses. 我有一个ListenerEditText.java文件,用于监听后退按钮的按下。 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. 问题是,当用户在第一行EditText行上输入数据,然后焦点移至第二行EditText行时,该代码不再正确捕获后退按钮的按下,因为它无法识别出在第一行EditText行上输入了数据-出现仅检查第二行EditText行上是否有数据。 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. 当按下后退按钮时,我希望代码识别出第一行EditText行和/或第二行EditText行上有数据,然后启动DialogFragment来确认用户是否会丢失输入的数据。 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. 当前,该代码无法“看到”第一条EditText行上的数据,并且如果第二条EditText行上没有数据,则会错误地将用户带回到上一个屏幕。

Activity.java: 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 我使用了两个选项首先与@war_Hero相同

First 第一
/ Add validation to your edittext / / 在您的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();
    }

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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