更新从MainAcivity.java不同的布局XML一个TextView? [英] Updating a textview in different layout xml from the MainAcivity.java?

查看:112
本文介绍了更新从MainAcivity.java不同的布局XML一个TextView?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的所谓game.xml布局文件夹中创建一个新的.xml文件。它包含一个TextView的。

I've created a new .xml file in my layout folder called game.xml. It contains an TextView.

是否有可能设置在位于game.xml从我的主要活动TextView的文字?

Is it possible to set the text on the textview located in the game.xml from my main activity?

game.xml(TextView的部分)

game.xml (Textview part)

<TextView
    android:id="@+id/tV1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_centerVertical="true"
    android:text="TextView" />

MainActivity.java

MainActivity.java

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setImageArray();
    String actualword = chooseWord(words);
    createLetterArray(actualword);
    TextView textView1 = (TextView) findViewById(R.id.tV1); //get a reference to the textview on the game.xml file.
    textView1.setText(actualword);

}
    ...

我尝试了这种方式。但是,这是行不通的。任何人谁可以帮我?

I tried it this way. But it doesn't work. Anyone who can help me ?

推荐答案

您不能设置一个视图,的TextView 在这里,还没有被创造的膨胀了的通过吹气或布局的setContentView()。您可以通过传递值的意图来将实际使用类的TextView

You can't set a view, TextView here, that hasn't been created by inflating the layout through an inflater or setContentView(). You can pass the value through an Intent to the class that will actually use the TextView

Intent intent = new Intent(MainActivity.this, NextActivity.class); 
intent.putExtra("key", actualword);
startActivity(intent);

其中, NextActivity 是将显示的TextView活动

Where NextActivity is the activity that will display the textview

然后你就可以得到该值,并将其设置在其他活动

Then you can get that value and set it in your other activity

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()你的第二个活动

After you have used setContentView(R.layout.game); in onCreate() of your second activity

这篇关于更新从MainAcivity.java不同的布局XML一个TextView?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆