2016-09-01 11 views
1

私は数字と3つのうち1つの文字列を選択できるダイアログが必要です。 ユーザーは文字列(年、月、週)を選択し、次にy、mまたはwの数を選択する必要があります。数字と文字列を選択するダイアログ

いずれも、それはこの

enter image description here

+0

これをチェックしてくださいhttps://developer.android.com/guide/topics/ui/con trols/pickers.html –

+0

'DatePickerDialog'を検索 – Sanoop

+0

私はそのことを知っています。私のアプリで使っていましたが、今は私が尋ねたようなものが必要です。デートのようなものではありませんが、Ageピッカーのようです。 –

答えて

2

そうです私はちょうどここにこのコードを使用していくつかのことを変更私が必要としていたのは、このトリックです

public void AgeDialog(){ 

    final android.support.v7.app.AlertDialog.Builder alert = new android.support.v7.app.AlertDialog.Builder(this); 
    alert.setCancelable(false); 

    LinearLayout l1 = new LinearLayout(getApplicationContext()); 

    l1.setOrientation(LinearLayout.HORIZONTAL); 
    final NumberPicker number =new NumberPicker((this)); 
    number.setMaxValue(12); 
    number.setMinValue(1); 
    number.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
    number.setWrapSelectorWheel(true); 
    final NumberPicker ageUnitss = new NumberPicker(this); 
    final String arrays[] = new String[3]; 
    arrays[0]="Years"; 
    arrays[1]="Months"; 
    arrays[2]="Days"; 

    ageUnitss.setMaxValue(2); 
    ageUnitss.setMinValue(0); 
    ageUnitss.setDescendantFocusability(NumberPicker.FOCUS_BLOCK_DESCENDANTS); 
    ageUnitss.setDisplayedValues(arrays); 
    ageUnitss.setWrapSelectorWheel(true); 


    l1.addView(number); 
    l1.addView(ageUnitss); 

    l1.setHorizontalGravity(Gravity.CENTER_HORIZONTAL); 

    alert.setView(l1); 

    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int whichButton) { 
      ageUnits = arrays[(ageUnitss.getValue())]; 
      age = number.getValue(); 
      fragment1.setAgeText(age +" "+ageUnits); 
     } 
    }); 

    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 
     public void onClick(DialogInterface dialog, int whichButton) { 
      // Canceled. 
     } 
    }); 

    alert.show(); 
} 
関連する問題