Android - 从alertbuilder对话框中检索文本输入
我在 xml 文件中定义了一个视图。它包含两个 Edittext 字段(以及文本等其他内容),
我使用 AlertBuilder 触发一个对话框,用户在其中输入文本(例如用户名和密码)到两个 edittext 字段中。当我尝试检索字符串并将它们发送到 Login() 时,两个字符串都为空。到底是怎么回事?
似乎字符串数据没有保存?
这是我在应用程序中显示对话框的时间:
SignInDialog.show(ScreenMain.this,
"Login",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());
}
},
"Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
这是我用来实例化对话框的类:
/* login dialog*/
static class SignInDialog {
public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
if(positive != null && positiveText != null){
builder.setPositiveButton(positiveText, positive);
}
if(negative != null && negativeText != null){
builder.setNegativeButton(negativeText, negative);
}
builder.create().show();
}
}
I've got a View defined in an xml file. It contains two Edittext fields (amongt other things like text)
I use an AlertBuilder to trigger a dialog where a user enters text(such as username and pass) into both edittext fields. When I try to retrieve the strings and send them to Login(), both strings are just null. What is going on?
It seems like somehow the string data isn't saved?
Here's when I show the Dialog in my app:
SignInDialog.show(ScreenMain.this,
"Login",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
LayoutInflater inflater = (LayoutInflater) ScreenMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
LogIn(((EditText) layout.findViewById(R.id.screen_dialog_login_username_edit)).getText().toString(),
((EditText) layout.findViewById(R.id.screen_dialog_login_password_edit)).getText().toString());
}
},
"Cancel",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
Here's a class I use to instantiate a Dialog:
/* login dialog*/
static class SignInDialog {
public static void show(Context context, String positiveText, DialogInterface.OnClickListener positive, String negativeText, DialogInterface.OnClickListener negative){
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.screen_dialog_login, null);
builder = new AlertDialog.Builder(context);
builder.setView(layout);
if(positive != null && positiveText != null){
builder.setPositiveButton(positiveText, positive);
}
if(negative != null && negativeText != null){
builder.setNegativeButton(negativeText, negative);
}
builder.create().show();
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
膨胀布局就是创建它的一个新实例。 (您没有收到对现有实例的引用。)因此,在您的
onClick
中,您正在创建布局的新副本,并且您的字段不包含任何文本,因为它们不相同您的用户刚刚输入文本的。To inflate a layout is to create a new instance of it. (You're not receiving a reference to an existing instance.) So, in your
onClick
you are creating a new copy of the layout and your fields don't contain any text because they are not the same ones your user just entered text in.为什么不完全子类化
AlertDialog.Builder
并添加一个方法来检索EditText
值?Why not just completely subclass
AlertDialog.Builder
and add a method to retrieve theEditText
values?做类似的事情:
我尝试过,它有帮助
Do something like:
i tried it and it helped
这是我正在使用的方法:
Here is the method I am using: