如何创建alertDialog
由代码中定义的项目列表组成的列表。我想要类似于这样的警报:
我已经看到了,但我仍然无法创建警报。
试试这个代码。我认为这肯定会帮助你
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();
}
}
如果您有任何疑问,请告诉我。