2013-06-28 46 views
1

在我的android應用程序中,我做了一個自定義的警報對話框。它在按鈕上單擊打開。在那個對話框中有4個按鈕應該像單選按鈕一樣反應。由於設計原因,我不想使用單選按鈕。Howto:在AlertDialog中實現ToggleButton的OnClickListener?

我無法完成這些操作來爲那些togglebuttons實現onClick監聽器。我以某種方式得到它們,只有一個按鈕可以是'開',其餘的應該關閉。 (就像一個單選按鈕組)

我目前不能用我的名譽上傳圖片,所以這裏是XML來代替:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/Dlg_DrvPut" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" > 
<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="@string/DrvPutDlgTitle" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 
<TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_x="1dp" 
    android:layout_y="3dp" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="@string/DrvPutDlgDrvCap" /> 
<TableLayout 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:shrinkColumns="*" 
    android:stretchColumns="*" > 
<TableRow 
     android:id="@+id/tableRow1" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content">" 
     <ToggleButton 
      android:id="@+id/rb_hit" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      android:textOn="Hit" 
      android:textOff="Hit" 
      android:layout_column="0" > 
     </ToggleButton> 
     <ToggleButton 
      android:id="@+id/rb_left" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      android:textOn="Left" 
      android:textOff="Left" 
      android:layout_column="1" /> 
     <ToggleButton 
      android:id="@+id/rb_right" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

     android:textOn="Right" 
      android:textOff="Right" 

      android:layout_column="2" /> 
     <ToggleButton 
      android:id="@+id/rb_miss" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 

      android:textOn="Miss" 
      android:textOff="Miss" 
      android:layout_column="3" /> 
     </TableRow> 
    </TableLayout> 
     <EditText 
      android:id="@+id/drvDst" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:ems="10" 
      android:inputType="number" > 
     </EditText> 
    <TextView 
    android:id="@+id/textView1" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_x="1dp" 
    android:layout_y="3dp" 
    android:gravity="center_vertical|center_horizontal" 
    android:text="@string/DrvPutDlgPutCap" /> 

    <EditText 
     android:id="@+id/putDst" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:ems="10" 
     android:inputType="numberDecimal" /> 

</LinearLayout> 

如何才能做到這一點?

我試過提到的onCheckedChangeListener,但是這似乎在AlertDialogBu​​ilder中不可用。 下面是按下按鈕顯示alertdialog後將要運行的代碼。 這是現在工作的代碼,每個切換按鈕都有一個onCheckedChangeListener。如果其中一個按鈕被用戶選中,則對於所有其他切換按鈕,選中狀態設置爲false。

public void pDrvPutClick(View v) { 
    final Button b = (Button) v; 

     LayoutInflater inflater = getLayoutInflater(); 
    View dstView = inflater.inflate(R.layout.dlg_drvput, null); 

     AlertDialog.Builder builder = new AlertDialog.Builder(context); 
    builder.setView(dstView); 
    final EditText edit_ddst = (EditText) dstView.findViewById(R.id.drvDst); 
    final EditText edit_pdst = (EditText) dstView.findViewById(R.id.putDst); 
    final ToggleButton rb1 = (ToggleButton) dstView.findViewById(R.id.rb_hit); 
    final ToggleButton rb2 = (ToggleButton) dstView.findViewById(R.id.rb_left); 
    final ToggleButton rb3 = (ToggleButton) dstView.findViewById(R.id.rb_right); 
    final ToggleButton rb4 = (ToggleButton) dstView.findViewById(R.id.rb_miss); 

    rb1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
    @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
     if (isChecked) 
     { 
      rb2.setChecked(false); 
      rb3.setChecked(false); 
      rb4.setChecked(false); 
     } 
    } 
    }); 

    rb2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
       rb1.setChecked(false); 
       rb3.setChecked(false); 
       rb4.setChecked(false); 
      } 
     } 
     }); 
    rb3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
       rb1.setChecked(false); 
       rb2.setChecked(false); 
       rb4.setChecked(false); 
      } 
     } 
     }); 
    rb4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){ 
     @Override 
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
     { 
      if (isChecked) 
      { 
       rb1.setChecked(false); 
       rb3.setChecked(false); 
       rb2.setChecked(false); 
      } 
     } 
     }); 



    builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){ 
     public void onClick(DialogInterface dialog, int whichButton){ 
      String ddst; 
      ddst = edit_ddst.getText().toString(); 
      if (ddst=="") { 
       ddst="Drive";  
      }else { 
       if (rb1.isChecked()){ 
        ddst="H"+ddst; 
       } 
       if (rb2.isChecked()){ 
        ddst="L"+ddst; 
       } 
       if (rb3.isChecked()){ 
        ddst="R"+ddst; 
       } 
       if (rb4.isChecked()){ 
        ddst="M"+ddst; 
       } 
      } 
      String pdst; 

      pdst = edit_pdst.getText().toString(); 
      if (pdst=="") { 
       pdst="PutDst"; 
      } 
      b.setText(ddst+"\n"+pdst); 


     } 


    }); 
+1

爲什麼不使用單選按鈕?你的onClickListener代碼在哪裏? – verybadalloc

+0

我不想要設計原因的單選按鈕...代碼現在上面... – Marco

回答

0

你必須使用onCheckedChangeListener

ToggleButton toggle = (ToggleButton) d.findViewById(R.id.toggle_id); 

toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() { 

public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 

    if(isChecked) 
    { 
     //your code 
    } 
    else 
    { 
     //your code 
    } 
} 
}); 
+0

thx,我會試試看...... :) – Marco

+0

是否爲你工作? – Eriz

+0

似乎沒有onCheckedChangeListener alertdialog.builder – Marco

相關問題