2016-03-28 102 views
1

我想知道如何檢查按鈕電臺檢查,以及如何告訴應用程序,只有一個電臺布頓檢查?Android - 選中檢查單選按鈕

PS:我創建XML 2個單選按鈕:

<RadioButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Boutton_Bluetooth" 
    android:id="@+id/BouttonRADIO_Bluetooth" 
    android:layout_gravity="center_horizontal" 
    android:textSize="20dp" 
    android:textStyle="bold" /> 

<RadioButton 
    android:layout_width="119dp" 
    android:layout_height="wrap_content" 
    android:text="@string/Boutton_RS232" 
    android:id="@+id/BouttonRADIO_RS232" 
    android:layout_gravity="center_horizontal" 
    android:textSize="20dp" 
    android:textStyle="bold"/> 

感謝提前:)

+0

可能的複製。但我已經創建了兩個單選按鈕。我會嘗試與radioGroup。 – McNavy

+0

是的'RadioGroup'使得它一次只檢查組中的一個'RadioButton'。 –

+0

我會盡力檢查你的想法。解決了。你可以關閉這個請。 :) – McNavy

回答

0

「我想知道如何檢查按鈕無線電檢查」)

yourRadioButton.isChecked(

「如何告訴應用程序,只有一個boutton辨認的收音機嗎?」

-put您的單選按鈕到radioGroup中

+0

感謝您的回答。我問你如何放置「Radiobutton.isChecked()」?我的兩個單選按鈕將顯示在alertdialog上。 – McNavy

+0

for examle [鏈接](http://www.learn-android-easily.com/2013/01/adding-radio-buttons-in-dialog.html) 但如果你需要更多的東西,然後只是一些單選按鈕,你的可以創建自己的對話類並根據需要進行設置... –

0

只要定義你的XML一樣,

<RadioGroup android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:orientation="horizontal"> 

       <RadioButton 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="@string/Boutton_Bluetooth" 
    android:id="@+id/BouttonRADIO_Bluetooth" 
    android:layout_gravity="center_horizontal" 
    android:textSize="20dp" 
    android:textStyle="bold" /> 

<RadioButton 
    android:layout_width="119dp" 
    android:layout_height="wrap_content" 
    android:text="@string/Boutton_RS232" 
    android:id="@+id/BouttonRADIO_RS232" 
    android:layout_gravity="center_horizontal" 
    android:textSize="20dp" 
    android:textStyle="bold"/> 

      </RadioGroup> 

希望它可以幫助你。

+0

感謝您的幫助。這是工作! :) – McNavy

0

要定義單擊事件處理程序按鈕,添加屬性爲<RadioButton>元素在你的XML佈局。此屬性的值必須是要響應點擊事件而要調用的方法的名稱。託管佈局的活動必須執行相應的方法。

<?xml version="1.0" encoding="utf-8"?> 
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

<RadioButton 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:text="@string/Boutton_Bluetooth" 
android:id="@+id/BouttonRADIO_Bluetooth" 
android:layout_gravity="center_horizontal" 
android:textSize="20dp" 
android:textStyle="bold" /> 

<RadioButton 
android:layout_width="119dp" 
android:layout_height="wrap_content" 
android:text="@string/Boutton_RS232" 
android:id="@+id/BouttonRADIO_RS232" 
android:layout_gravity="center_horizontal" 
android:textSize="20dp" 
android:textStyle="bold"/> 

</RadioGroup> 

在承載此佈局的活動,下面的方法處理點擊事件兩個單選按鈕:

public void onRadioButtonClicked(View view) { 
// Is the button now checked? 
boolean checked = ((RadioButton) view).isChecked(); 

// Check which radio button was clicked 
switch(view.getId()) { 
    case R.id.BouttonRADIO_Bluetooth: 
     if (checked) 
      // BouttonRADIO_Bluetooth 
     break; 
    case R.id.BouttonRADIO_RS232: 
     if (checked) 
      // BouttonRADIO_RS232 
     break; 
    } 
} 

Reference