在java类中使用string.xml资源更改textView
我不想用string.xml中的字符串值设置TextView的文本在java类中使用string.xml资源更改textView,java,android,xml,Java,Android,Xml,我不想用string.xml中的字符串值设置TextView的文本 <resources> <string name="t1"> hi </string> <string name="t2"> hello </string> <string name="t3"> ok </string> </resources> 所以我想用java类中的switch case来实现这一点 但它无法在我的文本视图上设
<resources>
<string name="t1"> hi </string>
<string name="t2"> hello </string>
<string name="t3"> ok </string>
</resources>
所以我想用java类中的switch case来实现这一点
但它无法在我的文本视图上设置文本,当我运行应用程序时,文本视图为空
抱歉,如果这是一个简单的愚蠢错误:)
这是我的布局
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="@drawable/shape">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:textColor="@color/txt_dialog"
android:textSize="25sp"/>
</RelativeLayout>
检查字符串资源的名称,它们是a1系列还是t1系列
public void hi (){
TextView txt_truth = (TextView) findViewById(R.id.textview);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
Random random = new Random();
int hi_random = random.nextInt(45) + 1;
switch (hi_random){
case 1:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a1));
}
break;
case 2:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a2));}
break;
case 3:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a3));}
break;}
dialog.show();
}
检查字符串资源的名称,它们是a1系列还是t1系列
public void hi (){
TextView txt_truth = (TextView) findViewById(R.id.textview);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.mylayout);
Random random = new Random();
int hi_random = random.nextInt(45) + 1;
switch (hi_random){
case 1:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a1));
}
break;
case 2:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a2));}
break;
case 3:
if (txt_truth != null) {
txt_truth.setText(getResources().getString(R.string.a3));}
break;}
dialog.show();
}
它无法在我的textView上设置文本,当我运行应用程序时,textView
是空的
string.xml
只有三个字符串要在TextView中随机显示,但您正在使用45
获取随机数,该随机数生成范围[0-45]+1
中的数字
获取介于1到3之间的数字,如下所示:
int hi_random = random.nextInt(2) + 1;
它无法在我的textView上设置文本,当我运行应用程序时,textView
是空的
string.xml
只有三个字符串要在TextView中随机显示,但您正在使用45
获取随机数,该随机数生成范围[0-45]+1
中的数字
获取介于1到3之间的数字,如下所示:
int hi_random = random.nextInt(2) + 1;
您必须以这种方式从对话框视图中获取textview的id
View view = LayoutInflater.from(this).inflate(R.layout.mylayout, null);
dialog.setContentView(view);
TextView txt_truth = (TextView) view.findViewById(R.id.textview);
您必须以这种方式从对话框视图中获取textview的id
View view = LayoutInflater.from(this).inflate(R.layout.mylayout, null);
dialog.setContentView(view);
TextView txt_truth = (TextView) view.findViewById(R.id.textview);
在我看来,你的随机发生器是个错误。您已将上限值指定为45 nextInt(int n)方法用于从该随机数生成器的序列中获取一个伪随机、均匀分布的int值,该值介于0(包含)和指定值(排除)之间
int hi_random = random.nextInt(3) + 1;
请尝试上面的代码,并对其是否有效进行注释
您还可以查看nextInt上的java文档:在我的头顶上,我认为错误在于您的随机生成器。您已将上限值指定为45 nextInt(int n)方法用于从该随机数生成器的序列中获取一个伪随机、均匀分布的int值,该值介于0(包含)和指定值(排除)之间
int hi_random = random.nextInt(3) + 1;
请尝试上面的代码,并对其是否有效进行注释
您还可以在nextInt上查看java文档:显而易见:
txt\u truth.setText(getResources().getString(R.string.t1))
但是你的字符串名为a1,a2,…
,而不是t1,t2,…
很抱歉,在我写这篇文章时发生了这个错误,我现在也编辑了,如果你是基于这个:int hi_random=random.nextInt(45)+1代码>很可能您得到的数字超出了您的开关范围。很抱歉:)我从大型项目复制了代码,并试图使其更简单,但我遗漏了一些明显的代码:txt_truth.setText(getResources().getString(R.string.t1))
但是你的字符串名为a1,a2,…
,而不是t1,t2,…
很抱歉,在我写这篇文章时发生了这个错误,我现在也编辑了,如果你是基于这个:int hi_random=random.nextInt(45)+1代码>很可能您得到的数字超出了切换范围。非常抱歉:)我从大型项目中复制了代码,并试图使其更简单,但我错过了其中一些随机。nextInt(3)+1
这将使int为1,2,3,4
,但4
不在其下界包含和上界独占的切换情况下。我在回答中提到过这一点。nextInt(3)+1
这将给出int作为1,2,3,4
,但4
不是在其下界包含和上界独占的切换情况下。我在报告中提到过这一点answer@MohsenMohseni:ctx表示上下文,您可以传递它,具体取决于您所在的位置using@MohsenMohseni:ctx表示上下文,您可以传递它,具体取决于您使用的位置