Android学习历程-AlerDialog(接收数组)
package com.example.testcontextmenu; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class TestAlertDialog01 extends Activity { private TextView myTextView; private Button myButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); myTextView =(TextView)findViewById(R.id.myTextView); myButton = (Button)findViewById(R.id.myButton); final String[] str = {"奥尔良","紫菜堡","咖啡"}; final AlertDialog.Builder builder = new AlertDialog.Builder(this); myButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // builder.setTitle("请点餐").setItems(str, listener); builder.setTitle("请点餐").setSingleChoiceItems(str, -1, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { myTextView.setText(str[which]); } }); AlertDialog a = builder.create(); a.show(); } }); } }