无法请求一个旋转器的焦点

[英]Can't manage to requestFocus a Spinner


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.

我对屏幕有个烦人的问题。屏幕由一堆旋转器组成,一个在另一个下面,然后在旋转器下面是一个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.

问题是,当屏幕开始时,EditText有焦点,这意味着一些旋转器不在屏幕顶部。尽管如此,我还是不能通过在屏幕XML中使用 或者在代码中使用requestFocus()来集中顶级微调器start。我已经尝试过做下一个EditText建议的requestFocus跳过,如果我正确地遵循了这个建议,它也不起作用。

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

要重现这个问题,请在Eclipse中创建一个新的Android项目。主要。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>

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.

注意尝试了多种类型的requestFocus。

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

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

Thanks.

谢谢。

2 个解决方案

#1


6  

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.

如果视图不是可调焦的(isfocapplicable()返回false),或者如果视图是可调焦的,并且在触摸模式(isFocusableInTouchMode())中不能调焦,那么视图实际上就不会被调焦。

#2


18  

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:

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

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.

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

智能推荐

注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:http://www.itdaan.com/blog/2011/02/07/13d16d4ada3a23a954df6edb0cdf2f7a.html



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告