我想写一个图片按钮,继承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()); }

解决方案 »

  1.   

    onTouch,你return false,应该改成return true.这样event就不会在view tree 中往下传递了
      

  2.   


    现在的问题不是图片改变没成功,是new 三个按钮,点击任意一个,都是最后new的那个在发生改变
      

  3.   

    ImgBtnClickListener,怎么定义的?看一下log,看看对应的没个button click的log.
      

  4.   


    我试过了,直接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); }
    }
      

  5.   

    知道问题了,addContentView,这个东西只能调用一次,这个是activity用来初始化自己的UI的,或设置view root.你如果多次调用,每次都会先把前面的remove掉,然后添加新的。
    你的这个问题,需要把所有的button放在一个viewgroup中,然后在把这个viewgroup,添加到activity中,就是用addContentView
      

  6.   


    找到问题所在了,不是addContentView的问题,而是我把按钮背景透明了,然后用了setPending,把背景拉大了,盖住了前面的按钮
      

  7.   

    多次addContentView,也没问题?