Custom Button handling state

I just want to make a button using a RelativeLayout, so I can have as many drawables or textfields inside, and I want all the children to change color or drawable accordlingly to the RelativeLayout state. If I press the custom button, it must have the correct colors and drawables.

Here is some code (only one TextView and an ImageView for the example):

In main.xml, the parent layout is a LinearLayout, here is the custom button:

<RelativeLayout
    android:id="@+id/relativelayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="10dip"
    android:layout_marginRight="10dip"
    android:layout_marginTop="25dp"
    android:background="@drawable/background_state"
    android:clickable="true"
    android:focusable="true" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="8dip"
        android:clickable="true"
        android:text="Awesome text"
        android:textColor="@color/textview_state"
        android:textSize="20sp" />

    <ImageView
        android:id="@+id/imageview"
        android:layout_width="51dip"
        android:layout_height="51dip"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="5dip"
        android:clickable="true"
        android:src="@drawable/imageview_state" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_alignParentTop="true"
        android:background="#FFFFFF"
        android:clickable="false"
        android:focusable="false" />

    <View
        android:layout_width="wrap_content"
        android:layout_height="1dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="50dip"
        android:background="#D3E992"
        android:clickable="false"
        android:focusable="false" />

</RelativeLayout>

In the drawable folder, imageview_state.xml:

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

In the drawable-hdpi folder, there is imageview.png and imageview_pressed.png.

In the color folder, background_state.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="#E0EBCC"/>
    <item android:drawable="#669900"/>

</selector>

In the color folder, textview_state.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true"
          android:color="#000"/>
    <item android:color="#FFF"/>
</selector>

So far, when I click on the RelativeLayout, the RelativeLayout's background changes correctly, but the ImageView and the TextView stay in default state. I tried setting the property android:clickable="true" for the TextView and the ImageView, but only the pressed element changes its state, not the whole button.

Any help to achieve my goal is welcome :)


You can set an OnClickListener on your relative layout, then just do something in your code when it is clicked. Like this pseudo-code.

RelativeLayout rl = (RelativeLayout) findViewById(R.id.myRelLayout);
rl.setOnClickListener(.....) {
    button1.setBackgroudDrawable(R.drawable.newColor).
    text1.setText("Something");
}

As suggested by Booger, I used a OnTouchListener and I basically do the change inside. It works perfectly, I would have preferred a xml solution, but whatever. Inside my Activity:

RelativeLayout relativeLayout= (RelativeLayout) findViewById(R.id.relativelayout);
relativeLayout.setOnTouchListener(...);

The implementation of the listener :

    public boolean onTouch(View v, MotionEvent event)
    {
        TextView text = (TextView) findViewById(R.id.textview);
        ImageView image = (ImageView) findViewById(R.id.imageview);
        RelativeLayout layout = (RelativeLayout) findViewById(R.id.relativelayout);
        if (event.getAction() == android.view.MotionEvent.ACTION_DOWN)
        {
            layout.setBackgroundResource(R.color.background_pressed);
            text.setPressed(true);
            image.setPressed(true);

        } else if (event.getAction() == android.view.MotionEvent.ACTION_UP)
        {
            layout.setBackgroundResource(R._background);
            text.setPressed(false);
            image.setPressed(false);
        }
        return false;
    }

I do everything programmatically, although the RelativeLayout's background changed its state properly using xml, because xml is faster than code, so a delay was visible. Now the RelativeLayout background property is set to:

 android:background="@color/background"

Not referring to a selector anymore.

链接地址: http://www.djcxy.com/p/84710.html

上一篇: 家长在同一时间?

下一篇: 自定义按钮处理状态