「Android/UIプルダウン」の版間の差分
提供: 初心者エンジニアの簡易メモ
(→プルダウンサンプル) |
|||
行3: | 行3: | ||
MainActivity.java | MainActivity.java | ||
<pre> | <pre> | ||
− | + | ArrayAdapter<String> adapter = new ArrayAdapter<>( | |
− | + | this, | |
− | + | android.R.layout.simple_spinner_item, | |
− | + | new String[] { | |
− | + | "test1", | |
− | + | "test2", | |
− | + | "test3", | |
− | + | "test4"} | |
− | + | ); | |
− | + | adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); | |
− | + | Spinner spinner = findViewById(R.id.sizelist); | |
− | + | spinner.setAdapter(adapter); | |
− | + | spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { | |
− | + | @Override | |
− | + | public void onItemSelected(AdapterView<?> parent, | |
− | + | View view, int position, long id) { | |
− | + | Spinner spinner = (Spinner)parent; | |
− | + | String item = (String)spinner.getSelectedItem(); | |
− | + | //textView.setText(item); | |
− | + | } | |
− | + | public void onNothingSelected(AdapterView<?> parent) { | |
− | + | // | |
− | + | } | |
− | + | }); | |
</pre> | </pre> | ||
2019年5月13日 (月) 18:08時点における版
プルダウンサンプル
MainActivity.java
ArrayAdapter<String> adapter = new ArrayAdapter<>( this, android.R.layout.simple_spinner_item, new String[] { "test1", "test2", "test3", "test4"} ); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); Spinner spinner = findViewById(R.id.sizelist); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Spinner spinner = (Spinner)parent; String item = (String)spinner.getSelectedItem(); //textView.setText(item); } public void onNothingSelected(AdapterView<?> parent) { // } });
main_activity.xml
<Spinner android:id="@+id/sizelist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginRight="5dp" android:layout_marginLeft="5dp" />