2013-07-22 26 views
7

Tôi có một lớp con tùy chỉnh DialogPreference mà tôi muốn có một RadioGroup với ba nút radio. Hai phần đầu tiên là các thành phần vani RadioButton có tiêu đề, nhưng đối với phần thứ ba, tôi muốn đặt một số EditText trực tiếp ở bên phải của nó để tôi có thể nhập giá trị tùy chỉnh khi nút đó được chọn.Thêm EditText cùng với RadioButton

Tôi đã thử đặt nút thứ ba vào ngang LinearLayout cùng với EditText nhưng sau đó nút thứ ba không tham gia vào số RadioGroup mẹ nữa.

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       android:layout_width="match_parent" 
       android:layout_height="match_parent"> 
    <RadioGroup 
      android:id="@+id/lactate_radio" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" > 

     <RadioButton 
       android:id="@+id/lactate_default_value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/lactate_default_value"/> 

     <RadioButton 
       android:id="@+id/lactate_calibrated_value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="@string/lactate_calibrated_value" /> 
     <LinearLayout android:orientation="horizontal" 
         android:layout_width="match_parent" 
         android:layout_height="wrap_content"> 
      <RadioButton 
        android:id="@+id/lactate_custom_value" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:text="@string/lactate_custom_value"/> 

      <EditText 
        android:id="@+id/lactate_custom_value_edit_text" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content"/> 

     </LinearLayout> 
    </RadioGroup> 
</LinearLayout> 

Screenshot of custom DialogPreference

Tôi cũng đã thử thêm LinearLayout này bằng chương trình nhưng vẫn có hành vi tương tự. Có cách nào để làm điều này, có lẽ bằng cách nói rõ ràng với phụ huynh RadioGroup về nút thứ ba hoặc bằng cách nào đó định vị EditText một cách thích hợp mà không sử dụng ngang LinearLayout? Nếu không, tôi nghĩ rằng tôi sẽ phải viết một lớp con RadioButton mà phải là một bữa tiệc thực sự!

+0

gì về một RelativeLayout sau đó Linear chứa RadioGroup và sau đó chỉ cần đặt EditText bên cạnh RadioButton thứ ba dựa trên id của nó thuộc tính. – TronicZomB

+0

Vì 'EditText' là chế độ xem hoàn toàn khác nên nó không thể là phần' RadioGroup'. Hoặc bạn có thể lập trình 'Enable' &' Disable' trong khi nút radio thứ ba được nhấp là cách để đạt được điều gì đó như thế này. Hy vọng bạn nhận được nó. –

Trả lời

3

Bạn sẽ có thể thay đổi bố cục bố mẹ thành RelativeLayout. Sau đó cung cấp cho bạn RadioGroup

android:orientation="vertical" 

sau đó đặt EditText bên cạnh và liên kết với các đáy của RadioGroup của bạn. Tôi đã không thử nó nhưng một cái gì đó như thế này sẽ làm việc.

Cập nhật

tôi có thời gian để kiểm tra nó. Điều này dường như cung cấp cho các đầu ra bạn đang tìm kiếm

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
      android:orientation="vertical" 
      android:layout_width="match_parent" 
      android:layout_height="match_parent"> 
<RadioGroup 
     android:id="@+id/lactate_radio" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" > 

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

    <RadioButton 
      android:id="@+id/lactate_calibrated_value" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="lactate_calibrated_value" /> 
     <RadioButton 
       android:id="@+id/lactate_custom_value" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:text="lactate_custom_value"/> 
    </RadioGroup> 

     <EditText 
      android:id="@+id/lactate_custom_value_edit_text" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBottom="@+id/lactate_radio" 
      android:layout_toRightOf="@+id/lactate_radio" 
      android:text="Test Text" /> 
</RelativeLayout> 

Lưu ý rằng tôi cũng thay đổi một số width s để wrap_content vì vậy nó sẽ phù hợp với nhau. Thay đổi orientation của RadioGroup để thẳng đứng loại bỏ sự cần thiết của các LinearLayout

+0

Thật tuyệt vời nếu có một cách để làm điều này giống như bài viết gốc của tôi. Hãy tưởng tượng nếu ai đó muốn một 'EditText' bên cạnh mỗi' RadioButton' chẳng hạn. Bất kể, điều này có vẻ là một giải pháp khả thi cho trường hợp của tôi. Chỉ cần chờ đợi để xem có người nào khác đi qua với một giải pháp kỳ diệu trước khi chấp nhận của bạn như là câu trả lời. Cảm ơn! – mharper

+1

Vâng, sẽ rất tuyệt nếu có thứ gì đó được tích hợp sẵn nhưng AFAIK thì không. Dù sao, bạn vẫn có thể làm điều đó một cách dễ dàng bằng cách sử dụng 'alignToRightOf' trong mỗi sau đó sử dụng' layout_below' và đặt mỗi 'EditText' bên dưới phần trước đó.Bạn được chào đón và tôi hiểu chờ đợi để xem bạn có nhận được gì tốt hơn không? – codeMagic

0
<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "horizontal" 
> 

<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "vertical" 
> 

<RadioGroup 
    <!-- radio buttons code here --> 
/> 

</LinearLayout> 

<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
> 

<EditText 

<!-- edit text code here --> 
    android:layout_alignBottom="@+id/id_ofRadioButton" 
    android:layout_toRightOf="@+id/id_ofRadioButton" 
/> 

</LinearLayout> 

Nếu bạn muốn chỉnh sửa văn bản cùng bên mỗi nút radio:

<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "vertical" 
> 

<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "horizontal" 
> 

<RadioGroup 
    <!-- single radio button code here --> 
/> 

<EditText 

<!-- edit text code here --> 

/> 

</LinearLayout> 


<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "vertical" 
> 

<LinearLayout 
    android:layout_height= "wrap_content" 
    android:layout_width= "wrap_content" 
    android:orientation= "horizontal" 
> 

<RadioGroup 
    <!-- single radio button code here --> 
/> 

<EditText 

<!-- edit text code here --> 

/> 

</LinearLayout> 

+0

Các thuộc tính như 'android: layout_toRightOf' không khả dụng với' LinearLayout'. Ngoài ra, trong giải pháp thứ hai của bạn, 'RadioButton' sẽ được lấy ra khỏi 'RadioGroup' – codeMagic

Các vấn đề liên quan