如何从班级中获取EditText
?我无法从EditText.getText().toString()
获取字符串。它是null。
public class SignUpModel {
String email;
Context context;
public SignUpModel(Context context) {
this.context = context;
}
public String getEmail() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.activity_sign_up, null);
EditText signup_email =(EditText) layout.findViewById(R.id.signup_email);
email = signup_email.getText().toString();
Log.v("email",signup_email.getText().toString());
return email;
}
}
答案 0 :(得分:0)
只需创建全局EditText
变量,然后获取它。但是,在使用它之前检查null。
public class SignUpModel {
String email;
Context context;
private EditText signup_email;
public SignUpModel(Context context) {
this.context = context;
}
public String getEmail() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout=inflater.inflate(R.layout.activity_sign_up, null);
signup_email =(EditText) layout.findViewById(R.id.signup_email);
email = signup_email.getText().toString();
Log.v("email",signup_email.getText().toString());
return email;
}
public EditText getEmailEditText() {
return signup_email;
}
}
答案 1 :(得分:0)
只需将字符串设为public static并在您的类中访问它.Put on editText更改侦听器并重新初始化您的字符串。希望它能为您提供帮助。