简体   繁体   English

如何将Radio Group添加到此XML文件夹

[英]How to add Radio Group To this XML FIle

I just created a 2 radiobutton In XML FIle All I need too add a radiogroup and put this two radio buttons inside the radiogroup but with the same VIew here's my xml file 我只是在XML文件中创建了一个2 radiobutton所有我需要添加一个radiogroup并将这两个单选按钮放在radiogroup但是使用相同的radiogroup这里是我的xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:background="#FFFFFF"
    android:layout_height="match_parent"
    tools:context="mediaclub.app.paymob.GameeyaFragment">

    <de.hdodenhof.circleimageview.CircleImageView
        android:id="@+id/profileImage"
        android:layout_width="70dp"
        android:layout_height="70dp"
        android:layout_marginTop="28dp"
        app:srcCompat="@android:drawable/sym_def_app_icon"
        android:src="@mipmap/ic_launcher"
        android:layout_alignParentTop="true"
        app:civ_border_color="@color/dark"
        android:layout_alignStart="@+id/amountEt" />

    <EditText
        android:id="@+id/gam3eyaNmeET"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName"
        android:hint="Gam3eya Name"
        android:layout_alignTop="@+id/profileImage"
        android:layout_toRightOf="@+id/profileImage"
        android:layout_toEndOf="@+id/profileImage"
        android:layout_marginTop="13dp" />

    <EditText
        android:id="@+id/amountEt"
        android:layout_width="330dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="39dp"
        android:ems="10"
        android:hint="Amount"
        android:backgroundTint="#8F8E8F"
        android:layout_below="@+id/uploadImgId"
        android:layout_centerHorizontal="true">

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="EGP"
        android:textSize="20dp"
        android:textColor="#1A99C4"
        android:layout_marginEnd="23dp"
        android:layout_alignBaseline="@+id/amountEt"
        android:layout_alignBottom="@+id/amountEt"
        android:layout_alignEnd="@+id/amountEt" />

    <TextView
        android:id="@+id/uploadImgId"
        android:layout_width="wrap_content"
        android:textSize="10dp"
        android:layout_height="wrap_content"
        android:textColor="#1A99C4"
        android:text="Upload Image"
        android:layout_below="@+id/profileImage"
        android:layout_alignEnd="@+id/profileImage"
        android:layout_marginEnd="17dp" />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:id="@+id/ll"
        android:layout_weight="15"
        android:layout_below="@+id/amountEt"
        android:orientation="horizontal"
        android:weightSum="2">

        <RadioButton
            android:id="@+id/monthlyRb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:buttonTint="#1A99C4"
            android:layout_marginLeft="60dp"
            android:text="Monthly"
            android:layout_weight="1"
            android:layout_centerVertical="true"
            android:layout_alignStart="@+id/amountEt" />

        <RadioButton
            android:id="@+id/weeklyRadioBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:buttonTint="#1A99C4"
            android:layout_weight="1"
            android:text="Weekly"
            android:layout_alignTop="@+id/monthlyRb" />
    </LinearLayout>
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:id="@+id/rl"
        android:layout_weight="15"
        android:layout_below="@+id/ll"
        android:orientation="horizontal"
        android:weightSum="2">

        <TextView
            android:id="@+id/textView5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="15dp"
            android:textColor="#000000"
            android:text="Reminder"
            android:layout_marginStart="56dp"
            android:layout_alignBaseline="@+id/textView6"
            android:layout_alignBottom="@+id/textView6"
            android:layout_alignParentStart="true" />

        <Spinner
            android:id="@+id/reminderSpinner1"
            android:layout_width="45dp"
            android:textAlignment="2"
            android:layout_height="wrap_content"
            android:layout_marginStart="15dp"
            android:layout_alignTop="@+id/textView5"
            android:layout_toEndOf="@+id/textView5" />

        <TextView
            android:id="@+id/textView6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Days"
            android:textSize="15dp"
            android:textColor="#000000"
            android:layout_marginStart="12dp"
            android:layout_centerVertical="true"
            android:layout_toEndOf="@+id/reminderSpinner1" />
    </RelativeLayout>

    <Button
        android:id="@+id/nxtBtn"
        android:layout_width="120dp"
        android:layout_height="wrap_content"
        android:background="@drawable/nextbtn"
        android:layout_marginBottom="62dp"
        android:textColor="#ffffff"
        android:text="Next"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

sorry if any thing is not clear I hope these gonna be understandable 对不起,如果有任何事情不清楚,我希望这些可以理解

Wrap your RadioButtons with RadioGroup in xml: And apply android:orientation="horizontal" in the RadioGroup 总结你的单选按钮RadioGroup的XML:与应用android:orientation="horizontal"RadioGroup

 <LinearLayout
        android:id="@+id/ll"
        android:layout_width="match_parent"
        android:layout_height="75dp"
        android:gravity="center"
        android:layout_below="@+id/amountEt"
        android:layout_weight="15"
        android:orientation="horizontal"
        android:weightSum="2">

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

            <RadioButton
                android:id="@+id/monthlyRb"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignStart="@+id/amountEt"
                android:layout_centerVertical="true"
                android:layout_marginLeft="60dp"
                android:layout_weight="1"
                android:buttonTint="#1A99C4"
                android:text="Monthly" />

            <RadioButton
                android:id="@+id/weeklyRadioBtn"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/monthlyRb"
                android:layout_weight="1"
                android:buttonTint="#1A99C4"
                android:text="Weekly" />
        </RadioGroup>
    </LinearLayout>

1. Add RadioGroup as a container of monthlyRb and weeklyRadioBtn . 1.RadioGroup添加为monthlyRbweeklyRadioBtn的容器。 Use orientation horizontal or vertical as per your needs. 根据您的需要使用horizontalvertical

2. Remove redundant attributes from RadioButtons . 2.RadioButtons删除冗余属性。

Try this: 尝试这个:

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="75dp"
    android:id="@+id/ll"
    android:layout_weight="15"
    android:layout_below="@+id/amountEt"
    android:orientation="horizontal">

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

        <RadioButton
            android:id="@+id/monthlyRb"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:buttonTint="#1A99C4"
            android:layout_marginLeft="16dp"
            android:text="Monthly"
            android:layout_weight="1" />

        <RadioButton
            android:id="@+id/weeklyRadioBtn"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:buttonTint="#1A99C4"
            android:layout_weight="1"
            android:text="Weekly" />
    </RadioGroup>

</LinearLayout>

xml file: xml文件:

<RadioGroup
  android:layout_width="fill_parent"
  android:layout_height="90dp"
  android:weightSum="1"
  android:id="@+id/radioGroup">

  <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="55dp"
     android:text="Male"
     android:id="@+id/radioButton"
     android:layout_gravity="center_horizontal"
     android:checked="false"
     android:textSize="25dp" />

  <RadioButton
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:text="Female"
     android:id="@+id/radioButton2"
     android:layout_gravity="center_horizontal"
     android:checked="false"
     android:textSize="25dp"
     android:layout_weight="0.13" />

Java class: Java类:

 radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);

  btnDisplay=(Button)findViewById(R.id.button);

  btnDisplay.setOnClickListener(new View.OnClickListener() {
     @Override
     public void onClick(View v) {
        int selectedId=radioSexGroup.getCheckedRadioButtonId();
        radioSexButton=(RadioButton)findViewById(selectedId);
        Toast.makeText(MainActivity.this,radioSexButton.getText(),Toast.LENGTH_SHORT).show();
     }
  });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM