对话框不出现 [英] Dialog does not appear

查看:94
本文介绍了对话框不出现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码:

 公共类设置扩展了Activity实现OnClickListener {

private Activity活动;
私人AlertDialog.Builder构建器;

@Override
protected void onCreate(Bundle savedInstanceState){
// TODO自动生成的方法存根
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.settings);

Button bAdd =(Button)findViewById(R.id.bAdd);
bAdd.setOnClickListener(this);

活动=此;
builder =新的AlertDialog.Builder(活动);
builder.setMessage( message)
.setTitle( title);

}

@Override
public void onClick(View v){
// TODO自动生成的方法存根
开关(v .getId()){
case R.id.bAdd:
AlertDialog dialog = builder.create();
休息时间;
}

}


}

但是由于某种原因,我的弹出窗口没有出现,并且什么也没有执行。关于导致此故障的原因有什么想法?谢谢!

解决方案

您必须调用 show()方法而不是 create()



注意: create() 方法仅创建 Dialog 的实例,但不会显示它的实例。



一个建议:



您可以创建返回 Dialog 的方法,如下所示:

 公共对话框createNewDialog(int type){
AlertDialog dlg = null;
开关(类型){
case SOME_CONSTANT:
dlg = new AlertDialog.Builder(ActivityName.this / this)
.setTitle( Title)
.setMessage (消息)
.setPositiveButton(是,空)
.create();
休息时间;
}
}

然后您可以将其称为:

  createNewDialog(SOME_CONSTANT).show(); 

和您的对话框将显示。 / p>

尤其是在您这种情况下,您可以使用以下代码段实现目标:

  @Override 
public void onClick(View v){
// TODO自动生成的方法存根
开关(v.getId()){
case R.id .bAdd:
createNewDialog(SOME_CONSTANT).show();
休息时间;
}
}

希望它会有所帮助。


I use following code:

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;
        }

    }


}

But for some reason my popup doesn't appear and does nothing at all.. Any ideas on what is causing this malfunction? Thanks!

解决方案

You have to call show() method instead of create().

Note: create() method only creates instance of Dialog but it won't show its.

One suggestion:

You can create method that returns Dialog like this:

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;
   }
}

Then you can call it as:

createNewDialog(SOME_CONSTANT).show();

and your Dialog will be shown.

Especially in your case you can reach your goal with this snippet of code:

@Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        switch (v.getId()) {
        case R.id.bAdd:
            createNewDialog(SOME_CONSTANT).show();
            break;
        }
    }

Hope it helps.

这篇关于对话框不出现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆