android 点击按钮时显示菜单应怎样实现?
点击button弹出对话框菜单
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;
publicclass choice extends Activity {
private Button button;
/** Called when the activity is first created. */
@Override
publicvoid onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener(){
@Override
publicvoid onClick(View arg0) {
new AlertDialog.Builder(choice.this)
.setTitle("choice")
.setItems(R.array.str_body, new DialogInterface.OnClickListener() {
@Override
publicvoid 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
publicvoid onClick(DialogInterface arg0, int arg1) {
// TODO Auto-generated method stub
}
}).show();
}
}).show();
// TODO Auto-generated method stub
}});
}
}
菜单项
<?xmlversion="1.0"encoding="utf-8"?>
<resources>
<stringname="hello">Hello World, choice!</string>
<stringname="app_name">ChoiceMenu</string>
<stringname="strtitle">按我选择:</string>
<stringname="str">你选择的是:</string>
<arrayname="str_body">
<item>选项1</item>
<item>选项2</item>
<item>选项3</item>
<item>选项4</item>
<item>选项团颂5</item>
<item>选项6</item>
</array>
</resources>
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_MENU) {
//do something
}
return super.onKeyDown(keyCode, event);
}