アンドロイドではDatePickerやTimePickerは年月日や時間用に作られています。小数点の入った数値などはNumberPickerを複数並べることで可能です。
 
![[Android] NumberPicker を設置する 1x1.trans - [Android] NumberPicker を設置する](https://akira-watson.com/wp-content/themes/simplicity2/images/1x1.trans.gif)
2021.2.1
NumberPicker
DatePicker などに比べると簡単で、ボタンを作成する感覚で作っていけます。
NumberPickerの設定するには、
- NumberPickerのインスタンス生成
- 最大と最小値の設定
- 値を取得
| 1 2 3 4 5 6 7 8 9 | // NumberPicker 設定  NumberPicker numberPicker = findViewById(R.id.number_picker); // 最大、最小を設定 numberPicker.setMaxValue(9); numberPicker.setMinValue(0); // 値を取得 int val = numberPicker.getValue(); | 
| 1 2 3 4 | <NumberPicker     android:id="@+id/numberPicker"     android:layout_width="wrap_content"     android:layout_height="wrap_content" /> | 
 
基本的にはこれだけなので簡単です。
 
サンプルコード
NumberPickerが選択できるのは1つの数字なので、「234」のような数値を作るためにはちょっと工夫が必要です。
数値自体は1桁づつの選択にならざる得ないので、NumberPickerを横に並べてしまえば可能となります。
 
数字1個につき NumberPicker が1個作りましたが配列を使えば短くできます。冗長的ですがわかりやすく書きました。
配列としてNumberPicker を作るにはコードで作成したほうがfor文を使えていいですね。以下のボタンの例が参考になります。
 
実際にコードをまとめるとこうなります
MainActivity.java
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | //package your.package.name; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.Button; import android.widget.NumberPicker; import android.widget.TextView; public class MainActivity extends AppCompatActivity {     private NumberPicker numPicker0, numPicker1, numPicker2, numPicker3,numPicker4;     private TextView pickerTextView;     private final String[] figures = new String[5];     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         pickerTextView = findViewById(R.id.text_view);         numPicker0 = findViewById(R.id.numPicker0);         numPicker1 = findViewById(R.id.numPicker1);         numPicker2 = findViewById(R.id.numPicker2);         numPicker3 = findViewById(R.id.numPicker3);         numPicker4 = findViewById(R.id.numPicker4);         Button pickerButton = findViewById(R.id.button1);         numPicker0.setMaxValue(9);         numPicker0.setMinValue(0);         numPicker1.setMaxValue(9);         numPicker1.setMinValue(0);         numPicker2.setMaxValue(9);         numPicker2.setMinValue(0);         numPicker3.setMaxValue(9);         numPicker3.setMinValue(0);         numPicker4.setMaxValue(9);         numPicker4.setMinValue(0);         // lambda         pickerButton.setOnClickListener( v -> {             figures[0] = String.valueOf(numPicker0.getValue());             figures[1] = String.valueOf(numPicker1.getValue());             figures[2] = String.valueOf(numPicker2.getValue());             figures[3] = String.valueOf(numPicker3.getValue());             figures[4] = String.valueOf(numPicker4.getValue());             String str = String.format("%s%s%s.%s%s",                     figures[0], figures[1], figures[2], figures[3], figures[4]);             Float fig = Float.parseFloat(str);             pickerTextView.setText(String.valueOf(fig));         });     } } | 
 
レイアウトです
activity_main.xml
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:padding="10dp"     android:background="#dfe"     android:gravity="center"     android:orientation="vertical" >     <TextView         android:id="@+id/text_view"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:layout_gravity="center"         android:layout_margin="30dp"         android:textStyle="bold"         android:text="@string/number"         android:textColor="#00f"         android:textSize="50sp" />     <LinearLayout         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:gravity="center"         android:layout_margin="10dp"         android:orientation="horizontal" >         <NumberPicker             android:id="@+id/numPicker0"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="#aaf" />         <NumberPicker             android:id="@+id/numPicker1"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="#aaf" />         <NumberPicker             android:id="@+id/numPicker2"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="#aaf" />         <TextView             android:layout_width="wrap_content"             android:layout_height="match_parent"             android:background="#aaf"             android:gravity="center"             android:text="@string/dot"             android:textSize="40sp" />         <NumberPicker             android:id="@+id/numPicker3"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="#aaf" />         <NumberPicker             android:id="@+id/numPicker4"             android:layout_width="wrap_content"             android:layout_height="wrap_content"             android:background="#aaf" />     </LinearLayout>     <Button         android:id="@+id/button1"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:layout_margin="40dp"         android:text="@string/ok" /> </LinearLayout> | 
リソース
strings.xml
| 1 2 3 4 5 6 | <resources>     <string name="app_name">Your App Name</string>     <string name="number">Number</string>     <string name="dot">.</string>     <string name="ok">OK</string> </resources> | 
サンプル動画
int から String にして、更に float 変換して String に戻しています。これは、「012.34」みたいなものを「12.34」にするためです。
尚、レイアウトの記述で、TextView のようにPickerの数字の色を変えたりするには、EditTextなどを使わないとできません。
同じく、Picker が存在するActivityで、ソフトキーボードが開いてしまうことがあります。最初にPicker、すなわちEditText にfocusが当たるからです。
他のものにfocusを当てておくことで回避できます。
Buttonがいいですね、こんな感じで
| 1 2 3 4 5 6 7 8 9 10 | Button button = (Button)findViewById(R.id.button); button.requestFocus(); // ... <Button     android:id="@+id/button"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:focusableInTouchMode="true" /> | 
 
 
関連ページ:
DatePicker
Reference:
NumberPicker
![[Android] NumberPicker を設置する cat41 00 100x100 - [Android] NumberPicker を設置する](https://akira-watson.com/wp-content/uploads/2019/07/cat41_00-100x100.jpg)
