更新从MainAcivity.java不同的布局XML一个TextView?布局、不同、java、MainAcivity

由网友(花落半夏)分享简介:我在我的所谓game.xml布局文件夹中创建一个新的.xml文件。它包含一个TextView的。I've created a new .xml file in my layout folder called game.xml. It contains an TextView.是否有可能设置在位于game.xml从我...

我在我的所谓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

阅读全文

相关推荐

最新文章