AlertDialog 中显示的空 EditText 值
Empty EditText value displayed in AlertDialog
我是 Android 开发新手,遇到了问题。我有 2 EditText
个登录名和密码字段以及 1 个 Button
。当我单击 Button
时,我收到 AlertDialog
消息“Logged in as”,但我需要它是“Logged in作为输入 "login" EditText
的 *值”。
EditText
值始终为空。这只是一个简单的实现,但它不起作用。
我在这里做错了什么?
代码如下:
public class FirstFragment extends Fragment {
EditText loginText;
EditText passwText;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
loginText = (EditText)view2.findViewById(R.id.loginText);
passwText = (EditText)view2.findViewById(R.id.passText);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view) {
handleLoginDialog();
}
});
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//View view2 = getLayoutInflater().inflate(R.layout.fragment_first, null, false);
String st = loginText.getText().toString();
if (loginText != null && passwText != null){
builder.setMessage("Logged in as " + st);
}
else
builder.setMessage("Login or password was not entered!" );
builder.show();
}
}
也许你可以试试这个,但这并没有使用onViewCreated
public class FirstFragment extends Fragment {
EditText loginText;
EditText passText;
Button btnLogin;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
loginText = (EditText)view2.findViewById(R.id.loginText);
passText = (EditText)view2.findViewById(R.id.passText);
btnLogin = (Button) view2.findViewById(R.id.button_first);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleLoginDialog();
}
});
return view2;
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//View view2 = getLayoutInflater().inflate(R.layout.fragment_first, null, false);
String st = loginText.getText().toString().trim();
String pt = passText.getText().toString().trim();
if (st.isEmpty() && pt.isEmpty()){
builder.setMessage("Login or password was not entered!" );
}
else
builder.setMessage("Logged in as " + st);
builder.show();
}
安全的做法是在 handleLoginDialog
方法中将 loginText
作为字符串参数发送并检查字符串是否为空。
我想从你所做的开始回答:
您膨胀了 2 个相同的布局:
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
return inflater.inflate(R.layout.fragment_first, container, false);
我们将这第二个 inflation 称为 view1
,这是为 Fragment
膨胀的原始视图,也是 返回的 在 onViewCreated()
方法中。
然后,您将 onClickListener
设置为 view1
的 button_first
。
然后,在 handleDialogLogin()
方法中,您尝试获取使用 view2
对象创建的 EditText
字段的文本值。
那就是问题所在。因为,在应用程序使用过程中,您在 view1
的 EditText
字段中插入文本,但您要求 view2
的 EditText
字段的文本值,这显然是空的。
这里是正确而干净的方法:
public class FirstFragment extends Fragment {
EditText loginText;
EditText passwText;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// just inflate the layout for this fragment as it is not correct
// place for declaring and initializing views
return inflater.inflate(R.layout.fragment_first, container, false);
}
// this is the best place for declaring and initializing views
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// declare and initialize your EditText here
loginText = (EditText)view.findViewById(R.id.loginText);
passwText = (EditText)view.findViewById(R.id.passText);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view) {
handleLoginDialog();
}
});
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
String st = loginText.getText().toString().trim();
String pt = passText.getText().toString().trim();
if (st.isEmpty() && pt.isEmpty())
builder.setMessage("Login or password was not entered!" );
else
builder.setMessage("Logged in as " + st);
builder.show();
}
}
我是 Android 开发新手,遇到了问题。我有 2 EditText
个登录名和密码字段以及 1 个 Button
。当我单击 Button
时,我收到 AlertDialog
消息“Logged in as”,但我需要它是“Logged in作为输入 "login" EditText
的 *值”。
EditText
值始终为空。这只是一个简单的实现,但它不起作用。
我在这里做错了什么?
代码如下:
public class FirstFragment extends Fragment {
EditText loginText;
EditText passwText;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
loginText = (EditText)view2.findViewById(R.id.loginText);
passwText = (EditText)view2.findViewById(R.id.passText);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first, container, false);
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view) {
handleLoginDialog();
}
});
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//View view2 = getLayoutInflater().inflate(R.layout.fragment_first, null, false);
String st = loginText.getText().toString();
if (loginText != null && passwText != null){
builder.setMessage("Logged in as " + st);
}
else
builder.setMessage("Login or password was not entered!" );
builder.show();
}
}
也许你可以试试这个,但这并没有使用onViewCreated
public class FirstFragment extends Fragment {
EditText loginText;
EditText passText;
Button btnLogin;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
loginText = (EditText)view2.findViewById(R.id.loginText);
passText = (EditText)view2.findViewById(R.id.passText);
btnLogin = (Button) view2.findViewById(R.id.button_first);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
handleLoginDialog();
}
});
return view2;
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
//View view2 = getLayoutInflater().inflate(R.layout.fragment_first, null, false);
String st = loginText.getText().toString().trim();
String pt = passText.getText().toString().trim();
if (st.isEmpty() && pt.isEmpty()){
builder.setMessage("Login or password was not entered!" );
}
else
builder.setMessage("Logged in as " + st);
builder.show();
}
安全的做法是在 handleLoginDialog
方法中将 loginText
作为字符串参数发送并检查字符串是否为空。
我想从你所做的开始回答:
您膨胀了 2 个相同的布局:
View view2 = inflater.inflate(R.layout.fragment_first, null, false);
return inflater.inflate(R.layout.fragment_first, container, false);
我们将这第二个 inflation 称为 view1
,这是为 Fragment
膨胀的原始视图,也是 返回的 在 onViewCreated()
方法中。
然后,您将 onClickListener
设置为 view1
的 button_first
。
然后,在 handleDialogLogin()
方法中,您尝试获取使用 view2
对象创建的 EditText
字段的文本值。
那就是问题所在。因为,在应用程序使用过程中,您在 view1
的 EditText
字段中插入文本,但您要求 view2
的 EditText
字段的文本值,这显然是空的。
这里是正确而干净的方法:
public class FirstFragment extends Fragment {
EditText loginText;
EditText passwText;
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// just inflate the layout for this fragment as it is not correct
// place for declaring and initializing views
return inflater.inflate(R.layout.fragment_first, container, false);
}
// this is the best place for declaring and initializing views
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// declare and initialize your EditText here
loginText = (EditText)view.findViewById(R.id.loginText);
passwText = (EditText)view.findViewById(R.id.passText);
view.findViewById(R.id.button_first).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick( View view) {
handleLoginDialog();
}
});
}
private void handleLoginDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(requireContext());
String st = loginText.getText().toString().trim();
String pt = passText.getText().toString().trim();
if (st.isEmpty() && pt.isEmpty())
builder.setMessage("Login or password was not entered!" );
else
builder.setMessage("Logged in as " + st);
builder.show();
}
}