2. MainActivity.java
package org.me.android_multiitemdialog; import android.app.Activity; import android.app.AlertDialog; import android.app.AlertDialog.Builder; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class MainActivity extends Activity { private Button showDialogButton; @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); showDialogButton = (Button) findViewById(R.id.showDialogButton); final AlertDialog mutiItemDialog = getMutiItemDialog(new String[]{"牛排","鸡排","猪排"}); showDialogButton.setOnClickListener(new Button.OnClickListener(){ @Override public void onClick(View view){ //显示对话框 mutiItemDialog.show(); } }); } public AlertDialog getMutiItemDialog(final String[] items) { Builder builder = new Builder(this); //设定对话框内的项目 builder.setItems(items, new DialogInterface.OnClickListener(){ @Override public void onClick(DialogInterface dialog,int which){ //当使用者点选对话框时,显示使用者所点选的项目 Toast.makeText(MainActivity.this, "您选择的是"+items[which], Toast.LENGTH_SHORT).show(); } }); return builder.create(); } }3. main.xml(Layout)
4. 按下「多个选项的对话框」按钮之後出现的画面。"
沒有留言:
張貼留言