[英]How to hide next button and show when radio group button is selected
How to I hide the next button and will only show when the user choose a radio group button 如何隐藏下一个按钮,并且仅当用户选择单选按钮时才会显示
I have this XML code 我有这个XML代码
<?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">
<RadioGroup
android:id="@+id/radioSex"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/assembly"
android:checked="false"
/>
<RadioButton
android:id="@+id/csharp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/java"
android:checked="false" />
<Button
android:id="@+id/btnDisplay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:onClick="OnClick"
/>
</RadioGroup>
</LinearLayout>
and this class code 和这个班级代码
package com.example.quiz;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
/**
* Created by leste on 3/5/2016.
*/
public class CS_Category extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton;
private Button btnDisplay;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.cs_category);
addListenerOnButton();
}
public void addListenerOnButton() {
radioSexGroup = (RadioGroup) findViewById(R.id.radioSex);
btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// get selected radio button from radioGroup
int selectedId = radioSexGroup.getCheckedRadioButtonId();
radioSexButton = (RadioButton) findViewById(selectedId);
if (radioSexButton.getId() == R.id.assembly) {
Intent i = new Intent(CS_Category.this, CS_Assembly.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.csharp) {
Intent i = new Intent(CS_Category.this, CS_Csharp.class);
startActivity(i);
} else {
if (radioSexButton.getId() == R.id.java) {
Intent i = new Intent(CS_Category.this, CS_Java.class);
startActivity(i);
}
}
Toast.makeText(CS_Category.this,
radioSexButton.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
}
What should I do to hide the next button and show only if there is a selected radio button 我应该怎么做才能隐藏下一个按钮并仅在选中单选按钮时显示
In your button xml you should use: 在您的按钮xml中,您应该使用:
android:visibility="gone"
机器人:能见度=“水涨船高”
In java you should use: 在Java中,您应该使用:
btnDisplay.setVisibility(View.VISIBLE);
btnDisplay.setVisibility(View.VISIBLE);
First of all I would take the button out of the radio group and just have it be below it. 首先,我将按钮从广播组中移出并放在其下方。 Then in my java code I would have this function:
然后在我的Java代码中,我将具有以下功能:
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.assembly:
if (checked)
make_button_visible();
break;
case R.id.csharp:
if (checked)
make_button_visible();
break;
}
}
Make sure that each radio button has onClick="onRadioButtonClicked" set. 确保每个单选按钮都设置了onClick =“ onRadioButtonClicked”。
Then have a function called make_button_visible() that has these lines: 然后有一个名为make_button_visible()的函数,其中包含以下几行:
Button mButton=(Button)findViewById(R.id.btnDisplay);
mButton.setVisibility(View.VISIBLE);//This will make it visible
add below code in your onCreate method 在onCreate方法中添加以下代码
addListenerOnButton();
btnDisplay.setVisibility(View.INVISIBLE);
radioSexGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup radioGroup, int i) {
if (btnDisplay.getVisibility() == View.INVISIBLE)
btnDisplay.setVisibility(View.VISIBLE);
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.