简体   繁体   English

使用在另一个 class 中调用的方法设置 edittext 文本

[英]setting edittext text with a method called in an another class

i have the edittext initialized in the activity like:我在活动中初始化了edittext,例如:

EditText date;
DateDialog date_class;

protected void onCreate(Bundle savedInstanceState) {
        date = findViewById(R.id.date_input);
        date.setOnClickListener(this);
        date_class = new DateDialog();
........
}

and at onclicklistner:在 onclicklistner 上:

public void onClick(View v) {
.....
date_class.SetDateString();
.....
}

the SetDateString() method in the other class (DateDialog) is:另一个 class (DateDialog) 中的 SetDateString() 方法是:

public void SetDateString() {
String name = "bla bla bla";
Activity act= new Activity();//defining the activity object
act.setdate(name);//sends the string to the activity's method where it will set a text for the edittext
}

so it sends a string with act.setdate(name) method which is defined in the activity like:所以它发送一个带有 act.setdate(name) 方法的字符串,该方法在活动中定义,如:

public void setdate(String name){
        date.setText(name);// sets a text for the edittext
    }

i have simplified the code for you to explain the point, I'm using other class to do some job on this activity but it unfortunately returns this error:我已经为您简化了代码来解释这一点,我正在使用其他 class 来完成这项活动,但不幸的是它返回了这个错误:

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.EditText.setText(java.lang.CharSequence)' on a null object reference

This approach is the opposite of standard data flow and even if you got it to work it could lead to thread problems down the line.这种方法与标准数据流相反,即使你让它工作,它也可能导致线程问题。 You should never be trying to manipulate the UI from any class other than itself.您不应该尝试从除自身之外的任何 class 操作 UI。

If i am inferring correctly, you want to click on a text box, open a dialog, do something in a dialog, and then update your initial activity?如果我推断正确,您想单击文本框,打开对话框,在对话框中执行某些操作,然后更新您的初始活动? This is a situation where you want to use an Interface.这是您要使用接口的情况。

Define an interface at the bottom of your activity, outside of the enclosing class for now.在活动的底部定义一个接口,现在在封闭的 class 之外。

interface DialogCallback{
    String doSomething(String name);
}

In your DateDialog class add a variable在您的 DateDialog class 添加一个变量

private DialogCallback dialogCallback;  

In your main activity add this to your class initialization:在您的主要活动中,将此添加到您的 class 初始化中:

implements DialogCallback  

Do the ALT+Enter and implement the method doSomething(String name).执行 ALT+Enter 并实现方法 doSomething(String name)。 When you initialize the DateDialog class, you need to set the callback variable, either by doing it in the init call or as a separate setter in the DateDialog class.当您初始化 DateDialog class 时,您需要设置回调变量,方法是在 init 调用中或作为 DateDialog class 中的单独设置器。

void setCallback(DialogCallback callback){
    this.dialogCallback = callback;
}

Then set it after you init the class:然后在你初始化 class 之后设置它:

date_class = new DateDialog();
date_class.setCallback(this);

Finally, change your function from最后,将您的 function 从

public void SetDateString() {
    String name = "bla bla bla";
    Activity act= new Activity();//defining the activity object
    act.setdate(name);//sends the string to the activity's method where it will  set a text for the edittext
}

to

public void setDateString(){
    String name = "foo";
    dialogCallback.doStuff(name);
}

Now it will pass that string back your main class to the "DoStuff(String name)" function that was implemented and you use that to update the UI.现在它将将该字符串传回您的主要 class 到已实现的“DoStuff(字符串名称)” function,您可以使用它来更新 UI。
Additionally, your variables should be like "dateClass" and not "date_class" and functions should not start with uppercase letters.此外,您的变量应该像“dateClass”而不是“date_class”,并且函数不应以大写字母开头。
As far as to why the code as is does not work, my guess would be Activity = new Activity().至于为什么代码不起作用,我的猜测是 Activity = new Activity()。 It makes no sense, you are not creating an instance of any specific class, even if you were, it would be "new" meaning any previously defined ui elements would not be accessible.这没有任何意义,您没有创建任何特定 class 的实例,即使您是,它也将是“新的”,这意味着将无法访问任何先前定义的 ui 元素。

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

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