Android:选择 EditField 上焦点上的所有文本

发布于 2024-11-15 21:53:37 字数 419 浏览 1 评论 0 原文

我试图让 Android 在获得焦点时选择 EditText 字段中的所有文本。我在布局中使用此属性(在两个字段上):

android:selectAllOnFocus="true"

我不确定这是否相关,但为了将光标移至第一个可编辑字段(前面​​还有一个禁用字段),我使用以下命令:

quizStartNum.setFocusable(true);
quizStartNum.requestFocus();

但是,虽然首次显示布局时光标确实移动到所需字段,但文本不会突出显示;相反,光标最终位于文本的左侧,这是默认行为。如果我通过触摸移动到第二个字段,则会根据需要选择所有文本。然后,如果我移回第一个字段,再次触摸它,文本也会被完全选择。我希望从一开始就拥有这种行为。有办法做到这一点吗?

I'm trying to get Android to select all the text in an EditText field when it gets the focus. I'm using this attribute in the layout (on both fields):

android:selectAllOnFocus="true"

I'm not sure if this is relevant, but to get the cursor to the first editable field (there's also a disabled field before it), I'm using the following commands:

quizStartNum.setFocusable(true);
quizStartNum.requestFocus();

But, while the cursor does move to the desired field when the layout is first displayed, the text doesn't get highlighted; instead the cursor ends up to the left of the text, the default behavior. If I move to the second field by touching it, all the text is selected as desired. Then, if I move back to the first field, again by touching it, the text is also completely selected. I would like to have the behavior right from the start. Is there a way to do this?

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

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

发布评论

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

评论(6

妥活 2024-11-22 21:53:37

如果 android:selectAllOnFocus="true" 不起作用,请尝试在该特定 EditText 上调用 setSelectAllOnFocus(true)

如果这也不起作用,这是上一篇 SO 帖子中的另一个解决方法。

EditText dummy = ... 

dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus) && (isDummyText())
            ((EditText)v).selectAll();
    }
});

If android:selectAllOnFocus="true" does not work, try calling setSelectAllOnFocus(true) on that particular EditText.

If that doesn't work either, this is another workaround from a previous SO post.

EditText dummy = ... 

dummy.setOnFocusChangedListener(new OnFocusChangedListener(){
    public void onFocusChange(View v, boolean hasFocus){
        if (hasFocus) && (isDummyText())
            ((EditText)v).selectAll();
    }
});
葬花如无物 2024-11-22 21:53:37

我遇到了类似的问题,并且 android:selectAllOnFocus="true" 对我不起作用。
原因是我在显示 EditText 之前以编程方式请求焦点。因此,如果您要对 AlertDialog 中的 EditText 执行此操作,请确保在请求焦点到 EditText 之前显示它。

I had a similar issue and android:selectAllOnFocus="true" did not work for me.
The reason was that i was programatically requesting focus to the EditText before it was displayed. So if you are doing this for a EditText in an AlertDialog make sure you show it before you request focus to the EditText.

自此以后,行同陌路 2024-11-22 21:53:37

EditText 显示后应该获得焦点。

以下锻炼对我有用,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Set Input Methode
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

@Override
public void onStart() {
    super.onStart();

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            Dialog dialog = getDialog();
            if (dialog != null) {
            // Set Layout 
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // Set Cancelable False
            setCancelable(false);

            // Set Focus Here <------------------------
            uiET_myTextView.requestFocus();
            }            
        }
    });
}

EditText should be focussed after it displayed.

Following workout worked for me,

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    // Set Input Methode
    getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
}

@Override
public void onStart() {
    super.onStart();

    new Handler().post(new Runnable() {
        @Override
        public void run() {
            Dialog dialog = getDialog();
            if (dialog != null) {
            // Set Layout 
            dialog.getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            // Set Cancelable False
            setCancelable(false);

            // Set Focus Here <------------------------
            uiET_myTextView.requestFocus();
            }            
        }
    });
}
夜无邪 2024-11-22 21:53:37

尝试删除焦点命令。它们不是必需的,Android 应该自动关注第一个字段?

Try removing your focus commands. They aren't necessary, Android should focus on the first field automatically?

澜川若宁 2024-11-22 21:53:37

我还注意到 android:selectAllOnFocus="true" 似乎不起作用,我用鼠标在模拟器中选择 EditText,但如果我用手指触摸它,就像在手机上一样,它就起作用了。如果您的计算机上没有触摸屏,您可能需要在物理设备上安装应用程序才能进行测试。
在这种情况下 Android Studio 可能无法识别鼠标

I also noticed that with android:selectAllOnFocus="true" seemed not to work, I used the mouse to select EditText in the emulator, but if I used my finger and touched it, as if on the phone, it worked. If you don't have a touch screen on your computer you may have to install your app on a physical device to test it.
Android Studio may not recognize the mouse in this case

动听の歌 2024-11-22 21:53:37

我将参考这篇旧文章

如果您只想让 EditText 显示提示(“此处内容”),您可能需要使用 Android 的 XML 属性 hint (链接)。

I'll refer to this older post.

If you just want your EditText to show a hint (for "what goes in here"), you might want to use the Android's XML-attribute hint (link).

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