facebook twitter hatena line google mixi email

Android/UIプルダウン

提供: 初心者エンジニアの簡易メモ
移動: 案内検索

プルダウンサンプル

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"
                />

参考:http://d.hatena.ne.jp/nkawamura/20131005/1380963701