2014-04-16 101 views
0

我想在對話框中點擊一個按鈕時創建一個對話框我需要2個單選按鈕,這些按鈕與移動默認音樂鏈接,其他則是移動設備的外部存儲器。任何人都可以幫助我,這裏是我的代碼如下?創建內部有2個單選按鈕的對話框

buttonSound = (Button) findViewById(R.id.btn_sound); 
     buttonSound.setOnClickListener (new OnClickListener(){ 
      public void onClick(View v){ 
       AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         getApplicationContext()); 
       Log.d("TAG","button inside mobile"); 
       alertDialogBuilder 
       //.setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setPositiveButton("Sound",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, close 
         // current activity 
         AlarmActivity.this.finish(); 
        } 
        }); 
//    builder.setPositiveButton("Sound",); 
       alertDialogBuilder 
       //.setMessage("Click yes to exit!") 
                              .setCancelable(false) 
       .setPositiveButton("SdCard",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, close 
         // current activity 

         Log.d("TAG","button inside sdCard"); 
         AlarmActivity.this.finish(); 
        } 
        }); 
       alertDialogBuilder.show(); 

而且它顯示如下錯誤

android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application 

回答

0

如下給出試試這個..

更改這個..

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         getApplicationContext()); 

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         AlarmActivity.this); 

編輯

radio_btn.xml

<?xml version="1.0" encoding="UTF-8"?> 
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/status_group" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_margin="5dp" 
    android:orientation="vertical" > 

    <RadioButton 
     android:id="@+id/default_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Default music" /> 

    <RadioButton 
     android:id="@+id/external_btn" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="External card music" /> 

</RadioGroup> 

JAVA

LayoutInflater inflater = getLayoutInflater(); 
    View dialoglayout = inflater.inflate(R.layout.radio_btn, (ViewGroup) getCurrentFocus()); 
    RadioButton default_btn = (RadioButton) dialoglayout.findViewById(R.id.default_btn); 
    RadioButton external_btn = (RadioButton) dialoglayout.findViewById(R.id.external_btn); 
    AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
         AlarmActivity.this); 
       Log.d("TAG","button inside mobile"); 
       alertDialogBuilder 
       //.setMessage("Click yes to exit!") 
       .setCancelable(false) 
       .setView(dialoglayout); 
       .setPositiveButton("OK",new DialogInterface.OnClickListener() { 
        public void onClick(DialogInterface dialog,int id) { 
         // if this button is clicked, close 
         // current activity 
         AlarmActivity.this.finish(); 
        } 
        }); 
       alertDialogBuilder.show(); 
+0

卻是露出的只有一個選項對話框。我需要顯示2選項之一是手機的默認音樂文件,其他是外部卡音樂文件 –

+0

@ user3116672請查看我的編輯。 – Hariharan

0

使用AlarmActivity.this或本這一翻譯的getApplicationContext()

建立在資源佈局/佈局

  1. radio_dialog_layout.xml

    <RadioButton 
        android:id="@+id/rd_!" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="A" /> 
    
    <RadioButton 
        android:id="@+id/rd_2" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/rd_!" 
        android:text="B" /> 
    
    <Button 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:layout_below="@+id/rd_2" 
        android:layout_centerInParent="true" 
        android:text="OK" /> 
    </RelativeLayout> 
    
  2. 通過使用此代碼創建活動對話框:

    Dialog dialog = new Dialog(this); 
    dialog.setContentView(R.layout.radio_dialog_layout); 
    dialog.setTitle("This is my custom dialog box"); 
    dialog.setCancelable(true); 
    // there are a lot of settings, for dialog, check them all out! 
    // set up radiobutton 
    RadioButton rd1 = (RadioButton) dialog.findViewById(R.id.rd_); 
    RadioButton rd2 = (RadioButton) dialog.findViewById(R.id.rd_2); 
    
    // now that the dialog is set up, it's time to show it 
    dialog.show(); 
    

您也可以參考這個教程:How to add lists, Checkboxes and Radio buttons to an AlertDialog – Android

0

創建一個XML文件,

<RadioButton 
    android:id="@+id/radiobutton1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Hi" /> 

<RadioButton 
    android:id="@+id/radiobutton2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/radiobutton1" 
    android:text="Bye" /> 

<Button 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/radiobutton2" 
    android:layout_centerInParent="true" 
    android:text="Click" /> 

,並在活動創建對話框,

Dialog dialog = new Dialog(MainActivity.this); 
    dialog.setContentView(R.layout.activity_main); 
    dialog.setTitle("My Dialog Box"); 
    dialog.setCancelable(true); 

    // set up radiobutton inside dialog box 
    RadioButton rb1 = (RadioButton) dialog.findViewById(R.id.radiobutton1); 
    RadioButton rb2 = (RadioButton) dialog.findViewById(R.id.radiobutton2); 

    // Show the dialog 
    dialog.show(); 
+1

對您的代碼給予一點解釋。 –