4

ダイアログを含むアプリがあります

ボリューム シークバー ポップアップ (ボリューム ボタンをクリックすると開き、2 秒間非アクティブになると閉じます) のように、ユーザーがアプリとやり取りしていないときに、x 秒後にこのダイアログを閉じたいと思います。これを実装する最も簡単な方法は何ですか?

ありがとうございました

4

4 に答える 4

6

たとえば、ハンドラーを使用して、ユーザーがダイアログと対話するたびにその .removeCallbacks() および .postDelayed() メソッドを呼び出すことができます。

インタラクションが発生すると、.removeCallbacks() メソッドは .postDelayed() の実行をキャンセルし、その直後に .postDelayed() で新しい Runnable を開始します。

この Runnable 内で、ダイアログを閉じることができます。

    // a dialog
    final Dialog dialog = new Dialog(getApplicationContext());

    // the code inside run() will be executed if .postDelayed() reaches its delay time
    final Runnable runnable = new Runnable() {

        @Override
        public void run() {
            dialog.dismiss(); // hide dialog
        }
    };

    Button interaction = (Button) findViewById(R.id.bottom);

    final Handler h = new Handler();

            // pressing the button is an "interaction" for example
    interaction.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {


            h.removeCallbacks(runnable); // cancel the running action (the hiding process)
            h.postDelayed(runnable, 5000); // start a new hiding process that will trigger after 5 seconds
        }
    });

ユーザー インタラクションを追跡するには、次を使用できます。

@Override
public void onUserInteraction(){
    h.removeCallbacks(runnable); // cancel the running action (the hiding process)
    h.postDelayed(runnable, 5000); // start a new hiding process that will trigger after 5 seconds
}

あなたの活動で利用できるもの。

于 2013-08-08T22:37:29.587 に答える
0

私もアンドロイドは初めてですが、タイマーを作成することをお勧めします。タイマー t が 2 以上の場合は、何かを実行します。こんな感じだろう

if (t >= 2.0){
//Do whatever you want it to do
}

これはあなたの目的には合わないかもしれませんが、最終的に必要なコード行が少なくて済むかもしれません。(私がいつも言うように、少ないコードは多くのコードです) タイマーは基本的に簡単に作成できることは知っていますが、アプリで使用したことはありません。タイマーの作り方は特にわかりませんが、YouTube でチュートリアルを見つけることができると確信しています。

于 2013-08-08T23:54:40.047 に答える
0

タイムアウトについて調べていたら出てきた質問です。AsyncTaskHandler、およびを使用する回答を実装しましたRunnable。将来の回答検索者のための潜在的なテンプレートとして、ここで私の回答を提供しています。

private class DownloadTask extends AsyncTask<Void, CharSequence, Void> {
     //timeout timer set here for 2 seconds
     public static final int timerEnd = 2000;
     private Handler timeHandler = new Handler();

     @Override
     protected void onPreExecute() {              
          ProgressDialog dProgress = new ProgressDialog(/*Context*/);
          dProgress.setMessage("Connecting...");
          dProgress.setCancelable(false);
          dProgress.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                     //Dismissing dProgress
                     dialog.dismiss();
                     //Removing any Runnables
                     timeHandler.removeCallbacks(handleTimeout);
                     //cancelling the AsyncTask
                     cancel(true);

                     //Displaying a confirmation dialog
                     AlertDialog.Builder builder = new AlertDialog.Builder(/*Context*/);
                     builder.setMessage("Download cancelled.");
                     builder.setCancelable(false);
                     builder.setPositiveButton("OK", null);
                     builder.show();
                } //End onClick()
          }); //End new OnClickListener()
          dProgress.show();
     } //End onPreExecute()

     @Override
     protected Void doInBackground(Void... params) {
          //Do code stuff here
          //Somewhere, where you need, call this line to start the timer.
          timeHandler.postDelayed(handleTimeout, timerEnd);
          //when you need, call onProgressUpdate() to reset the timer and
          //output an updated message on dProgress.
          //...        
          //When you're done, remove the timer runnable.
          timeHandler.removeCallbacks(handleTimeout); 
          return null;
    } //End doInBackground()

    @Override
    protected void onProgressUpdate(CharSequence... values) {
        //Update dProgress's text
        dProgress.setMessage(values[0]);
        //Reset the timer (remove and re-add)
        timeHandler.removeCallbacks(handleTimeout);
        timeHandler.postDelayed(handleTimeout, timerEnd);
    } //End onProgressUpdate()

    private Runnable handleTimeout = new Runnable() {
        public void run() {
                //Dismiss dProgress and bring up the timeout dialog
                dProgress.dismiss();
                AlertDialog.Builder builder = new AlertDialog.Builder(/*Context*/);
                builder.setMessage("Download timed out.");
                builder.setCancelable(false);
                builder.setPositiveButton("OK", null);
                builder.show();
        }
    }; //End Runnable()
} //End DownloadTask class

を使用するのに少し慣れていない人には、 を作成してを呼び出すAsyncTask必要があります。DownloadTask object.execute()

例えば:

DownloadTask dTaskObject = new DownloadTask();
dTaskObject.execute();

実際には、このコードは、すべてのdoInBackground()コードを関数を介して実行することで、表示されているものよりもさらに進んでいるため、実際には、オブジェクトonProgressUpdate()を使用して他の関数を呼び出す必要がありました。DownloadTask

于 2014-06-03T20:30:06.007 に答える