android自定義加減按鈕
本文實(shí)例為大家分享了android自定義加減按鈕的具體代碼,供大家參考,具體內(nèi)容如下
1、定義兩個(gè)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、定義一個(gè)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監(jiān)聽:
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,定義兩個(gè)線程用來加減:
// 減操作
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進(jìn)行處理:
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
if (msg.what == 1) {
//加操作
} else if (msg.what == 2) {
//減操作
}
}
};
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- android計(jì)算器實(shí)現(xiàn)兩位數(shù)的加減乘除
- Android基于反射技術(shù)實(shí)現(xiàn)的加減乘除運(yùn)算示例
- Android實(shí)現(xiàn)簡(jiǎn)單加法計(jì)算器
- Android實(shí)現(xiàn)加法計(jì)算器
- Android studio實(shí)現(xiàn)簡(jiǎn)單計(jì)算器
- android實(shí)現(xiàn)簡(jiǎn)單計(jì)算器功能
- Android實(shí)現(xiàn)簡(jiǎn)易計(jì)算器小程序
- Android studio設(shè)計(jì)簡(jiǎn)易計(jì)算器
- Android實(shí)戰(zhàn)教程第一篇之最簡(jiǎn)單的計(jì)算器
- Android計(jì)算器編寫代碼
相關(guān)文章
在Android系統(tǒng)中使用gzip進(jìn)行數(shù)據(jù)傳遞實(shí)例代碼
HTTP協(xié)議上的GZIP編碼是一種用來改進(jìn)WEB應(yīng)用程序性能的技術(shù),4.4MB的文本數(shù)據(jù)經(jīng)過Gzip傳輸?shù)娇蛻舳酥笞優(yōu)?92KB,壓縮效率極高,下面與大家分享下具體的實(shí)現(xiàn)2013-06-06
Android ScrollView嵌套橫向滑動(dòng)控件時(shí)沖突問題
本篇文章主要介紹了Android ScrollView嵌套橫向滑動(dòng)控件時(shí)沖突問題,具有一定的參考價(jià)值,有興趣的可以了解一下2017-08-08
Android中Intent機(jī)制詳解及示例總結(jié)(總結(jié)篇)
本文是小編日常收集整理些有關(guān)Android中Intent機(jī)制詳解及示例總結(jié),對(duì)android中intent相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-04-04
Android給自定義按鍵添加廣播和通過廣播給當(dāng)前焦點(diǎn)輸入框賦值
這篇文章主要介紹了Android給自定義按鍵添加廣播和通過廣播給當(dāng)前焦點(diǎn)輸入框賦值的相關(guān)資料,需要的朋友可以參考下2016-10-10
Android onKeyDown監(jiān)聽返回鍵無效的解決辦法
這篇文章主要介紹了 Android onKeyDown監(jiān)聽返回鍵無效的解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
RxJava+Retrofit實(shí)現(xiàn)網(wǎng)絡(luò)請(qǐng)求封裝的方法
Retrofit是當(dāng)前應(yīng)用非常廣泛的網(wǎng)絡(luò)請(qǐng)求框架,通常結(jié)合RxJava來進(jìn)行網(wǎng)絡(luò)請(qǐng)求,本文將展示一個(gè)采用RxJava+Retrofit的網(wǎng)絡(luò)請(qǐng)求demo,感興趣的可以了解一下2019-04-04
快速解決fragment中onActivityResult不調(diào)用的問題
下面小編就為大家?guī)硪黄焖俳鉀Qfragment中onActivityResult不調(diào)用的問題。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-04-04

