Android - Set Clicked focus on Button
I am having 4 buttons in my layout, now whenever i click any button, after the click event, button should be highlighed showing that it was clicked last.
For making such thing,开发者_高级运维 i have tried the following:
code:
btn1.setOnClickListener(new button_click_listener());
btn2.setOnClickListener(new button_click_listener());
class button_click_listener implements Button.OnClickListener
{
@Override
public void onClick(View v)
{
if(v==btn1)
{
btn1.requestFocus();
}
if(v==btn2)
{
btn2.requestFocus();
}
.......
}
}
XML Layout:
<Button
android:text="Click 1"
android:id="@+id/btnClick1"
android:layout_width="70dp"
android:layout_height="wrap_content"
style="@android:style/Widget.Button.Small"
android:padding="10dp"
android:focusableInTouchMode="true">
</Button>
How do i show clicked highlight on the button that was clicked last? Please, show me a way and give suggestions.
Update:
If i set android:focusable="true"
then button is highlighted and focused, but then at the same time, i need to click twice on the button to perform the actual click event.
its easy buddy...just you have to set backgroundcolor of ur button on ur click event and each time any button clicked u have to set other buttons background color null
This functionality resembles RadioButtonGroup, customize RadioButtonGroup to your requirements.
Setting Style in RadioGroup by :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_window_focused="false"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_window_focused="false"
android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="false" android:state_pressed="true"
android:drawable="@drawable/radio_active" />
<item android:state_checked="true" android:state_focused="true"
android:drawable="@drawable/radio_hover" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="@drawable/radio_normal_off" />
<item android:state_checked="false" android:drawable="@drawable/radio_normal" />
<item android:state_checked="true" android:drawable="@drawable/radio_hover" />
</selector>
Set this Selector to your Radio button.
The problem is that you need a new state. Follow the directions here and add a "state_highlighted", or whatever you'd like to call it:
How to add a custom button state
精彩评论