您好,登錄后才能下訂單哦!
本文實(shí)例講述了Android滑動(dòng)按鈕事件。分享給大家供大家參考,具體如下:
今天紀(jì)錄一下滑動(dòng)按鈕功能。。
首先效果圖:
然后是分別建立三個(gè)文件,第一個(gè)是main.class,第二個(gè)是SlipButton.class,第三個(gè)是 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)聽(tīng)器 } @Override public void OnChanged(boolean CheckState) {//當(dāng)按鈕狀態(tài)被改變時(shí) // TODO Auto-generated method stub if(CheckState) Toast.makeText(this,"打開(kāi)了..." , 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)前按鈕是否打開(kāi),true為打開(kāi),flase為關(guān)閉 private boolean OnSlip = false;//記錄用戶是否在滑動(dòng)的變量 private float DownX,NowX;//按下時(shí)的x,當(dāng)前的x, private Rect Btn_On,Btn_Off;//打開(kāi)和關(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)聽(tīng)器,也可以直接復(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))//滑動(dòng)到前半段與后半段的背景不同,在此做判斷 canvas.drawBitmap(bg_off,matrix, paint);//畫出關(guān)閉時(shí)的背景 else canvas.drawBitmap(bg_on,matrix, paint);//畫出打開(kāi)時(shí)的背景 if(OnSlip)//是否是在滑動(dòng)狀態(tài), { if(NowX >= bg_on.getWidth())//是否劃出指定范圍,不能讓游標(biāo)跑到外頭,必須做這個(gè)判斷 x = bg_on.getWidth()-slip_btn.getWidth()/2;//減去游標(biāo)1/2的長(zhǎng)度... else x = NowX - slip_btn.getWidth()/2; }else{//非滑動(dòng)狀態(tài) if(NowChoose)//根據(jù)現(xiàn)在的開(kāi)關(guān)狀態(tài)設(shè)置畫游標(biāo)的位置 x = Btn_Off.left; else x = Btn_On.left; } if(x<0)//對(duì)游標(biāo)位置進(jìn)行異常判斷... 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ù)動(dòng)作來(lái)執(zhí)行代碼 { case MotionEvent.ACTION_MOVE://滑動(dòng) 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://松開(kāi) OnSlip = false; boolean LastChoose = NowChoose; if(event.getX()>=(bg_on.getWidth()/2)) NowChoose = true; else NowChoose = false; if(isChgLsnOn&&(LastChoose!=NowChoose))//如果設(shè)置了監(jiān)聽(tīng)器,就調(diào)用其方法.. ChgLsn.OnChanged(NowChoose); break; default: } invalidate();//重畫控件 return true; } public void SetOnChangedListener(OnChangedListener l){//設(shè)置監(jiān)聽(tīng)器,當(dāng)狀態(tài)修改的時(shí)候 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 = "測(cè)試:" 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開(kāi)發(fā)入門與進(jìn)階教程》、《Android調(diào)試技巧與常見(jiàn)問(wèn)題解決方法匯總》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android基本組件用法總結(jié)》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。