本文實例講述了Android編程實現(xiàn)滑動按鈕功能。分享給大家供大家參考,具體如下:
網(wǎng)站設(shè)計制作過程拒絕使用模板建站;使用PHP+MYSQL原生開發(fā)可交付網(wǎng)站源代碼;符合網(wǎng)站優(yōu)化排名的后臺管理系統(tǒng);網(wǎng)站制作、網(wǎng)站設(shè)計收費合理;免費進行網(wǎng)站備案等企業(yè)網(wǎng)站建設(shè)一條龍服務(wù).我們是一家持續(xù)穩(wěn)定運營了10年的創(chuàng)新互聯(lián)公司網(wǎng)站建設(shè)公司。
首先效果圖:

然后是分別建立三個文件,第一個是main.class,第二個是SlipButton.class,第三個是 onchangeListener.class
main.class
import android.app.Activity;
import android.os.Bundle;
import android.widget.Toast;
public class Main extends Activity implements OnChangedListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn);//獲得指定控件
myBtn.SetOnChangedListener(this);//為控件設(shè)置監(jiān)聽器
}
@Override
public void OnChanged(boolean CheckState) {//當(dāng)按鈕狀態(tài)被改變時
// TODO Auto-generated method stub
if(CheckState)
Toast.makeText(this,"打開了..." , Toast.LENGTH_SHORT).show();
else
Toast.makeText(this,"關(guān)閉了..." , Toast.LENGTH_SHORT).show();
}
}
SlipButton.class
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
public class SlipButton extends View implements OnTouchListener{
private boolean NowChoose = false;//記錄當(dāng)前按鈕是否打開,true為打開,flase為關(guān)閉
private boolean OnSlip = false;//記錄用戶是否在滑動的變量
private float DownX,NowX;//按下時的x,當(dāng)前的x,
private Rect Btn_On,Btn_Off;//打開和關(guān)閉狀態(tài)下,游標(biāo)的Rect
private boolean isChgLsnOn = false;
private OnChangedListener ChgLsn;
private Bitmap bg_on,bg_off,slip_btn;
public SlipButton(Context context) {
super(context);
// TODO Auto-generated constructor stub
init();
}
public SlipButton(Context context, AttributeSet attrs) {
super(context, attrs);
// TODO Auto-generated constructor stub
init();
}
private void init(){//初始化
//載入圖片資源
bg_on = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_on);
bg_off = BitmapFactory.decodeResource(getResources(), R.drawable.slip_bg_off);
slip_btn = BitmapFactory.decodeResource(getResources(), R.drawable.slip_btn);
//獲得需要的Rect數(shù)據(jù)
Btn_On = new Rect(0,0,slip_btn.getWidth(),slip_btn.getHeight());
Btn_Off = new Rect(
bg_off.getWidth()-slip_btn.getWidth(),
0,
bg_off.getWidth(),
slip_btn.getHeight());
setOnTouchListener(this);//設(shè)置監(jiān)聽器,也可以直接復(fù)寫OnTouchEvent
}
@Override
protected void onDraw(Canvas canvas) {//繪圖函數(shù)
// TODO Auto-generated method stub
super.onDraw(canvas);
Matrix matrix = new Matrix();
Paint paint = new Paint();
float x;
{
if(NowX<(bg_on.getWidth()/2))//滑動到前半段與后半段的背景不同,在此做判斷
canvas.drawBitmap(bg_off,matrix, paint);//畫出關(guān)閉時的背景
else
canvas.drawBitmap(bg_on,matrix, paint);//畫出打開時的背景
if(OnSlip)//是否是在滑動狀態(tài),
{
if(NowX >= bg_on.getWidth())//是否劃出指定范圍,不能讓游標(biāo)跑到外頭,必須做這個判斷
x = bg_on.getWidth()-slip_btn.getWidth()/2;//減去游標(biāo)1/2的長度...
else
x = NowX - slip_btn.getWidth()/2;
}else{//非滑動狀態(tài)
if(NowChoose)//根據(jù)現(xiàn)在的開關(guān)狀態(tài)設(shè)置畫游標(biāo)的位置
x = Btn_Off.left;
else
x = Btn_On.left;
}
if(x<0)//對游標(biāo)位置進行異常判斷...
x = 0;
else if(x>bg_on.getWidth()-slip_btn.getWidth())
x = bg_on.getWidth()-slip_btn.getWidth();
canvas.drawBitmap(slip_btn,x, 0, paint);//畫出游標(biāo).
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch(event.getAction())//根據(jù)動作來執(zhí)行代碼
{
case MotionEvent.ACTION_MOVE://滑動
NowX = event.getX();
break;
case MotionEvent.ACTION_DOWN://按下
if(event.getX()>bg_on.getWidth()||event.getY()>bg_on.getHeight())
return false;
OnSlip = true;
DownX = event.getX();
NowX = DownX;
break;
case MotionEvent.ACTION_UP://松開
OnSlip = false;
boolean LastChoose = NowChoose;
if(event.getX()>=(bg_on.getWidth()/2))
NowChoose = true;
else
NowChoose = false;
if(isChgLsnOn&&(LastChoose!=NowChoose))//如果設(shè)置了監(jiān)聽器,就調(diào)用其方法..
ChgLsn.OnChanged(NowChoose);
break;
default:
}
invalidate();//重畫控件
return true;
}
public void SetOnChangedListener(OnChangedListener l){//設(shè)置監(jiān)聽器,當(dāng)狀態(tài)修改的時候
isChgLsnOn = true;
ChgLsn = l;
}
}
onchangeListener.class
package CMD100.demo.slipButton;
public interface OnChangedListener {
abstract void OnChanged(boolean CheckState);
}
main.xml代碼
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<LinearLayout
android:orientation = "horizontal"
android:layout_width = "fill_parent"
android:layout_height = "wrap_content"
android:background = "#ff0000"
>
<TextView
android:text = "測試:"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
/>
<CMD100.demo.slipButton.SlipButton
android:id = "@+id/slipBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft = "10sp"
/>
</LinearLayout>
</LinearLayout>
注意:在xml里頭要放置的位置
<[包名].SlipButton android:id = "@+id/slipBtn" android:layout_width="wrap_content" android:layout_height="wrap_content" />
然后可以像其他控件一樣使用了...
SlipButton myBtn =(SlipButton) findViewById(R.id.slipBtn); myBtn.SetOnChangedListener(...);
代碼到這里就全部完成了。
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android布局layout技巧總結(jié)》、《Android視圖View技巧總結(jié)》、《Android開發(fā)入門與進階教程》、《Android調(diào)試技巧與常見問題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對大家Android程序設(shè)計有所幫助。
網(wǎng)站欄目:Android編程實現(xiàn)滑動按鈕功能詳解
分享URL:http://www.chinadenli.net/article48/iiedep.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供標(biāo)簽優(yōu)化、面包屑導(dǎo)航、Google、網(wǎng)站建設(shè)、網(wǎng)站排名、營銷型網(wǎng)站建設(shè)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)