注意
AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this)
这里的参数不能写getApplicationContext();否则会报错。
public class MainActivity extends ActionBarActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AlertDialog.Builder dialog= new AlertDialog.Builder(MainActivity.this); dialog.setTitle("标题"); dialog.setMessage("我是内容"); dialog.setIcon(R.drawable.abc_ab_stacked_solid_dark_holo); dialog.setPositiveButton("确认", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "确认了", 10).show(); } }); dialog.setNegativeButton("拒绝", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "拒绝了", 10).show(); } }); dialog.setNeutralButton("详情", new OnClickListener() { @Override public void onClick(DialogInterface arg0, int arg1) { Toast.makeText(getApplicationContext(), "就是这样", 10).show(); } }); dialog.show(); }
本文出自 “安卓开发零基础” 博客,请务必保留此出处http://jinchao.blog.51cto.com/9651275/1586100
原文地址:http://jinchao.blog.51cto.com/9651275/1586100