Can#39;t manage to requestFocus a Spinner(无法请求Focus a Spinner)
问题描述
我遇到了一个恼人的屏幕问题.屏幕由一堆 Spinner 组成,一个接一个,然后在 Spinner 下方,一个 EditText.
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 有焦点,这意味着一些 Spinner 不在屏幕顶部.尽我所能,我无法通过在屏幕 XML 中使用 <requestFocus/>
或在代码中使用 requestFocus()
使顶部 Spinner 开始聚焦.我试图做 requestFocus skipping next 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.
要重现该问题,请在 Eclipse 中创建一个新的 Android 项目.main.xml 是
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>
活动代码是
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.
Note multiple styles of requestFocus attempted.
这是一个平台错误,还是我做错了什么?
Is this a platform bug, or am I doing something wrong?
谢谢.
推荐答案
来自网上文档:
如果视图不可聚焦(isFocusable() 返回 false),或者如果它可聚焦但在设备处于触摸模式时在触摸模式下不可聚焦(isFocusableInTouchMode()),则视图实际上不会获得焦点.
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.
这篇关于无法请求Focus a Spinner的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法请求Focus a Spinner
基础教程推荐
- Android:对话框关闭而不调用关闭 2022-01-01
- 在 gmail 中为 ios 应用程序检索朋友的朋友 2022-01-01
- Kivy Buildozer 无法构建 apk,命令失败:./distribute.sh -m “kivy"d 2022-01-01
- 如何在没有IB的情况下将2个按钮添加到右侧的UINavigationbar? 2022-01-01
- 如何在 iPhone 上显示来自 API 的 HTML 文本? 2022-01-01
- 如何让对象对 Cocos2D 中的触摸做出反应? 2022-01-01
- 当从同一个组件调用时,两个 IBAction 触发的顺序是什么? 2022-01-01
- UIWebView 委托方法 shouldStartLoadWithRequest:在 WKWebView 中等效? 2022-01-01
- 如何在 UIImageView 中异步加载图像? 2022-01-01
- android 应用程序已发布,但在 google play 中找不到 2022-01-01