在另一级Java编辑文本框 [英] java edit textfield in another class

查看:182
本文介绍了在另一级Java编辑文本框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有A类和B类A类要求在这里B类:

I have class A and class B. Class A calls class B here:

public void onClick(View v){
    //respond to clicks
    new MyTask().execute();

在这个新的任务,我做了一些网络垃圾,并最终找到了我要传回给A类的字符串。

In this new task, I do some network junk, and end up finding a string that I want to pass back to class A.

但我似乎无法做到这一点。我试图做类似classA.textfield.setText(文本),但没有奏效。它看起来像类不能返回值,无论是。我见过几个标记为类似的事情,但我不认为他们解决我的问题,如果他们这样做,我不知道如何实现它们。我喜欢这种感觉必须有一个超级简单的方法来做到这一点,但我唯一见过的提示声音甚至远程类似的是使一类的新版本,但听起来可怕的错误...

But I can't seem to do it. I tried to do something like classA.textfield.setText(text), but that didn't work. And it looks like classes can't return values, either. I've seen a few of the thing marked as similar, but I don't think they solve my issue, and if they do, I didn't understand how to implement them. I feel like this must have a super simple way to do it, but the only thing I've seen suggested that sound even remotely similar is making a new version of Class A, but that sounds horribly wrong...

推荐答案

这是其中回调的ActionListener 编码模式显示它的实力。

This is where the Callback or ActionListener coding pattern shows it's strength.

在您MyTask类的类声明之后补充一点:

In your MyTask class add this after the class declaration:

public interface MyTaskActionListener{
    public void didFinish(String result);
}

成员加入您的MyTask类:

Add a member to your MyTask class:

private MyTaskActionListener listener;

现在,添加一个方法的setActionListener 来MyTask:

Now, add a method setActionListener to MyTask:

public void setActionListener(MyTaskActionListener l){
    listener = l;
}

接下来,在开始之前MyTask,设置监听器:

Next, before starting the MyTask, set the listener:

MyTask task = new MyTask();
task.setActionListener(new MyTaskActionListener(){
    public void didFinish(String result){
        yoirTextView.setText(result);
    }
});

最后但并非最不重要的,在MyTask的onPostExecute调用didFinish():

Last but not least, call didFinish() in MyTask's onPostExecute:

protected void onPostExecute(String result){
    listener.didFinish(result);
}

这篇关于在另一级Java编辑文本框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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