开发者

Button draws normal state after pressing

开发者 https://www.devze.com 2022-12-25 06:36 出处:网络
I\'m new to Android and just starting the very basics. I implement my custom button skin using .9.png images for norma/focus/pressed states. It works fine, but I noticed that after a pressed the focus

I'm new to Android and just starting the very basics. I implement my custom button skin using .9.png images for norma/focus/pressed states. It works fine, but I noticed that after a pressed the focussed button it visually "lost" focus and draws the normal state 开发者_StackOverflow社区frame. I planned to use different state images to highloght what button is selected right now, but it seems that it would not work. I noticed also that the same happens with the default LAF button. Is it OK, or it's just emulator issue? What the good workaroud can be used?

Thanks


I think the following may help. I wanted to have one of the buttons in a list of button to be coloured differently, to highlight the fact you were already in that section.

My buttons android:background field was set to the following drawable (drawable/my_btn.xml)

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" android:drawable="@drawable/btn_pressed" />
    <item android:state_focused="true" android:drawable="@drawable/btn_focused" />

    <item android:state_selected="true" android:drawable="@drawable/btn_selected" />

    <item android:drawable="@color/transparent" />
</selector>

You'll noticed i've got an item with the android:state_selected="true" attribute set.

Then in code you can have

Button mybtn = (Button)findViewById(R.id.my_btn_1);
mybtn.setOnClickListener(new OnClickListener(){
    public void onClick(View v) {   
        Button btn = (Button)findViewById(R.id.nav_secondary_1);
        btn.setSelected(true);
    }
});

I'm not sure if you can set the selected stat of a Button through a property in the xml. Not sure you would want to.

The order of the item's are also important as it can change the visibility of the other states. The current order will allow you to see the pressed and focused states. however, if you moved the selected item to the top you would find that your pressed and focused states would not be displayed.

I am not sure if you can combine the pressed, focused and selected states to allow for more customised graphics. I haven't tried it but the following would allow for more complicated state based graphical layouts.

<item android:state_selected="true" android:state_focused="true" android:drawable="@drawable/btn_selected_focused" />

Read up on Selectors here http://developer.android.com/guide/topics/resources/drawable-resource.html


This is the default behavior in touch mode, and you should not seek to tamper with it. This is how your users will expect for your app to behave. If you set the focus without touching the screen, such as when using the trackball that's available on most devices, it will indeed remain in focus, but in touch mode there's no visual representation for the state of having focus.

0

精彩评论

暂无评论...
验证码 换一张
取 消