如图,B不动时是白色底,点击上半部分+号为灰色,-号为白色;点击下半部分-号为灰色,+号为白色。这个按钮该如何实现?菜鸟求指导。

解决方案 »

  1.   

    我在想这个图是不是可以通过两个ImageButton来拼起来的(从中分一半)?
    因为可以为单个imageButton设置click效果..
      

  2.   

    可以倒是可以,只是公司要求的是一张图片,一个ImageButton,这该怎么做啊?
      

  3.   

    怎么定义View啊,可否给些提示
      

  4.   

    现在公司给我们培训,这是出的练习题。用来练习的,没办法啊。我这里有个已经写好的代码,可是我看不太懂,谁能帮忙注释一下。package com.cdy.client.pctapp.custom;import android.content.Context;
    import android.graphics.drawable.Drawable;
    import android.util.AttributeSet;
    import android.util.Log;
    import android.view.MotionEvent;
    import android.widget.Button;/**
     * 双向按钮
     * 
     * @author chendy
     * 
     */
    public class TwoKeyButton extends Button {
    // 禁用时的背景图
    private Drawable disBitmap;
    // 正常状态时的背景图
    private Drawable normalBitmap;
    // 上半部分被按下的背景图
    private Drawable upBitmap;
    // 下半部分被按下的背景图
    private Drawable downBitmap; private String TAG = getClass().getName(); private onKeyTouchListener keyTouchListener; public void setOnKeyTouchListener(onKeyTouchListener keyTouchListener) {
    this.keyTouchListener = keyTouchListener;
    } public TwoKeyButton(Context context) {
    super(context);
    } @SuppressWarnings("deprecation")
    public TwoKeyButton(Context context, AttributeSet attrs) {
    super(context, attrs);
    int resId = 0;
    resId = attrs.getAttributeResourceValue(null, "disableBackground", 0);
    if (0 != resId) {
    disBitmap = getResources().getDrawable(resId);
    }
    resId = attrs.getAttributeResourceValue(null, "normalBackground", 0);
    if (0 != resId) {
    normalBitmap = getResources().getDrawable(resId);
    setBackgroundDrawable(normalBitmap);
    }
    resId = attrs.getAttributeResourceValue(null, "upBackground", 0);
    if (0 != resId) {
    upBitmap = getResources().getDrawable(resId);
    }
    resId = attrs.getAttributeResourceValue(null, "downBackground", 0);
    if (0 != resId) {
    downBitmap = getResources().getDrawable(resId);
    }
    } @SuppressWarnings("deprecation")
    @Override
    public void setEnabled(boolean enabled) {
    if (enabled) {
    if (null != normalBitmap)
    setBackgroundDrawable(normalBitmap);
    } else {
    if (null != disBitmap)
    setBackgroundDrawable(disBitmap);
    }
    super.setEnabled(enabled);
    } @SuppressWarnings("deprecation")
    @Override
    public boolean onTouchEvent(MotionEvent event) {
    if (!isEnabled())
    return super.onTouchEvent(event);
    float y = 0;
    switch (event.getAction()) {
    case MotionEvent.ACTION_DOWN:
    Log.i(TAG, "ACTION_DOWN");
    y = event.getY();
    // 按钮的上半部分被按下
    if (y < getHeight() / 2) {
    if (null != keyTouchListener)
    keyTouchListener.onUpKeyTouch(this);
    Log.i(TAG, "BUTTON_UP");
    if (null != upBitmap)
    setBackgroundDrawable(upBitmap);
    } else {
    if (null != keyTouchListener)
    keyTouchListener.onDownKeyTouch(this);
    Log.i(TAG, "BUTTON_DOWN");
    if (null != downBitmap)
    setBackgroundDrawable(downBitmap);
    }
    break;
    case MotionEvent.ACTION_UP:
    if (null != keyTouchListener)
    keyTouchListener.onKeyTouchEnd(this);
    Log.i(TAG, "ACTION_DOWN");
    if (null != normalBitmap)
    setBackgroundDrawable(normalBitmap);
    break;
    default:
    break;
    }
    return super.onTouchEvent(event);
    } public interface onKeyTouchListener {
    /**
     * Up键被按下
     */
    void onUpKeyTouch(TwoKeyButton send); /**
     * Down键被按下
     */
    void onDownKeyTouch(TwoKeyButton send); /**
     * 触摸结束
     */
    void onKeyTouchEnd(TwoKeyButton send);
    }}
      

  5.   

    我自己想到办法了,具体实现代码如下://设置B按钮的点击效果  by lbbzx
    imageButtonb.setOnTouchListener(new OnTouchListener() { @Override
    public boolean onTouch(View v, MotionEvent event) {
    float y = imageButtonb.getHeight();//获取b按钮的高度
    int action = event.getAction();
    switch (action) { case MotionEvent.ACTION_DOWN:
    //判断鼠标点下的纵坐标与按钮一般的大小
    if (event.getY() < y / 2) {
    imageButtonb.setImageResource(R.drawable.b_up);//设置按钮图片
    } else {
    imageButtonb.setImageResource(R.drawable.b_down);
    }
    break;
    case MotionEvent.ACTION_UP:
    imageButtonb.setImageResource(R.drawable.b);
    break;
    default:
    break;
    }
    return false;
    }
    });