1.定义两个shape:

my_button_shape_normal.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <stroke        android:width="1dp"        android:color="#007FFF" />    <corners android:radius="5dip" />    <padding        android:bottom="1dp"        android:left="10dp"        android:right="10dp"        android:top="1dp" /></shape>

my_button_shape_pressed.xml:

<?xml version="1.0" encoding="utf-8"?><shape xmlns:android="http://schemas.android.com/apk/res/android"    android:shape="rectangle" >    <stroke        android:width="1dp"        android:color="#007FFF" />    <corners android:radius="5dip" />    <padding        android:bottom="1dp"        android:left="10dp"        android:right="10dp"        android:top="1dp" /></shape>

2。定义一个drawable:my_button_style.xml

<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">    <item android:drawable="@drawable/my_button_shape_normal" android:state_focused="false" android:state_pressed="false"></item>    <item android:drawable="@drawable/my_button_shape_pressed" android:state_focused="false" android:state_pressed="true"></item></selector>

3.定义button布局(mybutton.xml):

<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:layout_width="wrap_content"    android:layout_height="wrap_content"    android:orientation="horizontal" >    <Button        android:id="@+id/reduce"        android:layout_width="50dp"        android:layout_height="30dp"        android:background="@drawable/my_button_style"        android:gravity="center"        android:paddingBottom="10dp"        android:text="-"        android:textColor="#007FFF" />    <Button        android:id="@+id/add"        android:layout_width="50dp"        android:layout_height="30dp"        android:layout_toRightOf="@+id/reduce"        android:background="@drawable/my_button_style"        android:gravity="center"        android:paddingBottom="10dp"        android:text="+"        android:textColor="#007FFF" /></RelativeLayout>

4.定义MyButton类:

public class MyButton extends RelativeLayout {private View view;private Button add, reduce;private OnAddReduceChangeStatusListener mAddReduceChangeStatusListener;public MyButton(Context context, AttributeSet attrs, int defStyle) {super(context, attrs, defStyle);// TODO Auto-generated constructor stub}public MyButton(Context context, AttributeSet attrs) {super(context, attrs);// TODO Auto-generated constructor stubview = LayoutInflater.from(context).inflate(R.layout.mybutton, this, true);init();}public MyButton(Context context) {super(context);// TODO Auto-generated constructor stub}private void init() {add = (Button) view.findViewById(R.id.add);reduce = (Button) view.findViewById(R.id.reduce);add.setOnTouchListener(new ComponentOnTouch());reduce.setOnTouchListener(new ComponentOnTouch());}class ComponentOnTouch implements OnTouchListener {@Overridepublic boolean onTouch(View v, MotionEvent event) {// TODO Auto-generated method stubswitch (v.getId()) {case R.id.add:if (mAddReduceChangeStatusListener != null) {mAddReduceChangeStatusListener.add(MyButton.this.getId(),event.getAction());}break;case R.id.reduce:if (mAddReduceChangeStatusListener != null) {mAddReduceChangeStatusListener.reduce(MyButton.this.getId(),event.getAction());}break;}return true;}}public void setOnAddReduceChangeStatusListener(OnAddReduceChangeStatusListener listener) {this.mAddReduceChangeStatusListener = listener;}public abstract interface OnAddReduceChangeStatusListener {public abstract boolean add(int viewId,int eventAction);public abstract boolean reduce(int viewId,int eventAction);}}

5。布局中使用:

<package.MyButton            android:id="@+id/mybutton_id"            android:layout_width="wrap_content"            android:layout_height="wrap_content" >        </package.MyButton>

6.代码中使用:

a.初始化:

mybutton = (MyButton) findViewById(R.id.mybutton_id);mybutton.setOnAddReduceChangeStatusListener(new OnAddReduceListener());

b.listener监听:

class OnAddReduceListener implements OnAddReduceChangeStatusListener {@Overridepublic boolean add(int viewId, int eventAction) {// TODO Auto-generated method stubif (eventAction == MotionEvent.ACTION_DOWN) {onTouchChange("add");} else if (eventAction == MotionEvent.ACTION_UP) {if (plusThread != null) {isOnLongClick = false;}} else if (eventAction == MotionEvent.ACTION_MOVE) {if (plusThread != null) {isOnLongClick = true;}} else if (eventAction == MotionEvent.ACTION_CANCEL) {if (plusThread != null) {isOnLongClick = false;}}return true;}@Overridepublic boolean reduce(int viewId, int eventAction) {// TODO Auto-generated method stubif (eventAction == MotionEvent.ACTION_DOWN) {onTouchChange("reduce");} else if (eventAction == MotionEvent.ACTION_UP) {if (miusThread != null) {isOnLongClick = false;}} else if (eventAction == MotionEvent.ACTION_MOVE) {if (miusThread != null) {isOnLongClick = true;}} else if (eventAction == MotionEvent.ACTION_CANCEL) {if (miusThread != null) {isOnLongClick = false;}}return true;}}private void onTouchChange(String method) {if (method.equals("add")) {plusThread = new PlusThread();isOnLongClick = true;plusThread.start();} else if (method.equals("reduce")) {miusThread = new MiusThread();isOnLongClick = true;miusThread.start();}}

c,定义两个线程用来加减:

// 减操作class MiusThread extends Thread {@Overridepublic void run() {while (isOnLongClick) {try {Thread.sleep(200);myHandler.sendEmptyMessage(1);} catch (InterruptedException e) {e.printStackTrace();}super.run();}}}// 加操作class PlusThread extends Thread {@Overridepublic void run() {while (isOnLongClick) {try {Thread.sleep(200);myHandler.sendEmptyMessage(2);} catch (InterruptedException e) {e.printStackTrace();}super.run();}}}

使用Handler进行处理:

Handler myHandler = new Handler() {@Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubif (msg.what == 1) {//加操作} else if (msg.what == 2) {//减操作}}};

更多相关文章

  1. android AlertDialog 弹窗自定义布局 点击外部不关闭弹窗
  2. Android自带的图标集合
  3. android suport respository和Android(安卓)Support Library
  4. 自定义progressbar使用图片
  5. android selector的使用
  6. Android(安卓)VectorDrawable与SVG
  7. android selector的使用
  8. Android:可拆分和可序列化之间的区别?
  9. Android(安卓)TextSwitcher的使用

随机推荐

  1. androidRadioGroup和RadioB…
  2. Android(安卓)面试之横竖屏切换的Activit
  3. Use Android(安卓)NDK r7 to compile FFm
  4. android include 使用
  5. Android(安卓)代码生成界面Sample
  6. 【译】Android(安卓)23最新接口变化(Andro
  7. Delphi XE5 android 获取电池电量
  8. Android(安卓)中保存图片的代码
  9. Android(安卓)密度转换多分辨率
  10. Android(安卓)DHT11驱动