Android скрыть диалоговое окно после установленного времени, как тост с пользовательским интервалом времени - PullRequest
4 голосов
/ 27 февраля 2011

Я пытаюсь отобразить текст на экране в течение определенного периода времени, похожего на тост, но с возможностью указать точное время на экране.Я думаю, что для этого может подойти диалоговое окно с предупреждением, но я не могу понять, как автоматически закрыть диалоговое окно.

Можете ли вы предложить альтернативу всплывающим уведомлениям, в которых я могу указать точное времяэто отображается?

Спасибо!

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


}

Ответы [ 2 ]

8 голосов
/ 27 февраля 2011

Вам просто нужно время звонить на Dialog#dismiss();для вашей задачи подойдет Handler класс (+1 к Javanator :)).

К вашему сведению, существуют и другие классы, а именно AlarmManager , Timer & 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);


}
5 голосов
/ 27 февраля 2011
 // 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); 

Надеюсь, что это поможет :)

...