Can't manage to requestFocus a Spinner

发布于 2022-09-06 08:44:07 字数 2380 浏览 14 评论 0

I've got an annoying issue with a screen. The screen consists of a bunch of Spinners one under the other, and then underneath the spinner, an EditText.

The problem is that when the screen starts, the EditText has focus, meaning that some Spinners are off the top of the screen. Try as I might, I cannot make the top Spinner start focused, either by using <requestFocus/> in the screen XML, or by using requestFocus() in code. I've attempted to do what requestFocus skipping next EditText suggests, and if I'm following the suggestion correctly, it doesn't work either.

To reproduce the issue, create a new Android project in Eclipse. main.xml is

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <Spinner 
        android:id="@+id/spinner"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content">   
        <requestFocus />
    </Spinner>
    <EditText  
        android:id="@+id/edittext"
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

The activity code is

package nz.co.kb.testspinner;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;

public class TestSpinner extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.main);
        View view = getLayoutInflater().inflate(R.layout.main, null);
        final View spinner = view.findViewById(R.id.spinner);
        view.post(new Runnable() {
            public void run() {
                spinner.requestFocus();
            }
        });
        setContentView(view);
        spinner.requestFocus();
    }
}

Note multiple styles of requestFocus attempted.

Is this a platform bug, or am I doing something wrong?

Thanks.

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

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

发布评论

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

评论(3

○愚か者の日 2022-09-13 08:44:07

I had a similar problem, I solved by doing two things:

1) I set the Spinner object on top (Within the onCreate method) just to make sure that my code gets executed first.
2) I used the following:

Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        s1.setFocusable(true);
        s1.setFocusableInTouchMode(true);

Let me know if this helps or you need any further help.

简单爱 2022-09-13 08:44:07

From online documentation:

A view will not actually take focus if it is not focusable (isFocusable() returns false), or if it is focusable and it is not focusable in touch mode (isFocusableInTouchMode()) while the device is in touch mode.

韬韬不绝 2022-09-13 08:44:07

In my case,this worked out.

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