0

我只想设置按钮的图像以在单击时更改。例如,如果我想点击按钮 A 它会改变。如果我单击按钮 B,它的图像将改变,按钮 A 的图像将恢复。我怎样才能做到这一点?我非常需要这方面的帮助。

package com.example.mobile_nurse;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.ImageButton;

public class Posture_1 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_posture_1);
        Back();
        _1minute();
        _15minute();
    }
    private void _15minute() {
        ImageButton Button1 = (ImageButton) findViewById(R.id.imageButton3);
        ImageButton Button4 = (ImageButton) findViewById(R.id.imageButton4);
        View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Button1.setBackgroundResource(R.drawable.posms1_button1_null);
            Button4.setBackgroundResource(R.drawable.posms1_button_a);
            }
        }; 
        Button1.setOnClickListener(myListener);
    }
    private void _1minute() {
        ImageButton Button1 = (ImageButton) findViewById(R.id.imageButton3);
        ImageButton Button4 = (ImageButton) findViewById(R.id.imageButton4);
        View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            Button1.setBackgroundResource(R.drawable.posms1_button1);
            Button4.setBackgroundResource(R.drawable.posms1_button_a_null);
            }
        }; 
        Button1.setOnClickListener(myListener);
    }
    private void Back() {
        ImageButton Button = (ImageButton) findViewById(R.id.imageButton1);
        View.OnClickListener myListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        }; 
        Button.setOnClickListener(myListener);
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.posture_1, menu);
        return true;
    }

}
4

6 回答 6

2

尝试选择器:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/iconSelector">
<!-- pressed -->
<item android:state_pressed="true" android:drawable="@drawable/btn_icon_hl" />
<!-- focused -->
<item android:state_focused="true" android:drawable="@drawable/btn_icon_hl" />
<!-- default -->
<item android:drawable="@drawable/btn_icon" />
</selector>

您可以根据需要更改状态

于 2013-08-08T07:28:25.583 回答
0

我认为你应该使用ImageViewas button 而不是selectorHarish 的答案中指定的。

您可以将ImageViewsrc drawable 设置为 a level-list,然后您可以level通过调用来控制 (drawable)setImageLevel(int)

将以下内容保存在res/drawable/level.xml

<?xml version="1.0" encoding="utf-8"?>
<level-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
       android:maxLevel="0">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="2dp" android:color="@android:color/black" />
            <solid android:color="#333" />
            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <item
        android:maxLevel="1">
        <shape xmlns:android="http://schemas.android.com/apk/res/android">
            <stroke android:width="2dp" android:color="@android:color/black" />
            <solid android:color="#FF6699" />
            <padding android:bottom="2dp" android:left="5dp" android:right="5dp" android:top="2dp" />
            <corners android:radius="5dp" />
        </shape>
    </item>
</level-list>

那么你的activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <ImageView
        android:id="@+id/level_image"
        android:layout_width="80dp"
        android:layout_height="40dp"
        android:src="@drawable/level" />
</RelativeLayout>

那么你的onCreate()方法

int count = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    final ImageView img = (ImageView) findViewById(R.id.level_image);
    img.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            if (count%2==0) img.setImageLevel(1);
            else img.setImageLevel(0);
            count++;
        }

    });
}

在您的情况下,您可以使用级别列表创建多个 ImageView。

于 2013-08-13T07:31:28.220 回答
0
setBackgroundResource updates background not the Image 

所以用setImageDrawable(getResources().getDrawable(R.drawable.your_Image))

里面_15minute() change to

Button1.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button1_null)
Button4.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button_a)

和里面_1minute() change to

Button1.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button1)
Button4.setImageDrawable(getResources().getDrawable(R.drawable.posms1_button_a_null)
于 2013-08-08T07:28:45.913 回答
0

像这样尝试它只是示例代码

`

    final Button b1 = (Button) findViewById(R.id.button1);
    final Button b2 = (Button) findViewById(R.id.button2);
    b1.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            b1.setBackgroundResource(R.drawable.imagesa);
            b2.setBackgroundColor(Color.TRANSPARENT);
        }
    });
    b2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            b2.setBackgroundResource(R.drawable.imagesa);
            b1.setBackgroundColor(Color.TRANSPARENT);
        }
    });`
于 2013-08-08T07:54:38.237 回答
0

试试这个代码:

Button1.setOnClickListener(new OnClickListener(){
    public void onClick(View view){
      //enter your code

      //remove background
      Button4.setBackground(null); //or
      Button4.setBackgroundColor(Color.WHITE);

      Button1.setBackgroundResource(R.drawable.posms1_button1);

    }
});
于 2014-02-14T09:04:51.757 回答
0

请参阅 StateListDrawable 及其在 xml:<selector>标记中的表示

于 2013-08-08T07:32:38.060 回答