Androidstudio中Dialog密码框传入空字符串,但是toast可以显示输入的内容,为什么?
Empty string is passed in Dialog password box in Android studio, but toast can display what was typed, why?
我正在尝试制作一个简单的警告对话框,让用户输入用户名和密码并将其发送回应用程序并关闭对话框。我正处于尝试将文本作为字符串输入编辑文本框的阶段,但是当我尝试将它们添加到字符串时,它们是空的。但是,toast 会显示输入框中的内容,如下例所示:Why are the pwtest and untest strings empty?
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
public class createid extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Toast.makeText(getActivity(), "Dialog Cancelled", Toast.LENGTH_SHORT).show();
}
}
您在错误的时间调用了代码,问题是您在用户可以对编辑文本执行任何操作之前要求编辑文本中的值,因此您收到的是空字符串
该代码应该在onClick接口中添加,而不是初始化
正确的代码是:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// You should get the String value here, after they're done writing in edit text.
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
我正在尝试制作一个简单的警告对话框,让用户输入用户名和密码并将其发送回应用程序并关闭对话框。我正处于尝试将文本作为字符串输入编辑文本框的阶段,但是当我尝试将它们添加到字符串时,它们是空的。但是,toast 会显示输入框中的内容,如下例所示:Why are the pwtest and untest strings empty?
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.DialogFragment;
public class createid extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}
@Override
public void onCancel(DialogInterface dialog) {
super.onCancel(dialog);
Toast.makeText(getActivity(), "Dialog Cancelled", Toast.LENGTH_SHORT).show();
}
}
您在错误的时间调用了代码,问题是您在用户可以对编辑文本执行任何操作之前要求编辑文本中的值,因此您收到的是空字符串
该代码应该在onClick接口中添加,而不是初始化
正确的代码是:
@NonNull
@Override
public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.create, null);
AlertDialog.Builder builder= new AlertDialog.Builder(getActivity());
builder.setTitle("Create ID");
builder.setView(view);
final EditText userid = view.findViewById(R.id.newusername);
final EditText password = view.findViewById(R.id.newpassword);
builder.setNegativeButton("Cancel", null);
builder.setPositiveButton("Create ID", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// You should get the String value here, after they're done writing in edit text.
final String pwtest = password.getText().toString();
final String untest = userid.getText().toString();
Toast.makeText(getActivity(), "User ID:" +userid.getText().toString() + "Password:" + password.getText().toString() +
"userstring is:" + untest + "password string is:" + pwtest, Toast.LENGTH_LONG).show();
}
});
return builder.create();
}