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) { //减操作 } } };
相关推荐
本文实例为大家分享了android自定义加减按钮的具体代码,供大家参考,具体内容如下 1、定义两个shape: my_button_shape_normal.xml: <?xml version=1.0 encoding=utf-8?> <shape xmlns:android=...
这个demo是关于android中自定义控件,在EditText两边放置加减按钮。 需求:带加减按钮的EditText控件。 如有BUG或者不 完善,欢迎小伙伴们提出~
自己封装的一个EditText两边放加减按钮Button来控制输入框的数值
Android自定义控件之购物车数量加减器 详细见博客:http://blog.csdn.net/whitley_gong/article/details/51956429
Android自定义控件EditText输入框两边加减按钮Button Demo【第二版】。 修复部分bug,添加了从XML中实例化控件的demo。
B_通过加减按钮操作数字 C_监听加减按钮 D_数组有最小值和最大值的限制 E_自定义属性 2.提供接口,让外界监听到数字的变化 1_设置接口 @Override public void onClick(View v) { if (v.getId() == R.id.btn_sub)...
Android 自定义控件 EditText输入框两边加减按钮Button
这个是Android应用代码中使用图片模拟按键发送键值,可以调节开关机,音量加减,退出等按钮。有音量的长按视觉效果
Android Studio 在xml文件中设置界面布局1、xml文件中给输入框editText修改下划线颜色2、设置button按钮为圆角及修改按钮颜色3、运用约束布局时预览效果和运行效果不一致 写一写今天困扰我很久的问题:如何给...
监听数字加减变化的EditText,一个加减按钮,一个edittext,edittext可以输入,也可以通过加减按钮改变,数字的改变可以通过监听器获取
本文实例为大家分享了Android实现购物车加减商品操作的具体代码,供大家参考,具体内容如下 MainActivity.java public class MainActivity extends AppCompatActivity { private Addand mAddand; @Override ...
一个可以加减数字的edittext,两个加减按钮可以用来加减数字,可以监听数字的变化