2017-06-27 39 views
0

我有一個警報對話框,其中包含RadioGroup中的兩個單選按鈕的自定義視圖。現在,當我通過膨脹自定義視圖在此RadioGroup上設置OnCheckChangedListener時,它不會被觸發。也沒有顯示錯誤日誌。無法觸發OnCheck在具有自定義視圖的警報對話框中更改RadioGroup的Evet

我已經按照這個帖子,詢問這裏之前:Link

以下是我的警告對話框代碼。

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this, R.style.AlertDialogStyle); 
    alt_bld.setTitle("Select Date"); 
    alt_bld.setView(R.layout.alert_dialog_radiogroup); 

    //here I'm setting listener. 
    try{ 
     final View view = View.inflate(addAlarm.this, R.layout.alert_dialog_radiogroup, null); 
     radioGroup = (RadioGroup)view.findViewById(R.id.radio); 
     radioGroup.clearCheck(); 
     radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
       //My code to handle checked radio button. 
      } 
     }); 
    } catch (Exception e){ 
     e.printStackTrace(); 
    } 

    //setting +ve and -ve buttons here. 
    ... 

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

這是我自定義的警報對話框。

alert_dialog_radiogroup.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="match_parent" 
android:layout_height="100sp" > 

<RadioGroup 
    android:layout_width="match_parent" 
    android:id="@+id/radio" 
    android:layout_marginTop="10sp" 
    android:layout_marginStart="25sp" 
    android:layout_marginBottom="15sp" 
    android:layout_height="wrap_content" 
    android:orientation="vertical"> 

    <RadioButton 
     android:id="@+id/track_everyday" 
     android:textColor="@color/colorPrimaryText" 
     android:layout_width="wrap_content" 
     android:buttonTint="@color/colorAccent" 
     android:layout_height="wrap_content" 
     android:text="@string/everyday" /> 

    <RadioButton 
     android:id="@+id/custom_track" 
     android:layout_marginTop="8sp" 
     android:textColor="@color/colorPrimaryText" 
     android:layout_width="wrap_content" 
     android:buttonTint="@color/colorAccent" 
     android:layout_height="wrap_content" 
     android:layout_below="@+id/rd" 
     android:text="@string/custom" /> 

</RadioGroup> 

我問這裏之前搜索這個問題。在某些帖子中,答案是在設置監聽器之前清除所有單選按鈕的檢查。我試過這個radioGroup.clearCheck();,但沒有工作。

那麼我在這裏錯過了什麼?感謝您的幫助。

+0

在實現偵聽器後,嘗試在alertBuilder.setView(View)方法中傳遞充氣視圖。 –

+0

謝謝。把它寫成一個有理由的答案,我會接受的。 @sarthakGandhi – Kaushal28

+0

@Pavneet_Singh我認爲這是行不通的。我以前試過。 – Kaushal28

回答

0

您正在初始化偵聽器之前設置視圖。

試試這個:

AlertDialog.Builder alt_bld = new AlertDialog.Builder(this, R.style.AlertDialogStyle); 
alt_bld.setTitle("Select Date"); 


try{ 
    final View view = View.inflate(addAlarm.this, R.layout.alert_dialog_radiogroup, null); 
    radioGroup = (RadioGroup)view.findViewById(R.id.radio); 
    radioGroup.clearCheck(); 
    radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { 
     @Override 
     public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) { 
      //My code to handle checked radio button. 
     } 

    }); 
    alt_bld.setView(view); 
} catch (Exception e){ 
    e.printStackTrace();  
} 

//setting +ve and -ve buttons here. 
... 

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

希望這有助於。

相關問題