开发者

Android java ImageButton method help

开发者 https://www.devze.com 2023-03-17 15:51 出处:网络
So I\'m figuring this stuff out in baby steps. And just got my button to toggle the way i want it. But now i want to add more buttons.

So I'm figuring this stuff out in baby steps. And just got my button to toggle the way i want it. But now i want to add more buttons.

       `public class Menu extends Activity{
        ImageButton select;    
        int isClicked = 0;

        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate开发者_运维百科(savedInstanceState);
                setContentView(R.layout.main);

                select = (ImageButton)findViewById(R.id.select);
        select.setOnClickListener(new OnClickListener() {

                public void onClick(View v) {
                     if (isClicked == 0){
                        select.setImageResource(R.drawable.select_pressed);
                        isClicked = 1;
                     }
                     else{
                        select.setImageResource(R.drawable.select);
                        isClicked = 0;
                     }
                }});
             }
          }`

So say i were to copy that ImageButton method. Where exactly would i insert it, if i were going to use the code for a new button?

    `<ImageButton 
android:src="@drawable/select"
android:id="@+id/select"
android:layout_height="30dp"
android:layout_width="120dp"
android:background="@null"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true">
    </ImageButton>`


You can use the tag attribute on the button to store the state. Then you could put your state logic in a separate method like this:

public void changeState(View v) {
                     if (v.getTag() == "false"){
                        ((ImageButton)v).setImageResource(R.drawable.select_pressed);
                        v.setTag("true");
                     }
                     else{
                        ((ImageButton)v).setImageResource(R.drawable.select);
                        v.setTag("false");
                     }
                }});

This method you could call from each ImageButton's clicklistener, without saving the state of numerous ImageButtons in the application context.


You can easily create image buttons using the graphical layout editor in you main.xml file. Create a new image button object for your new buttons and specify the on click listeners in the onCreate() function as you have done.


Instead of your approach, Android has something called selectors that can change the background resource automatically depending on the state of the view. Selectors are defined in a separate drawable xml file and referenced from the xml declaration of the view that utilizes the states. For example: Selector (android_button.xml):

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

Button:

<Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:padding="10dp"
        android:background="@drawable/android_button" />

You will still have to implement a clicklistener for each button, but you will have a selector that you can reuse in all the buttons you want, and the presentation logic has been separated from the business logic of your app.

The source code shown in this answer comes straight from http://developer.android.com/resources/tutorials/views/hello-formstuff.html#CustomButton where you can read a more complete explanation.

0

精彩评论

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

关注公众号