【发布时间】:2011-01-10 04:12:33
【问题描述】:
目前,我有一个包含Button、TextView 和EditText 的布局。当布局显示时,焦点将自动放在EditText,这将触发键盘显示在Android手机上。这不是我想要的。有什么方法可以在显示布局时将焦点设置在 TextView 或什么都没有?
【问题讨论】:
目前,我有一个包含Button、TextView 和EditText 的布局。当布局显示时,焦点将自动放在EditText,这将触发键盘显示在Android手机上。这不是我想要的。有什么方法可以在显示布局时将焦点设置在 TextView 或什么都没有?
【问题讨论】:
你可以添加
android:importantForAccessibility="yes"
android:focusable="true"
android:focusableInTouchMode="true"
到您的布局以强制对讲/辅助功能先到那里。
您实际上只需要第一行,但是,其他行会向操作系统强化您想要关注的内容。
【讨论】:
Focus 用于在您使用除触摸之外的其他东西(即,d-pad、键盘等)时选择 UI 组件。任何视图都可以接收焦点,尽管有些视图默认情况下是不可聚焦的。 (您可以使用setFocusable(true) 使视图可聚焦,并使用requestFocus() 强制它聚焦。)
但是,请务必注意,当您处于触摸模式时,焦点会被禁用。因此,如果您使用手指,以编程方式更改焦点不会做任何事情。例外情况是从输入编辑器接收输入的视图。 EditText 就是这样一个例子。对于这种特殊情况,setFocusableInTouchMode(true) 用于让软键盘知道将输入发送到哪里。 EditText 默认具有此设置。软键盘会自动弹出。
如果您不希望软键盘自动弹出,那么您可以按照@abeljus 的说明暂时抑制它:
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
当用户点击EditText 时,它仍应显示键盘。
【讨论】:
改变焦点使xml中的textView成为焦点
<TextView
**android:focusable="true"**
android:id="@+id/tv_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
在java中创建
textView.requestFocus();
或者干脆隐藏键盘
public void hideKeyBoard(Activity act) {
act.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
InputMethodManager imm = (InputMethodManager) act.getSystemService(Context.INPUT_METHOD_SERVICE);
}
【讨论】:
您可以首先将android:windowSoftInputMode 添加到AndroidManifest.xml 文件中的活动中。
<activity android:name="YourActivity"
android:windowSoftInputMode="stateHidden" />
这将使键盘不显示,但EditText 仍然是焦点。为了解决这个问题,您可以在根视图上将android:focusableInTouchmode 和android:focusable 设置为true。
<LinearLayout android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
...
>
<EditText
...
/>
<TextView
...
/>
<Button
...
/>
</LinearLayout>
上面的代码将确保 RelativeLayout 获得焦点而不是 EditText
【讨论】:
将这些行也设置为 OnResume,并确保在初始化控件时 focusableInTouch 是否设置为 true
<controlName>.requestFocus();
<controlName>.requestFocusFromTouch();
【讨论】:
requestFocusFromTouch()。我在容器中添加了android:focusableInTouchMode="true" 和android:focusable="true"(API 24 中出现了问题)。
最后一个建议是正确的解决方案。重复一遍,首先在布局xml 文件中设置android:focusable="true",然后在代码中的视图上设置requestFocus()。
【讨论】:
您可以尝试隐藏键盘。像这样的:
InputMethodManager inputManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
【讨论】:
以上答案都不适合我。唯一(假设)的解决方案是更改 disabled EditText 中的第一个 TextView 接收焦点然后添加
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
在 onCreate 回调中防止显示键盘。现在我的第一个 EditText 看起来像一个 TextView 但最终可以得到初始焦点。
【讨论】:
要设置焦点,请使用 Handler 延迟 requestFocus()。
private Handler mHandler= new Handler();
public class HelloAndroid extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout mainVw = (LinearLayout) findViewById(R.id.main_layout);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
EditText edit = new EditText(this);
edit.setLayoutParams(params);
mainVw.addView(edit);
TextView titleTv = new TextView(this);
titleTv.setText("test");
titleTv.setLayoutParams(params);
mainVw.addView(titleTv);
mHandler.post(
new Runnable()
{
public void run()
{
titleTv.requestFocus();
}
}
);
}
}
【讨论】:
设置
android:focusable="true"
在你的<EditText/>
【讨论】:
这行得通:
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
【讨论】:
你应该添加这个:
android:focusableInTouchMode="true"
【讨论】:
您可以添加大小为“0 dip”的编辑文本作为您的 xml 中的第一个控件,因此,这将获得渲染的焦点。(确保它的焦点和所有...)
【讨论】:
我认为文本视图不可聚焦。例如,尝试将焦点设置在按钮上,或者将属性 focusable 设置为 true。
【讨论】:
试试
comp.requestFocusInWindow();
【讨论】:
设置焦点:框架将处理 移动焦点以响应用户 输入。强制专注于特定的 查看,拨打requestFocus()
【讨论】: