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

时间:2015-07-06 02:49:19

标签: android android-edittext ime

我有一个简单的UI屏幕供用户在EditText行中输入数据,然后在第二个EditText行中输入日期。我有一个ListenerEditText.java文件,用于监听后退按钮。问题是,当用户在第一个EditText行上输入数据,然后焦点移动到第二个EditText行时,代码不再正确地按下后退按钮,因为它无法识别在第一个EditText行上输入的数据 - 它出现仅检查第二个EditText行上是否有数据。当按下后退按钮时,我希望代码识别第一个EditText行和/或第二个EditText行上的数据,然后启动DialogFragment以确认用户是否将丢失输入的数据。目前,该代码没有"看到"第一个EditText行上的数据,如果第二个EditText行没有数据,则用户被错误地带回到上一个屏幕。

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

...

2 个答案:

答案 0 :(得分:0)

你可以做这样的事情,

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

并在第一个编辑文本上执行验证,设置错误和其他内容。

答案 1 :(得分:0)

我使用的两个选项First与@war_Hero告诉

相同

首先
/ 为您的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();
    }

}