我只想在点击时设置按钮的图像。例如,如果我想点击按钮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;
}
}
答案 0 :(得分: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>
您可以根据需要更改状态
答案 1 :(得分: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)
答案 2 :(得分:0)
请参阅StateListDrawable及其在xml:<selector>
标记中的表示
答案 3 :(得分: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);
}
});`
答案 4 :(得分:0)
我认为您应该使用ImageView
作为按钮,而不是Harish的答案中指定的selector
。
使用ImageView
,您可以将src drawable设置为level-list
,然后您可以通过调用level
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++;
}
});
}
在您的情况下,您可以使用level-list创建多个ImageView。
答案 5 :(得分: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);
}
});