了解编辑文本何时完成编辑

发布于 2024-10-19 02:48:33 字数 159 浏览 3 评论 0原文

我如何知道我的编辑文本何时完成编辑?就像用户选择下一个框或按软键盘上的完成按钮时一样。

我想知道这一点,以便我可以限制输入。看起来文本观察器的 afterTextChanged 发生在输入每个字符之后。我需要对输入进行一些计算,因此我想避免在输入每个字符后进行计算。

谢谢

How do I know when my edit text is done being edited? Like when the user selects the next box, or presses the done button on the soft keyboard.

I want to know this so I can clamp the input. It looks like text watcher's afterTextChanged happens after each character is entered. I need to do some calculations with the input, so I would like to avoid doing the calculation after each character is entered.

thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

溺深海 2024-10-26 02:48:33

通过使用这样的东西

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

By using something like this

 meditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
        @Override
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            switch (actionId){
                case EditorInfo.IME_ACTION_DONE:
                case EditorInfo.IME_ACTION_NEXT:
                case EditorInfo.IME_ACTION_PREVIOUS:
                    yourcalc();
                    return true;
            }
            return false;
        }
    });
请止步禁区 2024-10-26 02:48:33

EditText 继承 setOnFocusChangeListener,它采用 OnFocusChangeListener 的实现。

实现onFocusChange并且有一个hasFocus的布尔参数。如果这是错误的,您就会失去对另一个控件的关注。

编辑

要处理这两种情况 - 编辑文本失去焦点或用户单击“完成”按钮 - 创建一个从两个侦听器调用的方法。

    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 inherits setOnFocusChangeListener which takes an implementation of OnFocusChangeListener.

Implement onFocusChange and there's a boolean parameter for hasFocus. When this is false, you've lost focus to another control.

EDIT

To handle both cases - edit text losing focus OR user clicks "done" button - create a single method that gets called from both listeners.

    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();
        }
    });
别想她 2024-10-26 02:48:33

使用这样定义的 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" />

我们可以捕获其文本 i) 当用户单击软键盘上的“完成”按钮 (OnEditorActionListener) 或 ii) 当 EditText 失去用户焦点 (OnFocusChangeListener)(现在位于另一个 EditText 上) :

    /**
     * 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添加返回值

Using an EditText object xml defined like this:

    <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" />

We can capture its text i) when the user clicks the Done button on the soft keyboard (OnEditorActionListener) or ii) when the EditText has lost the user focus (OnFocusChangeListener) which now is on another EditText:

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

This works for me. You can hide the soft keyboard after made the calculations using a code like this:

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

Edit: added return values to onEditorAction

横笛休吹塞上声 2024-10-26 02:48:33

我做了这个简单的事情,当焦点从编辑文本转移时获取文本。如果用户将焦点转移到选择其他视图(例如按钮或其他 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();
             }
        }
    });

I have done this simple thing, when focus is shifted from edit text get the text. This will work if user shifts the focus to select other views like a button or other EditText or any view.

        editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if (!hasFocus) {
                EditText editText = (EditText) v;
                String text = editText.getText().toString();
             }
        }
    });
丿*梦醉红颜 2024-10-26 02:48:33

要更高级,您可以使用 TextWatcher 的 afterTextChanged () 方法。

To be more highlevel you may use TextWatcher's afterTextChanged() method.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文