我想写一个图片按钮,继承ImageButton,传入down和up状态的图片ID,在OnTouch的时候更换图片。类是写出来了,但是如果new两个以上的按钮的时候,无论点击哪个按钮,都只会是最后一个按钮响应,请大侠指点package com.hahalotto.common.views;import android.content.Context;
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;public class GameImageButtonView extends ImageButton{
private int buttonUpId;
private int buttonDownId;
public GameImageButtonView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
public void setButtonStateIds(int upId, int downId)
{
System.out.println(getId());
buttonUpId = upId;
buttonDownId = downId;
setImageResource(upId);
setBackgroundColor(0x00000000);
setOnTouchListener(new ImgBtnTouchListener()); }
class ImgBtnTouchListener implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println(getId());
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
System.out.println("down");
setImageResource(buttonDownId);
}
else
{
System.out.println("up");
setImageResource(buttonUpId);
}
return false;
}
}
}
调用的方式是private void initBtns() {
type5Btn = new GameImageButtonView(this);
type10Btn = new GameImageButtonView(this);
type20Btn = new GameImageButtonView(this);
type5Btn.setId(1000);
type10Btn.setId(2000);
type20Btn.setId(3000);
addContentView(type5Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type10Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type20Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
type5Btn.setPadding(60, 110, 0, 0);
type10Btn.setPadding(300, 110, 0, 0);
type20Btn.setPadding(540, 110, 0, 0);
type5Btn.setButtonStateIds(R.drawable.hf_type_5_1, R.drawable.hf_type_5_2);
type10Btn.setButtonStateIds(R.drawable.hf_type_10_1, R.drawable.hf_type_10_2);
type20Btn.setButtonStateIds(R.drawable.hf_type_20_1, R.drawable.hf_type_20_2); type5Btn.setOnClickListener(new ImgBtnClickListener());
type10Btn.setOnClickListener(new ImgBtnClickListener());
type20Btn.setOnClickListener(new ImgBtnClickListener()); }
import android.view.MotionEvent;
import android.view.View;
import android.widget.ImageButton;public class GameImageButtonView extends ImageButton{
private int buttonUpId;
private int buttonDownId;
public GameImageButtonView(Context context) {
// TODO Auto-generated constructor stub
super(context);
}
public void setButtonStateIds(int upId, int downId)
{
System.out.println(getId());
buttonUpId = upId;
buttonDownId = downId;
setImageResource(upId);
setBackgroundColor(0x00000000);
setOnTouchListener(new ImgBtnTouchListener()); }
class ImgBtnTouchListener implements OnTouchListener
{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
System.out.println(getId());
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
System.out.println("down");
setImageResource(buttonDownId);
}
else
{
System.out.println("up");
setImageResource(buttonUpId);
}
return false;
}
}
}
调用的方式是private void initBtns() {
type5Btn = new GameImageButtonView(this);
type10Btn = new GameImageButtonView(this);
type20Btn = new GameImageButtonView(this);
type5Btn.setId(1000);
type10Btn.setId(2000);
type20Btn.setId(3000);
addContentView(type5Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type10Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type20Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
type5Btn.setPadding(60, 110, 0, 0);
type10Btn.setPadding(300, 110, 0, 0);
type20Btn.setPadding(540, 110, 0, 0);
type5Btn.setButtonStateIds(R.drawable.hf_type_5_1, R.drawable.hf_type_5_2);
type10Btn.setButtonStateIds(R.drawable.hf_type_10_1, R.drawable.hf_type_10_2);
type20Btn.setButtonStateIds(R.drawable.hf_type_20_1, R.drawable.hf_type_20_2); type5Btn.setOnClickListener(new ImgBtnClickListener());
type10Btn.setOnClickListener(new ImgBtnClickListener());
type20Btn.setOnClickListener(new ImgBtnClickListener()); }
解决方案 »
- 已知activitiy/class name,有方法能获取package name吗?
- 如何在菜单事件里面保存onActivityResult()回传的数据
- alertDialog.build的setMultiChoiceItems方法如何得到多选的值
- 如何给ListView的每一行使用不同的背景图片?
- ViewFlipper中切换layout
- Android~~~!~~~
- 关于百度地图api开发中遇到的两个问题,求解答
- 如何在指定主题下显示想要的DatePickerDialog样式
- Android程序不能运行
- WebView.setScaleX 后,视图显示不全
- jni thread 退出异常 , native thread exited without detaching
- 求android大神指点,拜谢!曲线坐标显示传送问题
现在的问题不是图片改变没成功,是new 三个按钮,点击任意一个,都是最后new的那个在发生改变
我试过了,直接new ImageButton,然后addContentView,然后三个button都setOnClickListenern,响应的还是最后一个。是不是android 开发中,不适用xml来布局,直接new 和addContentView的操作,需要有些特殊设置?
private void initBtns() {
type5Btn = new ImageButton(this);
type10Btn = new ImageButton(this);
type20Btn = new ImageButton(this);
type5Btn.setImageResource(R.drawable.hf_type_5_1);
type10Btn.setImageResource(R.drawable.hf_type_10_1);
type20Btn.setImageResource(R.drawable.hf_type_20_1);
type5Btn.setBackgroundColor(0x00000000);
type10Btn.setBackgroundColor(0x00000000);
type20Btn.setBackgroundColor(0x00000000);
type5Btn.setPadding(60, 110, 0, 0);
type10Btn.setPadding(300, 110, 0, 0);
type20Btn.setPadding(540, 110, 0, 0);
type5Btn.setId(10000);
type10Btn.setId(20000);
type20Btn.setId(30000);
addContentView(type5Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type10Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
addContentView(type20Btn,new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
type5Btn.setOnClickListener(new ImgBtnClickListener());
type10Btn.setOnClickListener(new ImgBtnClickListener());
type20Btn.setOnClickListener(new ImgBtnClickListener()); } class ImgBtnClickListener implements OnClickListener {
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == type5Btn) {
System.out.println("5 click");
UserModel.getInstance().gameType = HappyFingerConsts.GAME_TYPE_5;
}
if (v == type10Btn) {
System.out.println("10 click");
UserModel.getInstance().gameType = HappyFingerConsts.GAME_TYPE_10;
}
if (v == type20Btn) {
System.out.println("20 click");
UserModel.getInstance().gameType = HappyFingerConsts.GAME_TYPE_20;
}
Intent nextActIntent = new Intent();
nextActIntent.setClass(GameTypeSelecterActivity.this, BuyAmountActivity.class);
startActivity(nextActIntent); }
}
你的这个问题,需要把所有的button放在一个viewgroup中,然后在把这个viewgroup,添加到activity中,就是用addContentView
找到问题所在了,不是addContentView的问题,而是我把按钮背景透明了,然后用了setPending,把背景拉大了,盖住了前面的按钮