【问题标题】:Select gender in android like radioButton在android中选择性别,如radioButton
【发布时间】:2014-06-11 01:40:03
【问题描述】:

我正在制作一个简单的程序,我希望能够像单选按钮行为一样选择我的性别,您可以选择女性或男性,而另一个选择应该是未选中的。我有按钮的单独图像,按下和未按下。

我已将状态存储在选择器文件中:

button_female.xml

<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="@drawable/gender_f"/>
<item android:state_pressed="true" android:drawable="@drawable/gender_f" />
<item android:drawable="@drawable/gender_f_notpressed" />
</selector>

layout/settings_activity.xml 中:

    <Button
        android:layout_width="130dp"
        android:layout_height="130dp"
        android:id="@+id/maleButton"
        android:layout_alignParentTop="true"
        android:layout_marginTop="80dp"
        android:layout_marginLeft="220dp"
        android:drawable="@drawable/button_male"
        android:onClick="onGenderButtonClicked"
        />

而在activity中,onClick写成

   public void onGenderButtonClicked(View view) {
        if(feButton.isPressed()){
            maButton.setEnabled(false);
            radioPressed = true;
        } else if (maButton.isPressed()){
            feButton.setEnabled(false);
            radioPressed = true;
        } else {
            radioPressed = false;
        }

    }

但是,由于多种原因,这不起作用..

1 :按钮上的图片不显示,无论是按下还是未按下。

2 :我的代码错误,按钮无法按预期工作。我想让他们表现得像一个广播组(你可以选择男性或女性作为你的性别)..

所以我在这里似乎有两个问题。有谁知道解决这个问题的好方法?我对编写 android 应用程序很陌生..

【问题讨论】:

  • 请检查这两个例子是否相同:onetwo
  • 你也可以选择分段按钮github.com/vinc3m1/android-segmentedradiobutton
  • @user2450263 谢谢你,但你提到的例子没有提到使用图像按钮。我已经弄乱了可以工作的单选按钮,但我希​​望我自己设计的一些按钮可以像单选按钮一样工作。

标签: java android radio-button


【解决方案1】:

如果你需要这样的东西:

你可以使用这个布局:

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity"
    >
        <RadioGroup
            android:id="@+id/radioGrp"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:background="@drawable/gender_old"
            android:paddingTop="64dp"
            android:paddingLeft="8dp"
            android:paddingRight="8dp"
            >
            <RadioButton
                android:id="@+id/radioM"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="left"
                android:checked="true"
                android:drawableRight="@drawable/male"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Male"
            />
            <RadioButton
                android:id="@+id/radioF"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:checked="false"
                android:drawableRight="@drawable/female"
                android:layout_weight="1"
                android:textSize="14dp"
                android:text="Female"
            />
        </RadioGroup>
</RelativeLayout>

【讨论】:

    【解决方案2】:

    你有一个很好的例子来满足你的需要:

    http://www.mkyong.com/android/android-radio-buttons-example/

    布局:

     <RadioGroup
        android:id="@+id/radioSex"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >
    
        <RadioButton
            android:id="@+id/radioMale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_male" 
            android:checked="true" />
    
        <RadioButton
            android:id="@+id/radioFemale"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/radio_female" />
    
    </RadioGroup>
    

    对于控制:

    private RadioGroup radioSexGroup;
    private RadioButton radioSexButton;
    private Button btnDisplay;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    addListenerOnButton();
    }
    
    public void addListenerOnButton() {
    radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
    btnDisplay = (Button) findViewById(R.id.btnDisplay);
    
    btnDisplay.setOnClickListener(new OnClickListener() {
    
    @Override
    public void onClick(View v) {
    // get selected radio button from radioGroup
    int selectedId = radioSexGroup.getCheckedRadioButtonId();
    // find the radiobutton by returned id
    radioSexButton = (RadioButton) findViewById(selectedId);
    
    Toast.makeText(MyAndroidAppActivity.this, radioSexButton.getText(), Toast.LENGTH_SHORT).show();
    }
    });
    }
    

    【讨论】:

      【解决方案3】:

      您可以改为在 xml 中使用 radioGroup,如下所示:

      <RadioGroup
              android:id="@+id/radioGroup1"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:layout_alignParentLeft="true"
              android:layout_below="@+id/textView1"
              android:layout_marginTop="31dp" >
      
              <RadioButton
                  android:id="@+id/radio0"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/button_female"
                  android:checked="true"
                  android:text="male" />
      
              <RadioButton
                  android:id="@+id/radio1"
                  android:layout_width="wrap_content"
                  android:layout_height="wrap_content"
                  android:background="@drawable/button_female"
                  android:text="female" />
          </RadioGroup>
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2014-03-23
        • 2023-03-29
        • 1970-01-01
        • 1970-01-01
        • 2017-03-12
        • 1970-01-01
        • 1970-01-01
        • 2020-05-20
        相关资源
        最近更新 更多