【发布时间】:2021-09-15 14:36:05
【问题描述】:
我以编程方式将几个RadioButtons 添加到预先存在的RadioGroup。按钮本身就在那里并且是可点击的,但它们的文本不会出现。我已经使用setText 方法设置了它们的文本,并且通过使用Toast 和RadioButton 的getText 方法,我能够确认所有按钮都拥有它们应该拥有的文本——它们只是没有'不显示它们。
我确保将它们的 alpha 设置为 1,将文本大小设置为 20sp,将它们的可见性设置为 View.Visible,我尝试将文本颜色和背景颜色更改为对比色几次,但文本仍然没有t出现。
有人可以帮我弄清楚如何使文本出现吗?
Java 代码:
for(int i = 0; i < CERTAIN_CONSTANT_VALUE; i++)
{
RadioButton r = new RadioButton(this);
android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
r.setLayoutParams(params);
r.setId(View.generateViewId());
r.setText(answers[i]);
r.setOnClickListener(this::onBtnClicked);
r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
r.setTextColor(Color.BLACK);
r.setAlpha(1);
r.setVisibility(View.VISIBLE);
r.setBackgroundColor(Color.GREEN);
this.radioGroup.addView(r);
}
XML 文件中的RadioGroup:
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="200dp">
</RadioGroup>
最小的、可重现的示例
Java:
import androidx.appcompat.app.AppCompatActivity;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup radioGroup = findViewById(R.id.radioGroup);
String[] answers = {"first", "second", "third", "fourth"};
for (int i = 0; i < 4; i++)
{
RadioButton r = new RadioButton(this);
android.widget.LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
r.setLayoutParams(params);
r.setId(View.generateViewId());
r.setText(answers[i]);
r.setOnClickListener(this::onBtnClicked);
r.setTextSize(20, TypedValue.COMPLEX_UNIT_SP);
r.setTypeface(Typeface.SANS_SERIF, Typeface.BOLD);
r.setTextColor(Color.BLACK);
r.setAlpha(1);
r.setVisibility(View.VISIBLE);
r.setBackgroundColor(Color.GREEN);
radioGroup.addView(r);
}
}
public void onBtnClicked(View view){}
}
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
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:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<RadioGroup
android:id="@+id/radioGroup"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RadioGroup>
</LinearLayout>
【问题讨论】:
-
你确定 answers[i] 有值吗?对其调用 getText 是否返回正确的值?
-
这能回答你的问题吗? Dynamically Change the Text of RadioButton
-
@GabeSechan 我确定 answers[i] 有一个值,因为“敬酒”它会显示预期值。
-
@ShabbirHussain 遗憾的是,它没有。我相信我的情况是不同的。我仍然尝试像该帖子中建议的答案那样迭代按钮,但它没有改变任何东西。
-
你能提供一个minimal reproducible example吗?这段代码应该可以工作。也许
CERTAIN_CONSTANT_VALUE不正确?
标签: java android xml android-radiobutton