亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

android自定義加減按鈕

瀏覽:46日期:2022-09-25 10:14:03

本文實例為大家分享了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: 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: 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: 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: 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: 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
相關文章:
主站蜘蛛池模板: 日韩孕交japanese孕交 | 国产成人精品午夜 | 日韩激情成人 | 亚洲色图日韩 | 一级免费看片 | zoofilia杂交videos新一 | 国产成版人视频网站免费下 | 中文字幕在线观看一区二区 | 欧美日韩精品一区二区三区不卡 | 六月丁香深爱六月综合激情 | 中国一级特黄真人毛片免费看 | 在线免费看黄的网站 | 国产成人免费高清激情视频 | 午夜成年人网站 | 国产v精品成人免费视频400条 | 免费大片黄在线观看日本 | 一级黄色在线视频 | 美女被网站免费看九色视频 | 黄频在线看| 久久久久在线观看 | 国产一级视频播放 | 久久毛片网站 | 一级片视频免费 | 拍真实国产伦偷精品 | 欧美日韩视频在线观看高清免费网站 | 日韩福利在线视频 | 人成精品 | 黄网址在线永久免费观看 | 在线精品国产一区二区 | 国产精久久一区二区三区 | 国产三级大片 | 97在线观免费视频观看 | 国内不卡一二三四区 | 激情视频网站在线观看 | 国产一级做a爰片久久毛片男 | 成人不卡视频 | 操你影院| aaa在线观看高清免费 | 香港毛片在线观看 | 尤物视频www | 九九精 |