2011-12-08 14 views
2

これは非常にばかげた質問ですが、ユーザーがダイアログ内のラジオボタンをクリックするとダイアログを閉じる方法を理解できません。ラジオボタンをクリックしてラジオボタンでダイアログを閉じる方法

builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
    public void onClick(DialogInterface dialog, int item) { 
    Button country = (Button) findViewById(R.id.land_fac_but); 
    country.setText(items[item]); 
    dialog.dismiss(); 
    } 
}); 

あなたはできるだけ早く項目がクリックされたとして、それを却下する場合:

final CharSequence[] items = {"Nederland", "België", "Denemarken", "Duitsland", "Frankrijk", "Italië", "Luxemburg", "Oostenrijk", "Verenigd Koningkrijg", "Zweden"}; 

     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Selecteer het land"); 
     builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 

       Button country = (Button) findViewById(R.id.land_fac_but); 
       country.setText(items[item]); 
      } 
     }); 

     AlertDialog alert = builder.create(); 
     alert.show(); 

答えて

6

ちょうどあなたのOnClickListenerdialog.dismiss()を追加)のonClickに()を解任。それ以外の場合は、AlertDialogを拡張して、より詳細なカスタムDialogを作成したい場合があります。

0

ハンドラでdialog.dismiss()に電話をかけることができます。

0

のAPIの下に試してみてください。

cancel 
dismiss 

あなたのコードは次のようになります。2、これをチェックアウトの間に若干の差分がある

public void onClick(DialogInterface dialog, int item) { 

      Button country = (Button) findViewById(R.id.land_fac_but); 
      country.setText(items[item]); 
      dialog.cancel(); 
     } 

http://developer.android.com/reference/android/app/Dialog.html#cancelを() http://developer.android.com/reference/android/app/Dialog.html#dismissは()

0

お試しください:

 AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     builder.setTitle("Selecteer het land"); 
     final AlertDialog alert; 
     builder.setSingleChoiceItems(items, -1, new DialogInterface.OnClickListener() { 
      public void onClick(DialogInterface dialog, int item) { 

       Button country = (Button) findViewById(R.id.land_fac_but); 
       country.setText(items[item]); 
       alert.dismiss(); 
      } 
     }); 

     alert = builder.create(); 
     alert.show(); 
関連する問題