答案 0 :(得分:2)
试试这段代码。我认为这肯定会对你有所帮助
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setTitle("Pick Colour");
dialog.setItems(R.array.colors,new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int position) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"selected Item:"+position, Toast.LENGTH_SHORT).show();
}
});
dialog.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
AlertDialog alert = dialog.create();
alert.show();
}
}
如果您有任何疑问,请告诉我。