Android 如何从其他类获取文本视图
假设我有一个MainActivity类。这个类有一个TextView。 然后我编写了一个新类来处理这个TextView。 如何从MainActivity获取要处理的TextView。 谢谢大家!Android 如何从其他类获取文本视图,android,Android,假设我有一个MainActivity类。这个类有一个TextView。 然后我编写了一个新类来处理这个TextView。 如何从MainActivity获取要处理的TextView。 谢谢大家! public class MainActivity extends Activity { TextView txtMsg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedIn
public class MainActivity extends Activity {
TextView txtMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
new BackgroundAsyncTask(getApplicationContext());}
另一类
public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{
TextView textView;
Context context;
public BackgroundAsyncTask(Context context){
this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
textView = (TextView) context.findViewById(R.id.txtMsg); //here has an error }
public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{
TextView textView;
Activity context;
public BackgroundAsyncTask(Activity context){
this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
textView = (TextView) context.findViewById(R.id.txtMsg); //no errors }
public类BackgroundAsyncTask扩展了AsyncTask{
文本视图文本视图;
语境;
公共背景异步任务(上下文){
this.context=上下文;
}
@凌驾
受保护的Void doInBackground(整数…参数){
//TODO自动生成的方法存根
textView=(textView)context.findViewById(R.id.txtMsg);//此处有一个错误}
如果要处理TextView
值,可以通过以下方式传递该值:
public class BackgroundAsyncTask extends AsyncTask<String, String, Void>{
TextView textView;
Context context;
public BackgroundAsyncTask(Context context){
this.context = context;
}
@Override
protected Void doInBackground(String... params) {
String textBoxValue= params[0];
}
将类BackgroundAsyncTask的构造函数方法更改为:
public BackgroundAsyncTask(Context context, TextView textView){
this.context = context;
this.textView = textView;
}
在onCreate()方法中:
有很多方法。我在这里解释两种方法 1)在MainActivity.class instaread中编写BackgroundAsyncTask类,以避免创建新类,从而轻松使用TextView对象 2)使用BackgroundAsyncTask构造函数的参数传递TextView对象 像这样 从主活动中调用BackgroundAsyncTask
new BackgroundAsyncTask(MainActivity.this, textView);
您的背景任务构造函数
public BackgroundAsyncTask(Context context, TextView textview){
this.context = context;
this.textview = textview;
}
在应用程序外部创建全局实例
public class CommonsData{
public static TextView tvYourTextView;
}
在您希望声明它的类中使用此声明
CommonsData.tvYourTextView = (TextView) findViewById(R.id.txtMsg);
并在应用程序范围内的任何地方使用,使用前不要忘记添加空检查。我不知道为什么需要在doInBackground方法中使用textView。因为doInBackground方法没有访问UI线程的权限!!!
使用onPostExecute
中的所有UI元素调用,on重新执行
,onProgressUpdate
。
findViewById
是一种活动方法。因此,请改用此方法
public class MainActivity extends Activity {
TextView txtMsg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtMsg = (TextView) findViewById(R.id.txtMsg);
new BackgroundAsyncTask(this);}
另一类
public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{
TextView textView;
Context context;
public BackgroundAsyncTask(Context context){
this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
textView = (TextView) context.findViewById(R.id.txtMsg); //here has an error }
public class BackgroundAsyncTask extends AsyncTask<Integer, String, Void>{
TextView textView;
Activity context;
public BackgroundAsyncTask(Activity context){
this.context = context;
}
@Override
protected Void doInBackground(Integer... params) {
// TODO Auto-generated method stub
textView = (TextView) context.findViewById(R.id.txtMsg); //no errors }
public类BackgroundAsyncTask扩展了AsyncTask{
文本视图文本视图;
活动语境;
公共背景异步任务(活动上下文){
this.context=上下文;
}
@凌驾
受保护的Void doInBackground(整数…参数){
//TODO自动生成的方法存根
textView=(textView)context.findViewById(R.id.txtMsg);//无错误}
为什么需要在BackgroundAsyncTask
中使用textView?如果需要更改textView中的任何文本,最好从BackgroundAsyncTask返回值,尝试将textView内容传递给process,而不是textView引用。