2010-07-13 31 views

回答

156

是的,这是可能的。

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

或XML

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

重要提示: Button控件必须focusablefocusableInTouchMode。大多数小部件默认为focusable,但不是focusableInTouchMode。因此,请务必将它设置在代码

myBtn.setFocusableInTouchMode(true); 

或XML

android:focusableInTouchMode="true" 
+0

我想它在onCreate方法,但它不工作。我正在动态创建按钮并在LinearLayout中添加按钮。任何人都可以帮助我吗? – 2011-03-16 09:44:40

+5

它不适合我。我在我的页面中有一个EditView,始终是焦点。 – breceivemail 2011-10-23 12:29:17

+10

很遗憾,看到这个答案得到了一个荒唐的upvotes数时,虽然名义上“正确”,但它忽略了提前通常需要调用Button的setFocusable(true)方法和setFocusableInTouchMode(true)方法它会工作。虽然这些可能是从xml设置的,但如果按钮被设置为不可调焦,则可能必须重置它们。 – 2012-07-15 20:38:34

相关问题