我使用以下代码:
public class Settings extends Activity implements OnClickListener { private Activity activity; private AlertDialog.Builder builder; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.settings); Button bAdd = (Button) findViewById(R.id.bAdd); bAdd.setOnClickListener(this); activity = this; builder = new AlertDialog.Builder(activity); builder.setMessage("message") .setTitle("title"); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.bAdd: AlertDialog dialog = builder.create(); break; } } }
但由于某种原因,我的弹出窗口没有出现,什么也没做.有什么想法导致这种故障?谢谢!
解决方法
您必须调用show()方法而不是create().
注意:create()方法只创建Dialog的实例,但不会显示它.
一个建议:
您可以创建返回Dialog的方法,如下所示:
public Dialog createNewDialog(int type) { AlertDialog dlg = null; switch (type) { case SOME_CONSTANT: dlg = new AlertDialog.Builder(ActivityName.this / this) .setTitle("Title") .setMessage("Message") .setPositiveButton("Yes",null) .create(); break; } }
然后你可以称之为:
createNewDialog(SOME_CONSTANT).show();
并显示您的对话框.
特别是在您的情况下,您可以使用以下代码片段达到目标:
@Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.bAdd: createNewDialog(SOME_CONSTANT).show(); break; } }
希望能帮助到你.