facebook twitter hatena line email

「Android/UIプルダウン」の版間の差分

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
(プルダウンサンプル)
行3: 行3:
 
MainActivity.java
 
MainActivity.java
 
<pre>
 
<pre>
        ArrayAdapter<String> adapter = new ArrayAdapter<>(
+
ArrayAdapter<String> adapter = new ArrayAdapter<>(
                this,
+
        this,
                android.R.layout.simple_spinner_item,
+
        android.R.layout.simple_spinner_item,
                new String[] {
+
        new String[] {
                        "test1",
+
                "test1",
                        "test2",
+
                "test2",
                        "test3",
+
                "test3",
                        "test4"}
+
                "test4"}
        );
+
);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
+
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        Spinner spinner = findViewById(R.id.sizelist);
+
Spinner spinner = findViewById(R.id.sizelist);
        spinner.setAdapter(adapter);
+
    spinner.setAdapter(adapter);
        spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
+
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
+
    @Override
            public void onItemSelected(AdapterView<?> parent,
+
    public void onItemSelected(AdapterView<?> parent,
                                      View view, int position, long id) {
+
            View view, int position, long id) {
                Spinner spinner = (Spinner)parent;
+
        Spinner spinner = (Spinner)parent;
                String item = (String)spinner.getSelectedItem();
+
        String item = (String)spinner.getSelectedItem();
                //textView.setText(item);
+
        //textView.setText(item);
            }
+
    }
            public void onNothingSelected(AdapterView<?> parent) {
+
    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"
                />

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