2010-07-13 127 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

相關問題