2016-06-21 51 views
2

我有一個數字選擇器對話框來選擇小時和分鐘(間隔15分鐘)。它可以在大屏幕上正常工作,但在小屏幕和較舊的設備上,如果嘗試使用觸摸進行滾動,它會卡住。Android數字選擇器卡住

enter image description here

我延長android.widget.NumberPicker,減少字體大小,這有助於在某些設備上。

public class TimerNumberPicker extends android.widget.NumberPicker { 

    public TimerNumberPicker(Context context) { 
     super(context); 
    } 

    public TimerNumberPicker(Context context, AttributeSet attrs){ 
     super(context,attrs); 
    } 

    @Override 
    public void addView(View child) { 
     super.addView(child); 
     updateView(child); 
    } 

    @Override 
    public void addView(View child, ViewGroup.LayoutParams params) { 
     super.addView(child, params); 
     updateView(child); 
    } 

    @Override 
    public void addView(View child, int index, ViewGroup.LayoutParams params) { 
     super.addView(child, index, params); 
     updateView(child); 
    } 

    private void updateView(View view) { 
     if(view instanceof EditText){ 
      ((EditText) view).setTextSize(TypedValue.COMPLEX_UNIT_SP,16); 
      view.setTag(FontUtil.TAG_CONDENSED_LIGHT); 
      FontUtil.getInstence(getContext()).setFontByTag(view); 
     } 
    } 
} 

是什麼使得它更復雜的是,在最後的時間,只有值(0, 15, 30, 45),我必須setWrapSelectorWheel to false

我在想,是否可以始終顯示向上和向下箭頭,然後禁用滾動。

回答

0

將其更改爲帶有數字類型的Edittext,如果客戶端將數字設置爲高於或低於您的範圍,則使用Toast。這是更優雅的視覺,而且不會給你任何問題都

0

你可以使用神奇PickView

見​​

這是一個輔助lib中我們挑選日期或省像IOS系統WheelView小部件。

+2

在那裏做廣告,不是嗎? – Vucko

+1

不,我不是圖書館的所有者,我只是使用它,它的工作很完美 –

+0

正如@Vucko所建議的那樣,您應該表明您是圖書館的所有者,並提供了一個很好的示例,說明如何提供幫助:http:// meta.stackoverflow.com/questions/298734/is-it-acceptable-to-promote-my-own-library-as-part-of-a-real-answer – antonio

0

也許你可以使用TimePicker來解決它。

這可能是佈局:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="wrap_content" 
android:padding="8dp" 
android:layout_height="wrap_content"> 
<ScrollView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"> 
    <LinearLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:orientation="vertical" 
     > 
<TimePicker 
    android:id="@+id/time_picker" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    /> 

<Button 
    android:id="@+id/date_time_set" 
    android:layout_width="match_parent" 
    android:text="Set" 
    android:layout_height="wrap_content" /> 
    </LinearLayout> 
</ScrollView> 

雖然可以包含在一個正常的警告對話框:

private void showTimePicker(){ 
    final View dialogView = View.inflate(getActivity(), <time_picker_container_layout_id>, null); 
    final AlertDialog alertDialog = new AlertDialog.Builder(getActivity()).create(); 

    dialogView.findViewById(R.id.date_time_set).setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View view) { 

      TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker); 

      String time = timePicker.getCurrentHour()+":"+timePicker.getCurrentMinute()); 

      dateTime.setText(time); 
      alertDialog.dismiss(); 
     }}); 


    TimePicker timePicker = (TimePicker) dialogView.findViewById(R.id.time_picker); 

    timePicker.setCurrentHour(<default_hour>); 
    timePicker.setCurrentMinute(<default_minute>); 

    alertDialog.setView(dialogView); 
    alertDialog.show(); 
} 

如果需要只顯示15' ,你可以將拾取的分鐘舍入到最接近的有效值。

佈局中的scrollView將減少與選取器大小相關的問題。 我希望它有幫助。