我想创建动态无线电组(与动态无线电选项)。我正在使用下面的代码。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="@+id/linear_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/button">
</LinearLayout>
<TextView
android:id="@+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toTopOf="@+id/button"
android:textColor="#008b00"
/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Check Answer"
app:layout_constraintBottom_toBottomOf="parent"
android:onClick="checkAnswer"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
主要活动:
package com.example.quiz8;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private LinearLayout linearLayout = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
RadioGroup radioGroup = new RadioGroup(this);
RadioButton radioButton;
final String[] title = {"Male", "Female", "Other", "Secret"};
for (int i = 0; i < title.length; i++) {
radioButton = new RadioButton(this);
radioButton.setId(i);
radioButton.setText(title[i]);
radioGroup.addView(radioButton);
}
linearLayout.addView(radioGroup);
}
public void checkAnswer(View view) {
//get the answer here
TextView answerText = (TextView) findViewById(R.id.answer);
answerText.setText("You selected the option...");
}
}
您可以将标记设置为radioGroup
以便能够轻松地找到它。标记可以是任何对象
。这里我们将使用字符串
,该字符串在viewgroup
中必须是唯一的,对其调用FindViewWithTag()
。然后在checkanswer()
中,通过标记检索radioGroup
,从而获得选中的radioButton
的文本。
private static final String RADIOGROUP_TAG = "radiogroup";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setTag(RADIOGROUP_TAG);
RadioButton radioButton;
final String[] title = {"Male", "Female", "Other", "Secret"};
for (int i = 0; i < title.length; i++) {
radioButton = new RadioButton(this);
radioButton.setId(i);
radioButton.setText(title[i]);
radioGroup.addView(radioButton);
}
linearLayout.addView(radioGroup);
}
public void checkAnswer(View view) {
//get the answer here
RadioGroup radioGroup = linearLayout.findViewWithTag(RADIOGROUP_TAG);
int checkedId = radioGroup.getCheckedRadioButtonId();
RadioButton radioButton = radioGroup.findViewById(checkedId);
String text = radioButton.getText().toString();
TextView answerText = (TextView) findViewById(R.id.answer);
answerText.setText("You selected the option..." + text);
}
另一个选项:您可以将字符串[]title
和radioGroup
作为活动
中的字段。这是有意义的,因为您需要它们在不止一个地方。
private final String[] title = {"Male", "Female", "Other", "Secret"};
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
radioGroup = new RadioGroup(this);
RadioButton radioButton;
for (int i = 0; i < title.length; i++) {
radioButton = new RadioButton(this);
radioButton.setId(i);
radioButton.setText(title[i]);
radioGroup.addView(radioButton);
}
linearLayout.addView(radioGroup);
}
public void checkAnswer(View view) {
//get the answer here
int checkedId = radioGroup.getCheckedRadioButtonId();
TextView answerText = (TextView) findViewById(R.id.answer);
// Note: make sure there is a checked RadioButton!
answerText.setText("You selected the option..." + title[checkedId]);
}
如何获得文本而不是值,当我点击单选按钮? 下面的代码获取单选按钮的值。我想接短信(节目或电影)。提前道谢。 null null
本文向大家介绍jQuery获取选中单选按钮radio的值,包括了jQuery获取选中单选按钮radio的值的使用技巧和注意事项,需要的朋友参考一下 实例1: 获取一组单选按钮对象:var obj_payPlatform = $('#wrap input[name="payMethod"]'); 获取被选中按钮的值 :var val_payPlatform = $('#wrap inpu
问题内容: 我已经在单选组中动态创建了两个单选按钮,并选中其中一个。我需要在我按下另一个按钮时将其值保存在字符串中。但是我已经为此实现了,但是第一次没有用。这是我的代码。 问题答案: xml文件 Java代码
问题内容: 我有一段代码,其中一个包含三个s 。我想设置的,将显示的值的。但是,到目前为止我没有得到任何结果。如何获得的值并在中显示Toast?预先谢谢你,这是我的代码: XML档案: 问题答案: 经过测试和工作。检查一下 XML文件
问题内容: 我有两个单选按钮和。 如何获取所选单选按钮的值。 我是否需要使用ng-model或其他东西。在jquery中,我知道Angularjs中没有的东西。 问题答案: 双方应该有相同与不同 (意味着有选择使用S或按钮),使所选择的值将在修改$ scope变量,你可以抓住的形式的控制器内部的值或者提交按钮点击。 标记
问题内容: 在使用jQuery提交AJAX表单后,我想取消选中单选按钮。我有以下功能: 借助此功能,我可以清除文本框中的值,但不能清除单选按钮的值。 顺便说一句,我也尝试过,但是那没用。 问题答案: 要么(普通js) 或(jQuery)
我有两类单选按钮,比如 蛋糕名称 -接下来是花 现在问题是我的底价是200。只要我选择第一个单选按钮价格就会增加到 接下来,如果我选择值为100的花列,价格应该增加到。这应该是动态发生的。有人能帮我吗?
我的网页上有一个表单,如下所示: 如何在Javascript中提取当前选定单选按钮(没有提交操作)的值?我想要的是以下几点: