【问题标题】:Edittext inside radiogroup无线电组内的编辑文本
【发布时间】:2014-10-18 06:38:25
【问题描述】:

我正在尝试如下布局:

|--r1--|---EditText---|

|--r2--|---TextView---|

“r1”和“r2”位置的小部件是RadioButton

我希望: r1 和 r2 应该在同一个 RadioGroup 中。 我无法将EditText 放在 r1 旁边。 r2 旁边的 textview 很简单。

有人可以帮忙吗?

【问题讨论】:

  • 您使用的是什么布局?最好选择相对布局

标签: android android-layout radio-button


【解决方案1】:
LinearLayout 1 - Horizontal

    linear layout 2 - Vertcal
         RadioGroup with you radio buttons
    close linear layout 2

    linear layout 3 - Vertical
         edit Text here
    close linear layout 3


close LinearLayout 1

希望这种结构能有所帮助。

代码..

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >

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

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" />

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

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

</LinearLayout>

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

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

        <requestFocus />
    </EditText>

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

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

 </LinearLayout>

</LinearLayout>

谢谢!

【讨论】:

    【解决方案2】:

    试试这个布局

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        >
    
        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >
    
            <RelativeLayout 
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <RadioButton
                    android:id="@+id/radioButton1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginTop="20dp"
                    android:text="" />
    
                <EditText
                    android:id="@+id/editText1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBottom="@+id/radioButton1"
                    android:layout_marginLeft="28dp"
                    android:layout_toRightOf="@+id/radioButton1"
                    android:ems="10" >
    
                    <requestFocus />
                </EditText>
    
                <RadioButton
                    android:id="@+id/radioButton2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_below="@+id/radioButton1"
                    android:layout_marginTop="16dp"
                    android:text="" />
    
                <TextView
                    android:id="@+id/textView1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignBaseline="@+id/radioButton2"
                    android:layout_alignBottom="@+id/radioButton2"
                    android:layout_alignLeft="@+id/editText1"
                    android:layout_alignRight="@+id/editText1"
                    android:text="Large Text"
                    android:textAppearance="?android:attr/textAppearanceLarge" />
    
            </RelativeLayout>
    
    
        </RadioGroup>
    
    </RelativeLayout>
    

    它看起来像这样

    【讨论】:

    • 代码中的问题是我可以同时检查两个单选按钮..:(我希望当我选择一个单选按钮时,其他应该自动取消选中.. 虽然使用单选按钮会发生同样的错误
    【解决方案3】:

    这里是:)

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    
    <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="btn1" />
    
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sample text" />
    
    </RadioGroup>
    
    <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="btn1" />
    
        <EditText
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="sample text" />
    
    </RadioGroup>
    </LinearLayout>
    

    【讨论】:

    • 我想加入同一个电台组:(
    • 我可以看到两个不同的无线电组 :(
    • Ty,正是我所需要的。在接受的答案中,edittexts 可能无法与单选按钮完美对齐。
    【解决方案4】:

    可以使用“GridLayout”来实现。我已经在我的程序中完成了。

    【讨论】:

      【解决方案5】:

      使用这个 xml 布局

      <RelativeLayout>
         <RadioGroup>
            <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
            <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" />
         </RadioGroup>
         <EditText android:layout_width="match_parent" android:layout_height="wrap_content" />
      </RelativeLayout>
      

      还有这段代码 radioButton 是您要附加 EditText 的任何 RadioButton 的变量

      radioButton.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener()
      {
         @Override
         public void onGlobalLayout()
         {
            radioButton.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)editText.getLayoutParams();
            params.leftMargin = radioButton.getLeft() + radioButton.getWidth() + littleSpacing;
            params.topMargin = radioButton.getTop() + radioButton.getBaseline() - editText.getBaseline();
         }
      });
      

      【讨论】:

        【解决方案6】:
        <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:padding="@dimen/dp_8">
        
               <RadioGroup
                    android:id="@+id/rg_ends_after"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:orientation="vertical"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">
        
                    <RadioButton
                        android:id="@+id/rb_until_cancelled_never"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="true"
                        android:text="@string/txt_until_cancelled_never" />
        
                    <RadioButton
                        android:id="@+id/rb_after_occurrence"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="false"
                        android:text="@string/txt_after"
                        app:layout_constraintStart_toStartOf="parent"
                        app:layout_constraintTop_toTopOf="parent" />
                </RadioGroup>
        
                <LinearLayout
                    android:id="@+id/layout_occurrence"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="@+id/rg_ends_after"
                    app:layout_constraintStart_toStartOf="parent">
        
                    <RadioButton
                        android:id="@+id/rb_after_occurrence_dummy"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:checked="false"
                        android:visibility="invisible"
                        android:text="@string/txt_after" />
        
                    <EditText
                        android:id="@+id/et_occurrence"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="center"
                        android:inputType="number"
                        android:maxLength="2"
                        android:ems="2"
                        android:text="@string/number" />
        
                    <TextView
                        android:id="@+id/txt_occurrence"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="bottom"
                        android:text="@string/txt_occurrence" />
        
                </LinearLayout>
        </androidx.constraintlayout.widget.ConstraintLayout>
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-02-18
          • 2023-03-14
          • 1970-01-01
          • 1970-01-01
          • 2014-09-26
          • 2011-05-06
          • 2019-09-22
          • 2017-10-10
          相关资源
          最近更新 更多