Android的 - 将焦点设置到一个按钮插件的活动编程的onCreate插件、按钮、焦点、Android

2023-09-05 06:40:39 作者:北辰

是否可以设置一个焦点的一个按钮控件,它躺在我的布局的地方。活动的的onCreate 我的控制/重点应该放在该按钮编程。

Is it possible to set a focus to a button widget which lies somewhere down in my layout. onCreate of the activity my control/focus should be on that button programmatically.

推荐答案

是的这是可能的。

Button myBtn = (Button)findViewById(R.id.myButtonId);
myBtn.requestFocus();

或XML

<Button ...><requestFocus /></Button>

重要提示: Button控件必须是可聚焦的和的 focusableInTouchMode 。大多数部件是可聚焦而不是 focusableInTouchMode 默认情况下。因此,请务必将它设置在code

Important Note: The button widget needs to be focusable and focusableInTouchMode. Most widgets are focusable but not focusableInTouchMode by default. So make sure to either set it in code

myBtn.setFocusableInTouchMode(true);

或XML

android:focusableInTouchMode="true"