Java 像radioButton一样在android中选择性别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23291450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select gender in android like radioButton
提问by enrm
I'm making a simple program where I want to be able to select my gender much like a radio-button behaviour, you either check female or male, and the other choice is supposed to be unchecked. I have separate images for the buttons, both pressed and not pressed.
我正在制作一个简单的程序,我希望能够像单选按钮行为一样选择我的性别,您可以选中女性或男性,而另一个选择应该是未选中的。我有单独的按钮图像,按下和未按下。
I've stored the states in a selector file:
我已将状态存储在选择器文件中:
button_female.xml:
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>
And in layout/settings_activity.xml:
在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"
/>
And in the activity, the onClick is written as
在活动中,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;
}
}
However, this doesn't work for several reasons..
但是,由于多种原因,这不起作用..
1: The pictures on the buttons don't show up, either pressed or unpressed.
1: 按钮上的图片不显示,按下或未按下。
2: My code is bad, the buttons don't work as expected. I would like to have them behave as a radio group (you either choose male or female as your gender)..
2: 我的代码不好,按钮不能按预期工作。我想让他们表现得像一个广播小组(你选择男性或女性作为你的性别)。
So I seem to have two problems here. Does anyone know a good way to solve this? I'm very new to writing android apps..
所以我在这里似乎有两个问题。有谁知道解决这个问题的好方法?我对编写 android 应用程序很陌生..
采纳答案by Phant?maxx
If you need something like this:
如果你需要这样的东西:
You can use this layout:
您可以使用此布局:
<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>
回答by Akanksha Hegde
You can instead use a radioGroup in xml as below :
您可以改为在 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>
回答by Miguel Maciel
You have here a great example for what you need:
你在这里有一个很好的例子,你需要什么:
http://www.mkyong.com/android/android-radio-buttons-example/
http://www.mkyong.com/android/android-radio-buttons-example/
The layout:
布局:
<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>
And for controling:
并用于控制:
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();
}
});
}