点击button弹出对话框菜单:
package zhang.choiceMenu;
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;
public class choice extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
new AlertDialog.Builder(choice.this)
.setTitle("choice")
.setItems(R.array.str_body,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0,
int arg1) {
// TODO Auto-generated method stub
String[] aryshop = getResources()
.getStringArray(
R.array.str_body);
new AlertDialog.Builder(choice.this)
.setMessage(aryshop[arg1])
.setNegativeButton(
"ok",
new DialogInterface.OnClickListener() {
@Override
public void onClick(
DialogInterface arg0,
int arg1) {
// TODO
// Auto-generated
// method stub
}
}).show();
}
}).show();
// TODO Auto-generated method stub
}
});
}
}
菜单项
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, choice!</string>
<string name="app_name">ChoiceMenu</string>
<string name="strtitle">按我选择:</string>
<string name="str">你选择的是:</string>
<array name="str_body">
<item>选项1</item>
<item>选项2</item>
<item>选项3</item>
<item>选项4</item>
<item>选项5</item>
<item>选项6</item>
</array>
</resources>
布局就一个BUTTON