2011-03-05 96 views
1

我宣布一个Android的按钮是这样的:为Android按钮设置状态

<Button 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:background="@drawable/festival_button_selector" 
android:textColor="@color/white" 
android:text="@string/STR_FEST_SELOFF" 
android:onClick="onBtnFestSelectionClick" 
/> 

这里是这个按钮的选择背景(festival_button_selector)的内容:

<item android:state_focused="true" 
    android:drawable="@drawable/festival_button_focus" 
    /> 
<item android:state_pressed="true" 
    android:drawable="@drawable/festival_button_press" 
    /> 
<item android:state_focused="false" 
    android:state_pressed="false" 
    android:drawable="@drawable/festival_button" 
    /> 

其实我想设置这个按钮背景是在“焦点”状态下(所以按钮背景将是festival_button_focus)作为默认值。如果我们点击其他按钮,此按钮背景将返回到“正常”状态(背景为festival_button)。我们能做到吗?

P/S:我不想更改festival_button_selector。如果我们有一个像button.setFocusState(true)这样的函数使这个按钮处于焦点状态,这将是理想的。

回答

2

看看requestFocus()设置初始焦点状态。

+0

requestFocus()方法看起来正确,但遗憾的是这种方法是在越野车的Android 2.1和2.2:http://stackoverflow.com/questions/4211851/requestfocus-problem。无论如何,谢谢你的回应。 – anticafe 2011-03-05 14:45:43

+0

看着你的链接看起来像解决方案是使用runnable来设置焦点。 – androidworkz 2011-03-05 15:43:08

+0

新主题(新的Runnable(){ 公共无效的run(){ \t((按钮)findViewById(R.id.btnFestivalSelection))requestFocus()方法。 } }); // - >它不适合我。 – anticafe 2011-03-06 02:48:24

0
<item android:state_focused="true" 
    android:drawable="@drawable/festival_button_focus" 
    /> 
<item android:state_pressed="true" 
    android:drawable="@drawable/festival_button_press" 
    /> 
<item 
    android:drawable="@drawable/festival_button" 
    /> 

试试这个