烤面包没有出现在屏幕上

时间:2011-06-25 04:34:22

标签: android toast

5000毫秒我正在显示一个对话框解除我的对话框后,吐司应该出现,但不是,为什么我没有得到这个?我怎么能这样做?

总是感谢帮助..!

 signin.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {
        //  showDialog(0);
            t = new Thread() {
                public void run() {
                    register();
                    try {
                         while(counter<1){
                                showmsg(0);
                                Thread.sleep(5000);


                         }

            } catch (InterruptedException e) {
                e.printStackTrace();
            }
                }
            };
            t.start();
        }
    });


}

 @Override
protected Dialog onCreateDialog(int id) {
    switch (id) {
    case 0: {
        ++counter;
        dialog = new ProgressDialog(this);
        if(counter==1){
        dialog.setMessage("Registering...");
        }  
        else{

            Context context = getApplicationContext();
            CharSequence text = "Hello toast!";
            int duration = Toast.LENGTH_SHORT;

            Toast toast = Toast.makeText(context, text, duration);
            toast.show();
        } 
        dialog.setIndeterminate(true);
        dialog.setCancelable(true);
        return dialog;
    }
    }
    return null;
} 


 public void showmsg(int actionsToBePerformedOnScreen) {
        Message msg = new Message();
        msg.what = actionsToBePerformedOnScreen;
        handler.sendMessage(msg);

    }

    public Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
            case 0:

                showDialog(0);

                break;

            case 1:
                // clear all images in the list
                removeDialog(0);
                break;


            }

        };
    };

1 个答案:

答案 0 :(得分:2)

您正试图以onCreateDialog方法展示您的Toast,并且它以counter!=1为条件。显示对话框时不会调用onCreateDialog,但仅在创建对话框时调用。它只被叫一次,因此Toast永远不会出现。


我相信您正在尝试运行该线程,在运行时显示进度并将其解除并在Toast完成时显示Toast。如果是这种情况,实现目标的更简单和正确的方法是使用AsyncTask。在此处阅读:http://developer.android.com/reference/android/os/AsyncTask.html

随意提出更多问题。