简体   繁体   English

如何从另一个更新TextView

[英]How to update a TextView from another

I am new to android/java programming. 我是android / java编程的新手。 I have two class, one is an activity and other normal class. 我有两个班,一个是活动班,另一个是普通班。 In my activity class contains TextView. 在我的活动类中包含TextView。 Can i update my TextView of one class from a editText(that the user enters) in a another class. 我可以从另一类的editText(用户输入的内容)更新一个类的TextView吗? I tried with random code, but it fails. 我尝试使用随机代码,但失败了。 Please help I've been looking forever 请帮助我一直在寻找

You might update the TextView from wherever in the java code by referring to the 您可以通过引用Java代码中的任何位置来更新TextView。

findViewById(R.id.some_text_view_name) . findViewById(R.id.some_text_view_name)

Some what like this: 一些像这样的东西:

TextView textViewName = (TextView) findViewById(R.id.some_text_view_name);
textViewName.methodName();

Here methodName() refers to the Public methods listed here 这里methodName()引用这里列出公共方法

Hope it helps. 希望能帮助到你。 :) :)

You can start your second activity using startActivityForResult() instead of startActivity() . 您可以使用startActivityForResult()而不是startActivity()启动第二个活动。 In the second activity you can set the result and its status using setResult() and get back to previous activity (via back press or something). 在第二个活动中,您可以使用setResult()设置结果及其状态,然后返回上一个活动(通过按回车键或其他方式)。 In the first activity, this result will be received in onActvityResult() . 在第一个活动中,此结果将在onActvityResult()接收。 From here, you can get the data set by second activity and update your textview. 在这里,您可以通过第二个活动获取数据集并更新您的textview。

This is the gist of what you are supposed to do. 这就是您应该做的要点。 You can get code example here , here and here . 您可以在此处此处此处获取代码示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM