热门标签 | HotTags
当前位置:  开发笔记 > Android > 正文

android自定义加减按钮

本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下 1、定义两个shape: my_button_sh

本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下

1、定义两个shape:

my_button_shape_normal.xml:

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 

my_button_shape_pressed.xml:

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

 

 

2、定义一个drawable:my_button_style.xml

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 
 

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

<&#63;xml version="1.0" encoding="utf-8"&#63;>


 

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 stub
 view = 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 {

 @Override
 public boolean onTouch(View v, MotionEvent event) {
 // TODO Auto-generated method stub
 switch (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、布局中使用:


 

6.代码中使用:

a.初始化:

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

b.listener监听:

class OnAddReduceListener implements OnAddReduceChangeStatusListener {

 @Override
 public boolean add(int viewId, int eventAction) {
 // TODO Auto-generated method stub
 if (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;
 }

 @Override
 public boolean reduce(int viewId, int eventAction) {
 // TODO Auto-generated method stub
 if (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 {
 @Override
 public void run() {
 while (isOnLongClick) {
 try {
 Thread.sleep(200);
 myHandler.sendEmptyMessage(1);
 } catch (InterruptedException e) {
 e.printStackTrace();
 }
 super.run();
 }
 }
 }

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

使用Handler进行处理:

Handler myHandler = new Handler() {

 @Override
 public void handleMessage(Message msg) {
 // TODO Auto-generated method stub
 if (msg.what == 1) {
 //加操作
 } else if (msg.what == 2) {
 //减操作
 }
 }
 };

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。


推荐阅读
author-avatar
十九世纪末的伦敦_371
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有