2013-06-08 2 views
1

У меня есть диалог, который отображает информацию, когда приложение загружается в первый раз. Поскольку пользователи в эти дни всегда нажимают «ok», не читая текст. Я хотел бы отключить кнопку OK в течение первых 5 секунд (желательно с обратным отсчетом внутри). Как это можно сделать?Включить alertdialog кнопку «ok» после x секунд

Мой код (не очень нужно):

new AlertDialog.Builder(this) 
     .setMessage("Very usefull info here!") 
     .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() { 
     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE); 
      // the rest of your stuff 
     } 
     }) 
     .show(); 

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

ответ

8

Здесь вы идете:

// Create a handler 
Handler handler = new Handler(); 

// Build the dialog 
AlertDialog dialog = new AlertDialog.Builder(this) 
    .setMessage("Very usefull info here!") 
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialog, int which) { 
     // the rest of your stuff 
    } 
}) 
.create(); 

dialog.show(); 

// Access the button and set it to invisible 
final Button button = dialog.getButton(AlertDialog.BUTTON_POSITIVE); 
button.setVisibility(View.INVISIBLE); 

// Post the task to set it visible in 5000ms   
handler.postDelayed(new Runnable(){ 
    @Override 
    public void run() { 
     button.setVisibility(View.VISIBLE); 
    }}, 5000); 

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

Смежные вопросы