Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 是否从MainAcivity.java以不同的布局xml更新textview?_Android_Android Layout_Android Emulator_Android Widget_Textview - Fatal编程技术网

Android 是否从MainAcivity.java以不同的布局xml更新textview?

Android 是否从MainAcivity.java以不同的布局xml更新textview?,android,android-layout,android-emulator,android-widget,textview,Android,Android Layout,Android Emulator,Android Widget,Textview,我在布局文件夹game.xml中创建了一个新的.xml文件。它包含一个文本视图 是否可以从我的主要活动中设置game.xml中的textview上的文本 game.xml(文本视图部分) 我这样试过了。但它不起作用。有谁能帮我吗?您不能在此处设置视图,TextView,该视图不是通过充气器或setContentView()充气布局创建的。您可以通过Intent将值传递给实际使用TextView Intent intent = new Intent(MainActivity.this, NextA

我在布局文件夹game.xml中创建了一个新的.xml文件。它包含一个文本视图

是否可以从我的主要活动中设置game.xml中的textview上的文本

game.xml(文本视图部分)


我这样试过了。但它不起作用。有谁能帮我吗?

您不能在此处设置视图,
TextView
,该视图不是通过充气器或
setContentView()
充气布局创建的。您可以通过
Intent
将值传递给实际使用
TextView

Intent intent = new Intent(MainActivity.this, NextActivity.class); 
intent.putExtra("key", actualword);
startActivity(intent);
其中
NextActivity
是将显示文本视图的活动

然后您可以获取该值并在其他活动中设置它

Intent recIntent = getIntent();
String text = recIntent.getStringExtra("key");
TextView textView1 = (TextView) findViewById(R.id.tV1); //get a reference to the textview on the game.xml file.
textView1.setText(text);
使用
setContentView(R.layout.game)后onCreate()
中的code>,

简短回答,否

布局文件只是一个蓝图。直到布局膨胀(变成视图),实际的文本视图才存在

我的问题是,(如何)您没有使用这个game.xml吗?是在另一个活动中吗

Intent intent = new Intent(MainActivity.this, NextActivity.class); 
intent.putExtra("key", actualword);
startActivity(intent);
Intent recIntent = getIntent();
String text = recIntent.getStringExtra("key");
TextView textView1 = (TextView) findViewById(R.id.tV1); //get a reference to the textview on the game.xml file.
textView1.setText(text);