Android TextWatcher获取整个键入的单词

时间:2016-04-05 06:02:54

标签: android android-edittext textwatcher

在我的Android应用程序中,我使用了TextWatcher,它工作正常,但我需要获得整个键入的单词而不是单个字母,因为我将类型值保存到arraylist中。注意:我正在动态创建edittext,因此用户可以更新已输入的值,因此我只需要使用TextWatcher。

我的尝试在下面

    public class CustomWatch implements TextWatcher {
                EditText _dynamicEd;
                Model model;
                boolean isUpdateEd = false;
                StringBuffer previousChar;
                String typedValue;
                boolean flag;

                public CustomWatch(EditText dynamic_editxt) {
                    this._dynamicEd = dynamic_editxt;
                    model = new Model();
                    previousChar = new StringBuffer();
                    if (!TextUtils.isEmpty(_dynamicEd.getText().toString())) {
                        isUpdateEd = true;
                    }
                }

                @Override
                public void beforeTextChanged(CharSequence s, int start, int count, int after) {

                }

                @Override
                public void onTextChanged(CharSequence s, int start, int before, int count) {
                }

                @Override
                public void afterTextChanged(final Editable s) {                                        flag = false;
                    typedValue = s.toString();
                    new Handler().postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            if(!flag) {
                                Log.d("Timer", "value-->" + s.toString());
 model.setName(s.toString());
                Controllers.getInstance().modellist.add(model);
                Controllers.getInstance().getPreferenceManager(MainActivity.this).saveProductList("CHECK_LIST", Controllers.getInstance().modellist);
                c++;
                                flag = true;
                            }else{

                            }
                        }
                    },2000);

                }
            }

2 个答案:

答案 0 :(得分:0)

dynamic_editxt.getText()

内使用afterTextChanged

答案 1 :(得分:0)

它可能有用,

@Override
  public void afterTextChanged(final Editable s) {
    if(s.contains("\\s")){
     String[] words = s.toString().split("\\s+");
     //Code to add words to the list
   }
 }

更新:

根据您的要求,您可以通过获取EditText的焦点来添加arraylist中的整个句子

 myEditText.setOnFocusChangeListener(new OnFocusChangeListener() {

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
      if (!hasFocus) {
       //Here user finished the typing, Now save userTypedValue as required
      String userTypedValue = myEditText.getText().toString();
      }
    }
 });