android自定义加减按钮
程序员文章站
2023-10-27 21:05:28
本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下1、定义两个shape:my_button_shape_normal.xml:
本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下
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 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、布局中使用:
<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 { @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) { //减操作 } } };
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。