2012-06-23 87 views
0

我有一個按鈕,點擊時必須生成一個對話框,其中包含一個數字選取器和確定取消按鈕。我正在使用Android 2.3.3(不是API 11),所以我已經從here下載了號碼選取器。它工作正常。點擊確定按鈕後,我需要輸入數字選擇器中的計數。任何幫助,將不勝感激。謝謝。如何從QuietlyCoding的NumberPicker中檢索值?

+0

這是不是一個真正的問題StackOverflow上。這不是一個標準的控制,而且真的是一個問題,你應該問控制作者。 [SO不能替代供應商技術支持](http://meta.stackexchange.com/a/128579/172661)。您鏈接的網站也提到了一個演示應用程序 - 您看過嗎? –

回答

0

創建一個自定義對話框...

創建XML佈局,稱爲picker_dialog.xml即:

<LinearLayout 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"/> 
    <com.mypackage.NumberPicker 
     //set your layout 
     /> 
    <LinearLayout 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:orientation="horizontal"> 
     <Button 
       //cancel button 
       /> 
     <Button 
       //Ok button 
       /> 
    </LinearLayout> 
</LinearLayout> 

創建自定義對話框類

public class DialogPicker extends Picker { 

    public DialogPicker(Context context) { 
     super(context); 
     setContentView(R.layout.picker_dialog); 
     NumberPicker numberPicker = (NumberPicker)findViewById(R.id.my_number_picker); 
     //do whatever 
     Button okbutton = (Button) findViewById(R.id.my_ok_button); 
     //set your click listener 
    } 
} 

在您的活動,剛剛運行:

PickerDialog pd = new PickerDialog(this); 
pd.show(); 

好運

+0

有一個錯誤發生在 PickerDialog pd = new PickerDialog(this); 它說: 構造函數PickerDialog(new View.OnClickListener(){})未定義。 這是我得到的唯一的錯誤。請幫忙。 –

+0

您應該傳遞上下文,而不是偵聽器,即傳遞MyActivity.this而不是指向您單擊偵聽器的「this」。 –

0
  1. 添加一個機器人:值id您NumberPicker元素在版面 XML。

  2. 當膨脹爲您AlertDialog佈局,調用 findViewById()上膨脹的佈局,傳遞你 在步驟#1中所提供的ID,和鑄造的結果是一個NumberPicker。

  3. 在演示中有一個NumberPicker類,它對應於您的數字選擇器窗口小部件,該類包含一個getCurrent()方法用它來檢索數字選擇器的當前值。

0

嘗試此類

公共類DialogPicker擴展選擇器{

public DialogPicker(Context context) { 
    super(context); 
    setContentView(R.layout.picker_dialog); 
    NumberPicker numberPicker = (NumberPicker)findViewById(R.id.my_number_picker); 
    //do whatever 
    Button okbutton = (Button) findViewById(R.id.my_ok_button); 
    //set your click listener 
} 

}