首页 > 解决方案 > 当 EditText 有焦点时选择所有文本不起作用

问题描述

当 EditText 有焦点时,我想默认选择里面的所有文本,但它不起作用:

editText.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View view, boolean hasFocus) {
                if (hasFocus) {
                  // Go to the end
            editText.setSelection(getEditTextView().getText().length());

                    // Select all the content
                    editText.selectAll();
                }
            }
        });

非常感谢你们!

标签: androidandroid-edittext

解决方案


有 2 种好方法可以在 EditText 中选择文本:

在你的 main.xml 里面:

android:selectAllOnFocus="true"

或者 :

editText.setSelectAllOnFocus(true); 

(如果你想以编程方式进行)

来源:当它获得焦点时选择 EditText 内的所有文本


推荐阅读