2017-03-26 4 views
0

私はユーザに表示するメッセージのリストを持っています。これらは配列に格納されます。私はアラートを表示したいと思うし、ユーザーが正のボタンをクリックすると、メッセージを1つずつ調べます。各クリックでAndroidがAlertDialogテキストを変更しています

_msgCountは、私のクラスのプライベート変数です。

msgsは最終的な最終変数です。

すべてのログが記録されます。しかし、メッセージは変わらない。 _alert.hide()、setMessage()、そして_alert.show()もやってみました。

ありがとうございました!

AlertDialog.Builder builder = new AlertDialog.Builder(this); 

     _alert = builder.setTitle("Whoa there!") 
       .setMessage(msgs[_msgCount]) 
       .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) 
        { 
         Log.i("EasterHunt", "On positive click"); 
         _alert.setMessage(msgs[++_msgCount]); 
        } 
       }) 
       .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
        @Override 
        public void onClick(DialogInterface dialogInterface, int i) 
        { 
         Log.i("EasterHunt", "On negative click"); 
         Toast.makeText(MapsMarkerActivity.this, R.string.nope, Toast.LENGTH_SHORT).show(); 
        } 
       }) 
       .create(); 



     _alert.setOnShowListener(new DialogInterface.OnShowListener() { 
      @Override 
      public void onShow(DialogInterface dialog) 
      { 
       Log.i("EasterHunt", "what!"); 
      } 
     }); 
+0

あなたは次のようなものを試してみましたか?dialogInterface.setMessage(msgs [++ _ msgCount]); –

+0

このような(AlertDialog)dialogInterface.setMessage(msgs [++ _ msgCount]); https://developer.android.com/reference/android/app/AlertDialog.html –

答えて

1

ダイアログの終了操作を無効にするには、ポジティブボタンのクリックを上書きする必要があります。このようなことをしてください

... 
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
    @Override 
    public void onClick(DialogInterface dialogInterface, int i) { 
     // leave this empty 
    } 
}) 
...; 

_alert.show(); // show the dialog before getting its button 

_alert.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View view) { 
     Log.i("EasterHunt", "On positive click"); 
     if (_msgCount < msgs.length - 1) { 
      _alert.setMessage(msgs[++_msgCount]);  
     } else { 
      // do some action 
      _alert.dismiss(); 
     } 

    } 
}) 
+0

ニース。言及するポイント: 'getButton()'を実行する前にダイアログが既に表示されているはずです。つまり、 '_alert.show()'が実行されているはずです。 – azizbekian

+0

うん、それを忘れてしまった。私はそれを私の答えに加えます。ありがとう! – Sourabh

+0

これは私があなたの答えを得る前に実際にやったことですが、応答とコードに感謝します!また、最初に表示されているように....私はその笑に出会った! – kamcknig

0

既に構築されているAlertDialogのメッセージを変更できるかどうかは不明です。 毎回新しいものを作成しようとします。

String[] msgs = new String[] { "ciao", "boh", "buh" }; 
    int _msgCount = 0; 

    private void showDialog() { 
    if (_msgCount >= msgs.length) return; 

    new AlertDialog.Builder(this).setTitle("Whoa there!") 
     .setMessage(msgs[_msgCount++]) 
     .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
      Log.i("EasterHunt", "On positive click"); 
      showDialog(); 
      } 
     }) 
     .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() { 
      @Override 
      public void onClick(DialogInterface dialogInterface, int i) { 
      Log.i("EasterHunt", "On negative click"); 
      Toast.makeText(MainActivity.this, "nope", Toast.LENGTH_SHORT).show(); 
      } 
     }) 
     .show(); 
    } 
関連する問題