android开发分享了解编辑文本何时完成编辑

我如何知道编辑文本何时被编辑? 就像用户select下一个方框,或者按下软键盘上的完成button。

我想知道这个,所以我可以钳制input。 它看起来像文本观察者的afterTextChanged发生在每个字符input后。 我需要对input做一些计算,所以我想避免在input每个字符后进行计算。

谢谢

    通过使用这样的东西

    meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE) { yourcalc(); return true; } return false; } }); 

    EditTextinheritancesetOnFocusChangeListener ,它接受setOnFocusChangeListener的实现。

    实现onFocusChangehasFocus有一个布尔参数。 当这是错误的,你已经失去了焦点到另一个控制。

    编辑

    要处理这两种情况 – 编辑丢失焦点的文本或者用户单击“完成”button – 创build一个从两个侦听器中调用的方法。

      private void calculate() { ... } btnDone.setOnClickListener(new OnClickListener() { public void onClick(View v) { calculate(); } }); txtEdit.setOnFocusChangeListener(new OnFocusChangeListener() { public void onFocusChange(View v, boolean hasFocus) { if(!hasFocus) calculate(); } }); 

    使用像这样定义的EditText对象xml:

      <EditText android:id="@+id/create_survey_newquestion_editText_minvalue" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_centerVertical="true" android:ems="4" android:imeOptions="actionDone" android:inputType="number" /> 

    当用户单击软键盘上的“完成”button(OnEditorActionListener)时,或者ii)当EditText丢失了现在位于另一个EditText上的用户焦点(OnFocusChangeListener)时,我们可以捕获它的文本i)

      /** * 3. Set the min value EditText listener */ editText= (EditText) this.viewGroup.findViewById(R.id.create_survey_newquestion_editText_minvalue); editText.setOnEditorActionListener(new OnEditorActionListener() { @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { String input; if(actionId == EditorInfo.IME_ACTION_DONE) { input= v.getText().toString(); MyActivity.calculate(input); return true; // consume. } return false; // pass on to other listeners. } }); editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { String input; EditText editText; if(!hasFocus) { editText= (EditText) v; input= editText.getText().toString(); MyActivity.calculate(input); } } }); 

    这对我有用。 您可以使用如下代码进行计算后隐藏软键盘:

     private void hideKeyboard(EditText editText) { InputMethodManager imm= (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(editText.getWindowToken(), 0); } 

    编辑:添加返回值到onEditorAction

    我做了这个简单的事情,当焦点从编辑文本转移到文本。 这将工作,如果用户转移焦点select其他视图,如button或其他EditText或任何视图。

      editText.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if (!hasFocus) { EditText editText = (EditText) v; String text = editText.getText().toString(); } } }); 

    为了更高级别,你可以使用TextWatcher的afterTextChanged()方法。

      以上就是android开发分享了解编辑文本何时完成编辑相关内容,想了解更多android开发(异常处理)及android游戏开发关注计算机技术网(www.ctvol.com)!)。

      本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。

      ctvol管理联系方式QQ:251552304

      本文章地址:https://www.ctvol.com/addevelopment/515601.html

      (0)
      上一篇 2020年12月1日
      下一篇 2020年12月1日

      精彩推荐