Android 在设置时间后隐藏对话框,就像自定义时间间隔 toast

发布于 2024-10-19 13:14:06 字数 1001 浏览 4 评论 0原文

我试图在屏幕上显示一些文本一段时间,类似于吐司,但能够指定它在屏幕上的确切时间。我认为警报对话框可能适用于此,但我似乎无法弄清楚如何自动关闭该对话框。

您能否建议一个 Toast 通知的替代方案,让我可以指定其显示的确切时间?

谢谢你!

static DialogInterface dialog = null;
public void toast(String text, int duration) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

    LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
    ((TextView)layout.findViewById(R.id.message)).setText(text);

    builder
        .setView(layout);


    builder.show();

    if (dialog!=null){
        dialog.cancel();
        dialog.dismiss();
    }

    dialog = builder.create();


    Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 500);


}

I'm trying to display some text on the screen for a set period of time, similar to a toast, but with the ability to specify the exact time it is on the screen. I'm thinking an alert dialog may work for this, but I can't seem to figure out how to dismiss the dialog automatically.

Can you suggest an alternative to toast notifications in which I can specify the exact time it is displayed?

Thank you!

static DialogInterface dialog = null;
public void toast(String text, int duration) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

    LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
    ((TextView)layout.findViewById(R.id.message)).setText(text);

    builder
        .setView(layout);


    builder.show();

    if (dialog!=null){
        dialog.cancel();
        dialog.dismiss();
    }

    dialog = builder.create();


    Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 500);


}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

简单 2024-10-26 13:14:06

您只需要计时调用 Dialog#dismiss();对于你的问题 Handler 类就足够了(+1 到 Javanator :))。

仅供参考,还有其他类,即 AlarmManager计时器 & TimerTask 可以帮助计时代码的运行。

编辑:

将您的代码更改为:

static DialogInterface dialog = null;
public void toast(String text, int duration) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

    LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
    ((TextView)layout.findViewById(R.id.message)).setText(text);

    builder
        .setView(layout);


    //builder.show(); commented this line
// I don't understand the purpose of this if block, anyways
// let it remain as is at the moment
    if (dialog!=null){
        dialog.cancel();
        dialog.dismiss();
    }

    dialog = builder.create().show();


    Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 500);


}

You just need to time calls to Dialog#dismiss(); for your problem Handler class would suffice(+1 to Javanator :)).

FYI, there are other classes namely AlarmManager, Timer & TimerTask that can help with timing the runs of your code.

EDIT:

Change your code to:

static DialogInterface dialog = null;
public void toast(String text, int duration) {

    final AlertDialog.Builder builder = new AlertDialog.Builder(gameplay);

    LayoutInflater inflater = (LayoutInflater) gameplay.getSystemService(gameplay.LAYOUT_INFLATER_SERVICE);
    View layout = inflater.inflate(R.layout.tutorial, (ViewGroup)gameplay.findViewById(R.id.layout_root));
    ((TextView)layout.findViewById(R.id.message)).setText(text);

    builder
        .setView(layout);


    //builder.show(); commented this line
// I don't understand the purpose of this if block, anyways
// let it remain as is at the moment
    if (dialog!=null){
        dialog.cancel();
        dialog.dismiss();
    }

    dialog = builder.create().show();


    Handler handler = null;
    handler = new Handler(); 
    handler.postDelayed(new Runnable(){ 
         public void run(){
             dialog.cancel();
             dialog.dismiss();
         }
    }, 500);


}
灯角 2024-10-26 13:14:06
 // Make your Main UIWorker Thread to execute this statement
 Handler mHandler = new Handler(); 

在您的代码需要关闭对话框的地方执行类似的操作。

 // this will dismiss the dialog after 2 Sec. set as per you 
 mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {     
        dialog.dismiss();
    }
 },2000L); 

希望这有帮助:)

 // Make your Main UIWorker Thread to execute this statement
 Handler mHandler = new Handler(); 

Do something like this where ever your code need to dismiss the dialog.

 // this will dismiss the dialog after 2 Sec. set as per you 
 mHandler.postDelayed(new Runnable() {
    @Override
    public void run() {     
        dialog.dismiss();
    }
 },2000L); 

Hope This Help :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文