Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android自定義控件實戰——滾動選擇器PickerView淺析

Android自定義控件實戰——滾動選擇器PickerView淺析

編輯:關於Android編程



Android自定義控件實戰——滾動選擇器PickerView  

    手機裡設置鬧鐘需要選擇時間,那個選擇時間的控件就是滾動選擇器,前幾天用手機刷了MIUI,發現自帶的那個時間選擇器效果挺好看的,於是就自己仿寫了一個,權當練手。先來看效果:

\

效果還行吧?實現思路就是自定義一個PickerView,單獨滾動的是一個PickerView,顯然上圖中有分和秒的選擇所以在布局裡用了兩個PickerView。由於這裡不涉及到text的點擊事件,所以只需要繼承View就行了,直接把text用canvas畫上去。PickerView的實現的主要難點:

難點1:

字體隨距離的漸變。可以看到,text隨離中心位置的距離變化而變化,這裡變化的是透明度alpha和字體大小TexSize,這兩個值我都設置了Max和Min值,通過其與中心點的距離計算scale。我用的是變化曲線是拋物線scale=1-ax^2(x<=Height/4),scale = 0(x>Height/4),a=(4/Height)^2。x就是距離View中心的偏移量。用圖片表示如下:

\

難點2:

text的居中。繪制text的時候不僅要使其在x方向上居中,還要在y方向上居中,在x方向上比較簡單,設置Paint的Align為Align.CENTER就行了,但是y方向上很蛋疼,需要計算text的baseline。

難點3:

循環滾動。為了解決循環滾動的問題我把存放text的List從中間往上下攤開,通過不斷地moveHeadToTail和moveTailToHead使選中的text始終是list的中間position的值。

 

以上就是幾個難點,了解了之後可以來看PickerView的代碼了:

 

  • [java] view plain copy print?    
    1. packagecom.jingchen.timerpicker;
    2. importjava.util.ArrayList;importjava.util.List;
    3. importjava.util.Timer;importjava.util.TimerTask;
    4. importandroid.content.Context;
    5. importandroid.graphics.Canvas;importandroid.graphics.Paint;
    6. importandroid.graphics.Paint.Align;importandroid.graphics.Paint.FontMetricsInt;
    7. importandroid.graphics.Paint.Style;importandroid.os.Handler;
    8. importandroid.os.Message;importandroid.util.AttributeSet;
    9. importandroid.view.MotionEvent;importandroid.view.View;
    10. /**
    11. *滾動選擇器*
    12. *@authorchenjing*
    13. */publicclassPickerViewextendsView
    14. {
    15. publicstaticfinalStringTAG="PickerView";/**
    16. *text之間間距和minTextSize之比*/
    17. publicstaticfinalfloatMARGIN_ALPHA=2.8f;/**
    18. *自動回滾到中間的速度*/
    19. publicstaticfinalfloatSPEED=2;
    20. privateListmDataList;/**
    21. *選中的位置,這個位置是mDataList的中心位置,一直不變*/
    22. privateintmCurrentSelected;privatePaintmPaint;
    23. privatefloatmMaxTextSize=80;
    24. privatefloatmMinTextSize=40;
    25. privatefloatmMaxTextAlpha=255;privatefloatmMinTextAlpha=120;
    26. privateintmColorText=0x333333;
    27. privateintmViewHeight;
    28. privateintmViewWidth;
    29. privatefloatmLastDownY;/**
    30. *滑動的距離*/
    31. privatefloatmMoveLen=0;privatebooleanisInit=false;
    32. privateonSelectListenermSelectListener;privateTimertimer;
    33. privateMyTimerTaskmTask;
    34. HandlerupdateHandler=newHandler(){
    35. @Override
    36. publicvoidhandleMessage(Messagemsg){
    37. if(Math.abs(mMoveLen)mMoveLen=0;if(mTask!=null)
    38. {mTask.cancel();
    39. mTask=null;performSelect();
    40. }}else
    41. //這裡mMoveLen/Math.abs(mMoveLen)是為了保有mMoveLen的正負號,以實現上滾或下滾mMoveLen=mMoveLen-mMoveLen/Math.abs(mMoveLen)*SPEED;
    42. invalidate();}
    43. };
    44. publicPickerView(Contextcontext)
    45. {super(context);
    46. init();}
    47. publicPickerView(Contextcontext,AttributeSetattrs)
    48. {super(context,attrs);
    49. init();}
    50. publicvoidsetOnSelectListener(onSelectListenerlistener)
    51. {mSelectListener=listener;
    52. }
    53. privatevoidperformSelect(){
    54. if(mSelectListener!=null)mSelectListener.onSelect(mDataList.get(mCurrentSelected));
    55. }
    56. publicvoidsetData(Listdatas){
    57. mDataList=datas;mCurrentSelected=datas.size()/2;
    58. invalidate();}
    59. publicvoidsetSelected(intselected)
    60. {mCurrentSelected=selected;
    61. }
    62. privatevoidmoveHeadToTail(){
    63. Stringhead=mDataList.get(0);mDataList.remove(0);
    64. mDataList.add(head);}
    65. privatevoidmoveTailToHead()
    66. {Stringtail=mDataList.get(mDataList.size()-1);
    67. mDataList.remove(mDataList.size()-1);mDataList.add(0,tail);
    68. }
    69. @OverrideprotectedvoidonMeasure(intwidthMeasureSpec,intheightMeasureSpec)
    70. {super.onMeasure(widthMeasureSpec,heightMeasureSpec);
    71. mViewHeight=getMeasuredHeight();mViewWidth=getMeasuredWidth();
    72. //按照View的高度計算字體大小mMaxTextSize=mViewHeight/4.0f;
    73. mMinTextSize=mMaxTextSize/2f;isInit=true;
    74. invalidate();}
    75. privatevoidinit()
    76. {timer=newTimer();
    77. mDataList=newArrayList();mPaint=newPaint(Paint.ANTI_ALIAS_FLAG);
    78. mPaint.setStyle(Style.FILL);mPaint.setTextAlign(Align.CENTER);
    79. mPaint.setColor(mColorText);}
    80. @Override
    81. protectedvoidonDraw(Canvascanvas){
    82. super.onDraw(canvas);//根據index繪制view
    83. if(isInit)drawData(canvas);
    84. }
    85. privatevoiddrawData(Canvascanvas){
    86. //先繪制選中的text再往上往下繪制其余的textfloatscale=parabola(mViewHeight/4.0f,mMoveLen);
    87. floatsize=(mMaxTextSize-mMinTextSize)*scale+mMinTextSize;mPaint.setTextSize(size);
    88. mPaint.setAlpha((int)((mMaxTextAlpha-mMinTextAlpha)*scale+mMinTextAlpha));//text居中繪制,注意baseline的計算才能達到居中,y值是text中心坐標
    89. floatx=(float)(mViewWidth/2.0);floaty=(float)(mViewHeight/2.0+mMoveLen);
    90. FontMetricsIntfmi=mPaint.getFontMetricsInt();floatbaseline=(float)(y-(fmi.bottom/2.0+fmi.top/2.0));
    91. canvas.drawText(mDataList.get(mCurrentSelected),x,baseline,mPaint);
    92. //繪制上方datafor(inti=1;(mCurrentSelected-i)>=0;i++)
    93. {drawOtherText(canvas,i,-1);
    94. }//繪制下方data
    95. for(inti=1;(mCurrentSelected+i)drawOtherText(canvas,i,1);}
    96. }
    97. /**
    98. *@paramcanvas*@paramposition
    99. *距離mCurrentSelected的差值*@paramtype
    100. *1表示向下繪制,-1表示向上繪制*/
    101. privatevoiddrawOtherText(Canvascanvas,intposition,inttype){
    102. floatd=(float)(MARGIN_ALPHA*mMinTextSize*position+type*mMoveLen);
    103. floatscale=parabola(mViewHeight/4.0f,d);floatsize=(mMaxTextSize-mMinTextSize)*scale+mMinTextSize;
    104. mPaint.setTextSize(size);mPaint.setAlpha((int)((mMaxTextAlpha-mMinTextAlpha)*scale+mMinTextAlpha));
    105. floaty=(float)(mViewHeight/2.0+type*d);FontMetricsIntfmi=mPaint.getFontMetricsInt();
    106. floatbaseline=(float)(y-(fmi.bottom/2.0+fmi.top/2.0));canvas.drawText(mDataList.get(mCurrentSelected+type*position),
    107. (float)(mViewWidth/2.0),baseline,mPaint);}
    108. /**
    109. *拋物線*
    110. *@paramzero*零點坐標
    111. *@paramx*偏移量
    112. *@returnscale*/
    113. privatefloatparabola(floatzero,floatx){
    114. floatf=(float)(1-Math.pow(x/zero,2));returnf<0?0:f;
    115. }
    116. @OverridepublicbooleanonTouchEvent(MotionEventevent)
    117. {switch(event.getActionMasked())
    118. {caseMotionEvent.ACTION_DOWN:
    119. doDown(event);break;
    120. caseMotionEvent.ACTION_MOVE:doMove(event);
    121. break;caseMotionEvent.ACTION_UP:
    122. doUp(event);break;
    123. }returntrue;
    124. }
    125. privatevoiddoDown(MotionEventevent){
    126. if(mTask!=null){
    127. mTask.cancel();mTask=null;
    128. }mLastDownY=event.getY();
    129. }
    130. privatevoiddoMove(MotionEventevent){
    131. mMoveLen+=(event.getY()-mLastDownY);
    132. if(mMoveLen>MARGIN_ALPHA*mMinTextSize/2)
    133. {//往下滑超過離開距離
    134. moveTailToHead();mMoveLen=mMoveLen-MARGIN_ALPHA*mMinTextSize;
    135. }elseif(mMoveLen<-MARGIN_ALPHA*mMinTextSize/2){
    136. //往上滑超過離開距離moveHeadToTail();
    137. mMoveLen=mMoveLen+MARGIN_ALPHA*mMinTextSize;}
    138. mLastDownY=event.getY();
    139. invalidate();}
    140. privatevoiddoUp(MotionEventevent)
    141. {//抬起手後mCurrentSelected的位置由當前位置move到中間選中位置
    142. if(Math.abs(mMoveLen)<0.0001){
    143. mMoveLen=0;return;
    144. }if(mTask!=null)
    145. {mTask.cancel();
    146. mTask=null;}
    147. mTask=newMyTimerTask(updateHandler);timer.schedule(mTask,0,10);
    148. }
    149. classMyTimerTaskextendsTimerTask{
    150. Handlerhandler;
    151. publicMyTimerTask(Handlerhandler){
    152. this.handler=handler;}
    153. @Override
    154. publicvoidrun(){
    155. handler.sendMessage(handler.obtainMessage());}
    156. }
    157. publicinterfaceonSelectListener
    158. {voidonSelect(Stringtext);
    159. }}  
      package com.jingchen.timerpicker;
      
      import java.util.ArrayList;
      import java.util.List;
      import java.util.Timer;
      import java.util.TimerTask;
      
      import android.content.Context;
      import android.graphics.Canvas;
      import android.graphics.Paint;
      import android.graphics.Paint.Align;
      import android.graphics.Paint.FontMetricsInt;
      import android.graphics.Paint.Style;
      import android.os.Handler;
      import android.os.Message;
      import android.util.AttributeSet;
      import android.view.MotionEvent;
      import android.view.View;
      
      /**
       * 滾動選擇器
       * 
       * @author chenjing
       * 
       */
      public class PickerView extends View
      {
      
      	public static final String TAG = "PickerView";
      	/**
      	 * text之間間距和minTextSize之比
      	 */
      	public static final float MARGIN_ALPHA = 2.8f;
      	/**
      	 * 自動回滾到中間的速度
      	 */
      	public static final float SPEED = 2;
      
      	private List mDataList;
      	/**
      	 * 選中的位置,這個位置是mDataList的中心位置,一直不變
      	 */
      	private int mCurrentSelected;
      	private Paint mPaint;
      
      	private float mMaxTextSize = 80;
      	private float mMinTextSize = 40;
      
      	private float mMaxTextAlpha = 255;
      	private float mMinTextAlpha = 120;
      
      	private int mColorText = 0x333333;
      
      	private int mViewHeight;
      	private int mViewWidth;
      
      	private float mLastDownY;
      	/**
      	 * 滑動的距離
      	 */
      	private float mMoveLen = 0;
      	private boolean isInit = false;
      	private onSelectListener mSelectListener;
      	private Timer timer;
      	private MyTimerTask mTask;
      
      	Handler updateHandler = new Handler()
      	{
      
      		@Override
      		public void handleMessage(Message msg)
      		{
      			if (Math.abs(mMoveLen) < SPEED)
      			{
      				mMoveLen = 0;
      				if (mTask != null)
      				{
      					mTask.cancel();
      					mTask = null;
      					performSelect();
      				}
      			} else
      				// 這裡mMoveLen / Math.abs(mMoveLen)是為了保有mMoveLen的正負號,以實現上滾或下滾
      				mMoveLen = mMoveLen - mMoveLen / Math.abs(mMoveLen) * SPEED;
      			invalidate();
      		}
      
      	};
      
      	public PickerView(Context context)
      	{
      		super(context);
      		init();
      	}
      
      	public PickerView(Context context, AttributeSet attrs)
      	{
      		super(context, attrs);
      		init();
      	}
      
      	public void setOnSelectListener(onSelectListener listener)
      	{
      		mSelectListener = listener;
      	}
      
      	private void performSelect()
      	{
      		if (mSelectListener != null)
      			mSelectListener.onSelect(mDataList.get(mCurrentSelected));
      	}
      
      	public void setData(List datas)
      	{
      		mDataList = datas;
      		mCurrentSelected = datas.size() / 2;
      		invalidate();
      	}
      
      	public void setSelected(int selected)
      	{
      		mCurrentSelected = selected;
      	}
      
      	private void moveHeadToTail()
      	{
      		String head = mDataList.get(0);
      		mDataList.remove(0);
      		mDataList.add(head);
      	}
      
      	private void moveTailToHead()
      	{
      		String tail = mDataList.get(mDataList.size() - 1);
      		mDataList.remove(mDataList.size() - 1);
      		mDataList.add(0, tail);
      	}
      
      	@Override
      	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
      	{
      		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
      		mViewHeight = getMeasuredHeight();
      		mViewWidth = getMeasuredWidth();
      		// 按照View的高度計算字體大小
      		mMaxTextSize = mViewHeight / 4.0f;
      		mMinTextSize = mMaxTextSize / 2f;
      		isInit = true;
      		invalidate();
      	}
      
      	private void init()
      	{
      		timer = new Timer();
      		mDataList = new ArrayList();
      		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
      		mPaint.setStyle(Style.FILL);
      		mPaint.setTextAlign(Align.CENTER);
      		mPaint.setColor(mColorText);
      	}
      
      	@Override
      	protected void onDraw(Canvas canvas)
      	{
      		super.onDraw(canvas);
      		// 根據index繪制view
      		if (isInit)
      			drawData(canvas);
      	}
      
      	private void drawData(Canvas canvas)
      	{
      		// 先繪制選中的text再往上往下繪制其余的text
      		float scale = parabola(mViewHeight / 4.0f, mMoveLen);
      		float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize;
      		mPaint.setTextSize(size);
      		mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha));
      		// text居中繪制,注意baseline的計算才能達到居中,y值是text中心坐標
      		float x = (float) (mViewWidth / 2.0);
      		float y = (float) (mViewHeight / 2.0 + mMoveLen);
      		FontMetricsInt fmi = mPaint.getFontMetricsInt();
      		float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0));
      
      		canvas.drawText(mDataList.get(mCurrentSelected), x, baseline, mPaint);
      		// 繪制上方data
      		for (int i = 1; (mCurrentSelected - i) >= 0; i++)
      		{
      			drawOtherText(canvas, i, -1);
      		}
      		// 繪制下方data
      		for (int i = 1; (mCurrentSelected + i) < mDataList.size(); i++)
      		{
      			drawOtherText(canvas, i, 1);
      		}
      
      	}
      
      	/**
      	 * @param canvas
      	 * @param position
      	 *            距離mCurrentSelected的差值
      	 * @param type
      	 *            1表示向下繪制,-1表示向上繪制
      	 */
      	private void drawOtherText(Canvas canvas, int position, int type)
      	{
      		float d = (float) (MARGIN_ALPHA * mMinTextSize * position + type
      				* mMoveLen);
      		float scale = parabola(mViewHeight / 4.0f, d);
      		float size = (mMaxTextSize - mMinTextSize) * scale + mMinTextSize;
      		mPaint.setTextSize(size);
      		mPaint.setAlpha((int) ((mMaxTextAlpha - mMinTextAlpha) * scale + mMinTextAlpha));
      		float y = (float) (mViewHeight / 2.0 + type * d);
      		FontMetricsInt fmi = mPaint.getFontMetricsInt();
      		float baseline = (float) (y - (fmi.bottom / 2.0 + fmi.top / 2.0));
      		canvas.drawText(mDataList.get(mCurrentSelected + type * position),
      				(float) (mViewWidth / 2.0), baseline, mPaint);
      	}
      
      	/**
      	 * 拋物線
      	 * 
      	 * @param zero
      	 *            零點坐標
      	 * @param x
      	 *            偏移量
      	 * @return scale
      	 */
      	private float parabola(float zero, float x)
      	{
      		float f = (float) (1 - Math.pow(x / zero, 2));
      		return f < 0 ? 0 : f;
      	}
      
      	@Override
      	public boolean onTouchEvent(MotionEvent event)
      	{
      		switch (event.getActionMasked())
      		{
      		case MotionEvent.ACTION_DOWN:
      			doDown(event);
      			break;
      		case MotionEvent.ACTION_MOVE:
      			doMove(event);
      			break;
      		case MotionEvent.ACTION_UP:
      			doUp(event);
      			break;
      		}
      		return true;
      	}
      
      	private void doDown(MotionEvent event)
      	{
      		if (mTask != null)
      		{
      			mTask.cancel();
      			mTask = null;
      		}
      		mLastDownY = event.getY();
      	}
      
      	private void doMove(MotionEvent event)
      	{
      
      		mMoveLen += (event.getY() - mLastDownY);
      
      		if (mMoveLen > MARGIN_ALPHA * mMinTextSize / 2)
      		{
      			// 往下滑超過離開距離
      			moveTailToHead();
      			mMoveLen = mMoveLen - MARGIN_ALPHA * mMinTextSize;
      		} else if (mMoveLen < -MARGIN_ALPHA * mMinTextSize / 2)
      		{
      			// 往上滑超過離開距離
      			moveHeadToTail();
      			mMoveLen = mMoveLen + MARGIN_ALPHA * mMinTextSize;
      		}
      
      		mLastDownY = event.getY();
      		invalidate();
      	}
      
      	private void doUp(MotionEvent event)
      	{
      		// 抬起手後mCurrentSelected的位置由當前位置move到中間選中位置
      		if (Math.abs(mMoveLen) < 0.0001)
      		{
      			mMoveLen = 0;
      			return;
      		}
      		if (mTask != null)
      		{
      			mTask.cancel();
      			mTask = null;
      		}
      		mTask = new MyTimerTask(updateHandler);
      		timer.schedule(mTask, 0, 10);
      	}
      
      	class MyTimerTask extends TimerTask
      	{
      		Handler handler;
      
      		public MyTimerTask(Handler handler)
      		{
      			this.handler = handler;
      		}
      
      		@Override
      		public void run()
      		{
      			handler.sendMessage(handler.obtainMessage());
      		}
      
      	}
      
      	public interface onSelectListener
      	{
      		void onSelect(String text);
      	}
      }
      

       

       

      代碼裡的注釋都寫的很清楚了。接下來,我們就用寫好的PickerView實現文章開頭的圖片效果吧~

      首先看MainActivity的布局:

       

      [html] view plain copy print?    
      1. android:layout_height="match_parent"android:background="#000000">
      2. android:layout_width="wrap_content"android:layout_height="wrap_content"
      3. android:layout_centerInParent="true"android:background="#ffffff">
      4. android:id="@+id/minute_pv"android:layout_width="80dp"
      5. android:layout_height="160dp"/>
      6. android:layout_width="wrap_content"android:layout_height="wrap_content"
      7. android:layout_centerVertical="true"android:layout_toRightOf="@id/minute_pv"
      8. android:text="分"android:textColor="#ffaa33"
      9. android:textSize="26sp"android:textStyle="bold"/>
      10. android:id="@+id/second_pv"android:layout_width="80dp"
      11. android:layout_height="160dp"android:layout_toRightOf="@id/minute_tv"/>
      12. android:id="@+id/second_tv"android:layout_width="wrap_content"
      13. android:layout_height="wrap_content"android:layout_centerVertical="true"
      14. android:layout_toRightOf="@id/second_pv"android:text="秒"
      15. android:textColor="#ffaa33"android:textSize="26sp"
      16. android:textStyle="bold"/>
      17.  
        
        
            
        
                
        
                
        
                
        
                
            
        
        
        兩個PickerView兩個TextView,很簡單。

         

        下面是MainActivity的代碼:

         

        [java] view plain copy print?    
        1. packagecom.jingchen.timerpicker;
        2. importjava.util.ArrayList;importjava.util.List;
        3. importcom.jingchen.timerpicker.PickerView.onSelectListener;
        4. importandroid.app.Activity;
        5. importandroid.os.Bundle;importandroid.view.Menu;
        6. importandroid.widget.TextView;importandroid.widget.Toast;
        7. publicclassMainActivityextendsActivity
        8. {
        9. PickerViewminute_pv;PickerViewsecond_pv;
        10. @Override
        11. protectedvoidonCreate(BundlesavedInstanceState){
        12. super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);
        13. minute_pv=(PickerView)findViewById(R.id.minute_pv);second_pv=(PickerView)findViewById(R.id.second_pv);
        14. Listdata=newArrayList();Listseconds=newArrayList();
        15. for(inti=0;i<10;i++){
        16. data.add("0"+i);}
        17. for(inti=0;i<60;i++){
        18. seconds.add(i<10?"0"+i:""+i);}
        19. minute_pv.setData(data);minute_pv.setOnSelectListener(newonSelectListener()
        20. {
        21. @OverridepublicvoidonSelect(Stringtext)
        22. {Toast.makeText(MainActivity.this,"選擇了"+text+"分",
        23. Toast.LENGTH_SHORT).show();}
        24. });second_pv.setData(seconds);
        25. second_pv.setOnSelectListener(newonSelectListener(){
        26. @Override
        27. publicvoidonSelect(Stringtext){
        28. Toast.makeText(MainActivity.this,"選擇了"+text+"秒",Toast.LENGTH_SHORT).show();
        29. }});
        30. }
        31. @OverridepublicbooleanonCreateOptionsMenu(Menumenu)
        32. {getMenuInflater().inflate(R.menu.main,menu);
        33. returntrue;}
        34. }  
          package com.jingchen.timerpicker;
          
          import java.util.ArrayList;
          import java.util.List;
          
          import com.jingchen.timerpicker.PickerView.onSelectListener;
          
          import android.app.Activity;
          import android.os.Bundle;
          import android.view.Menu;
          import android.widget.TextView;
          import android.widget.Toast;
          
          public class MainActivity extends Activity
          {
          
          	PickerView minute_pv;
          	PickerView second_pv;
          
          	@Override
          	protected void onCreate(Bundle savedInstanceState)
          	{
          		super.onCreate(savedInstanceState);
          		setContentView(R.layout.activity_main);
          		minute_pv = (PickerView) findViewById(R.id.minute_pv);
          		second_pv = (PickerView) findViewById(R.id.second_pv);
          		List data = new ArrayList();
          		List seconds = new ArrayList();
          		for (int i = 0; i < 10; i++)
          		{
          			data.add("0" + i);
          		}
          		for (int i = 0; i < 60; i++)
          		{
          			seconds.add(i < 10 ? "0" + i : "" + i);
          		}
          		minute_pv.setData(data);
          		minute_pv.setOnSelectListener(new onSelectListener()
          		{
          
          			@Override
          			public void onSelect(String text)
          			{
          				Toast.makeText(MainActivity.this, "選擇了 " + text + " 分",
          						Toast.LENGTH_SHORT).show();
          			}
          		});
          		second_pv.setData(seconds);
          		second_pv.setOnSelectListener(new onSelectListener()
          		{
          
          			@Override
          			public void onSelect(String text)
          			{
          				Toast.makeText(MainActivity.this, "選擇了 " + text + " 秒",
          						Toast.LENGTH_SHORT).show();
          			}
          		});
          	}
          
          	@Override
          	public boolean onCreateOptionsMenu(Menu menu)
          	{
          		getMenuInflater().inflate(R.menu.main, menu);
          		return true;
          	}
          
          }
          

          OK了,自定義自己的TimerPicker就是這麼簡單~

           

          源碼下載

           

               
      1. 上一頁:
      2. 下一頁:
      熱門文章
      閱讀排行版
      Copyright © Android教程網 All Rights Reserved