如何从 EditText OnClickListener 动态选择文本?

Sid*_*ath 1 android android-widget

我想在 OnClickListener 而不是 onlongclicklistener 上使用 android 选择文本功能。有没有办法做到这一点?有人可以帮我解决这个问题吗?谢谢

waq*_*lam 5

使用 xml:

android:selectAllOnFocus="true"
Run Code Online (Sandbox Code Playgroud)

使用代码(选项 1):

    yourEditText.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            //((EditText)v).selectAll();
            ((EditText)v).setSelection(startValue, stopValue);
        }
   });
Run Code Online (Sandbox Code Playgroud)

使用代码(选项 2):

yourEditText.setOnFocusChangedListener(new OnFocusChangedListener(){
    @Override
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus){
            //((EditText)v).selectAll();
            ((EditText)v).setSelection(startValue, stopValue);
        }
    }
});
Run Code Online (Sandbox Code Playgroud)