Android : Drop Down Menu - che hanno semplice esempio ?

? Lyusten @ | Original: StackOverFlow
---

Che hanno semplice esempio per Drop Down Menu da Button Click ?

Necessità fare lista per i programmi installati e selezionare per l'avvio .

Menu listMenu = null;
listMenu.add("quasatron"); listMenu.add("magnetron"); listMenu.add("atarrilix");
onCreateOptionsMenu(listMenu);


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.popup_menu, menu);
        return true;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.food:
            makeToast("food","","","");
            return true;
        case R.id.other:
            makeToast("other","","","");
            return true;
        default:
            return super.onOptionsItemSelected(item);
        }
    }

E file XML di popup_menu :

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/food" android:title="Food" />
<item android:id="@+id/other" android:title="Other" />
</menu>
---

Top 5 Risposta

1user1153551 @

Questo esempio è me stesso per creare quando si seleziona una voce in discesa ( filatore ) Lista in mostra un'immagine momento rightside sulla base su selezionare la voce . quindi questo esempio aiuto a voi .

MainActivity.java
public class MainActivity extends Activity implements OnClickListener, OnItemSelectedListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        String[] technology = {"PHP", "Ruby", "Java", "SQL"};
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
        final Spinner spinnertech = (Spinner) findViewById(R.id.spinnertech);
        spinnertech.setAdapter(adapter);

        spinnertech.setOnItemSelectedListener(this);

    // Spinner Start.... 
    @Override
    public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
            long arg3) {
        int position = arg0.getSelectedItemPosition();
        ImageView ivtech = (ImageView) findViewById(R.id.imgtech);

        if(position == 0) {
            ivtech.setImageResource(R.drawable.php);
        } else if(position == 1) {
            ivtech.setImageResource(R.drawable.ruby);
        } else if(position == 2) {
            ivtech.setImageResource(R.drawable.java);
        } else if(position == 3) {
            ivtech.setImageResource(R.drawable.sql);
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> arg0) {

    }
    // Spinner End.... 

}
main.xml
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/TableLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <ImageView
            android:id="@+id/imgtech"
            android:layout_width="80dp"
            android:layout_height="60dp"
            android:layout_marginRight="10dp" />

        <Spinner
            android:id="@+id/spinnertech"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:layout_weight="1" />

    </LinearLayout>

</TableLayout>