如何在android中当前焦点编辑文本?

发布于 2024-12-29 18:22:34 字数 112 浏览 3 评论 0原文

您好,我想要编辑当前焦点位于按钮单击的文本,有任何按钮可以执行此操作。

或者,如果活动中有许多编辑文本,则有一种方法可以识别最后修改的编辑文本。

请向我推荐可用的链接或示例代码。

Hi i want to get edit text which is currently focus at button click , there is any button to doing it .

Or there is a way to identify which edittext is last modified if there are many edit text in activity.

Please suggest me usable link or sample code.

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

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

发布评论

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

评论(2

相对绾红妆 2025-01-05 18:22:34

我采用线性布局并向其中添加自定义 EditText。

    LayoutInflater li = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    for(int i = 0; i < 3; i++) {
        View v = li.inflate(R.layout.addit, null);
        final EditText e = (EditText) v.findViewById(R.id.e1);
        e.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                // TODO Auto-generated method stub

                System.out.println("--change--");
                mEditText = e;
                System.out.println(e.getText().toString());
            }

        });
        edit_ll.addView(v);
    }

这里mEditText是一个全局变量。

您可以在活动中的任何位置访问它。并且可以获得你最后写入的editText是什么。

I take a linear layout and add custom EditText into it.

    LayoutInflater li = (LayoutInflater) this.getSystemService(LAYOUT_INFLATER_SERVICE);
    for(int i = 0; i < 3; i++) {
        View v = li.inflate(R.layout.addit, null);
        final EditText e = (EditText) v.findViewById(R.id.e1);
        e.addTextChangedListener(new TextWatcher() {

            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub

            }

            public void beforeTextChanged(CharSequence s, int start,
                    int count, int after) {
                // TODO Auto-generated method stub

            }

            public void onTextChanged(CharSequence s, int start,
                    int before, int count) {
                // TODO Auto-generated method stub

                System.out.println("--change--");
                mEditText = e;
                System.out.println(e.getText().toString());
            }

        });
        edit_ll.addView(v);
    }

Here mEditText is a global variable.

Anywhere in your activity you can access it. And can get what's the last editText you have written into.

伪装你 2025-01-05 18:22:34

您可以向所有 EditText 添加一个 textWatcher:

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

    public CustomTextWatcher(EditText e) {
        mEditText = e;
    }

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

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

    public void afterTextChanged(Editable s) {
    }
}

并且可以在您的类中添加一个全局变量来包含正在编辑的 editText 引用。

you can add a textWatcher to all EditTexts:

private class CustomTextWatcher implements TextWatcher {
    private EditText mEditText;

    public CustomTextWatcher(EditText e) {
        mEditText = e;
    }

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

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

    public void afterTextChanged(Editable s) {
    }
}

and can have a global variable to your class to contain editText reference, which is being edited.

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