它在自定义警报对话框中从Edittext检索数据时显示空值?

时间:2017-10-27 13:59:35

标签: android android-edittext

我制作了一个由teacherLogin()方法调用的自定义警报对话框。 对话包括一个Editext(其值存储在" text" String)和两个按钮,即" Login" &安培; "取消&#34 ;.按下Login按钮时,它显示String" 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();
}

2 个答案:

答案 0 :(得分:2)

只是因为您在显示对话之前从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();
}

答案 1 :(得分:0)

这是因为您在用户按下警告对话框的text按钮之前初始化Login变量。在正Login按钮的监听器内初始化它。

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 textEditText = ((EditText) alertview.findViewById(R.id.date_text));

builder.setPositiveButton("Login", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        String textValue = textEditText.getText().toString();

        if(textValue!=null && !textValue.isEmpty(){
           Toast.makeText(getApplicationContext(), "name:" +textValue, Toast.LENGTH_SHORT).show();
        }
         //show empty value toast to user
        else{
          Toast.makeText(getApplicationContext(), "Please enter name", Toast.LENGTH_SHORT).show();
          //dismiss dialog
          dialogInterface.dismiss();
       }
    }
    });

builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
    @Override
    public void onClick(DialogInterface dialogInterface, int i) {

        Toast.makeText(MainActivity.this, "cancelled", Toast.LENGTH_SHORT).show();
        //dismiss dialog
        dialogInterface.dismiss();
    }
});

AlertDialog alertDialog = builder.create();
alertDialog.show();
}