我制作了一个由教师登录()方法调用的自定义警报对话框。Dialogue 包含一个 Editext(其值存储在“text”字符串中)和两个按钮,即“登录”和“取消”。当按下登录按钮时,它显示字符串“文本”的空值。请帮助如果可能?
public void teacherLogin(View view)
{
View alertview = getLayoutInflater().inflate(R.layout.custom_alert,null);
AlertDialog.Builder builder = new AlertDialog.Builder(LoginActivity.this);
builder.setTitle("Login");
builder.setView(alertview);
final String text = ((EditText) alertview.findViewById(R.id.date_text)).getText().toString();
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Toast.makeText(getApplicationContext(), "name:"+text, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i)
{
Snackbar.make(findViewById(android.R.id.content), "Dialogue Cancelled", Snackbar.LENGTH_LONG)
.show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
仅仅是因为您从EditText
显示对话之前获取文本。
这有效:
private void showDialog() {
View alertview = getLayoutInflater().inflate(R.layout.custom_alert, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Login");
builder.setView(alertview);
final EditText editText = ((EditText) alertview.findViewById(R.id.date_text));
builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(), "name:" + editText.getText().toString(), Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_SHORT).show();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句