无法请求聚焦微调器

发布于 2024-10-16 07:23:13 字数 1951 浏览 3 评论 0原文

我遇到了一个恼人的屏幕问题。屏幕由一堆旋转器组成,一个在另一个下,然后在旋转器下面是一个 EditText。

问题是,当屏幕启动时,EditText 具有焦点,这意味着某些 Spinner 不在屏幕顶部。尽我所能,我无法通过在屏幕 XML 中使用 或在代码中使用 requestFocus() 使顶部 Spinner 开始聚焦。我尝试按照 requestFocus 跳过下一个 EditText 的建议进行操作,如果我正确遵循了建议,那么它也不行。

要重现该问题,请在 Eclipse 中创建一个新的 Android 项目。 main.xml 是

<?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>

活动代码,

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();
    }
}

请注意尝试了多种 requestFocus 样式。

这是平台错误还是我做错了什么?

谢谢。

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

一场春暖 2024-10-23 07:23:13

我遇到了类似的问题,我通过做两件事解决了:

1)我将 Spinner 对象设置在顶部(在 onCreate 方法中)只是为了确保我的代码首先执行。
2)我使用了以下内容:

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

如果这有帮助或者您需要任何进一步的帮助,请告诉我。

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.

海的爱人是光 2024-10-23 07:23:13

来自在线文档

如果视图不可聚焦(isFocusable() 返回 false),或者在设备处于触摸模式时可聚焦但在触摸模式下不可聚焦(isFocusableInTouchMode()),则视图实际上不会获得焦点。< /p>

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.

无边思念无边月 2024-10-23 07:23:13

就我而言,这成功了。

Spinner s1 = (Spinner) findViewById(R.id.spinner1);
s1.requestFocusFromTouch();
s1.performClick();

In my case,this worked out.

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