android - 在 AlertDialog 的 editText 中如何以及在何处添加 setError

标签 android android-edittext android-alertdialog

    package alonso.radiobuttonquiz;

    import android.content.Context;
    import android.content.DialogInterface;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.preference.PreferenceManager;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ImageButton;
    import android.widget.TextView;

    public class HomePage extends AppCompatActivity {
      TextView tV1,tV2;
    SharedPreferences sp;
    ImageButton btn;
    Button button;
    //alert dialog box
    final Context c = this;



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_page);
        tV1 = (TextView) findViewById(R.id.totalcoin);
        tV2 = (TextView)findViewById(R.id.nameDisplay);
        btn = (ImageButton) findViewById(R.id.startQuiz);


        SharedPreferences preferences = 
    PreferenceManager.getDefaultSharedPreferences(this);
        Integer coin = preferences.getInt("coin", 0);
        tV1.setText("COIN: " + coin);


        final SharedPreferences prefs = 
    PreferenceManager.getDefaultSharedPreferences(this);
        final SharedPreferences.Editor editor = prefs.edit();
        final String username = prefs.getString("USERNAME", "");
        prefs.getString("USERNAME","");
        tV2.setText(username);



        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomePage.this, LvlMenu.class);
                startActivity(intent);
                finish();


            }
        });


        LayoutInflater layoutInflaterAndroid = LayoutInflater.from(c);
        View mView = 
     layoutInflaterAndroid.inflate(R.layout.activity_login2,null);
        final AlertDialog.Builder alertDialogBuilderUserInput = new 
    AlertDialog.Builder(c,R.style.AlertDialogTheme);

        alertDialogBuilderUserInput.setView(mView);

        final EditText userInputDialogEditText = (EditText) 
    mView.findViewById(R.id.userInputDialog);
        alertDialogBuilderUserInput
                .setCancelable(false)
                .setPositiveButton("LOGIN", new 
    DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialogBox, int id) {
                        String username = 
    userInputDialogEditText.getText().toString();
                        editor.putString("USERNAME",username);
                        editor.apply();
                        tV2.setText(username);
                }});

        AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
        alertDialogAndroid.show();
     }
}

alertDialog中的editText为空时,在哪里添加setError?如果可能,请编辑我的代码以在用户从不在 editText 中输入任何字母时添加 setError。谢谢!这是测验应用程序中的登录屏幕...真的希望大家能伸出援手,因为我已经为此工作了几个小时...

最佳答案

 LayoutInflater layoutInflaterAndroid = LayoutInflater.from(this);
    View mView =
            layoutInflaterAndroid.inflate(R.layout.activity_login2,null);
    final AlertDialog.Builder alertDialogBuilderUserInput = new
            AlertDialog.Builder(this,R.style.AlertDialogTheme);



    final EditText userInputDialogEditText = (EditText)
            mView.findViewById(R.id.userInputDialog);
    alertDialogBuilderUserInput.setView(mView);
    AlertDialog alertDialogAndroid = alertDialogBuilderUserInput.create();
    alertDialogAndroid.show();

    alertDialogAndroid.setOnShowListener(new DialogInterface.OnShowListener() {

        @Override
        public void onShow(DialogInterface dialogInterface) {

            Button button = ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE);
            button.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View view) {
                    // TODO Do something
                    if(userInputDialogEditText.getText().toString().equals("")){
                        userInputDialogEditText.setError("Enter Username");
                    }else {
                        String username =
                                userInputDialogEditText.getText().toString();
                        editor.putString("USERNAME",username);
                        editor.apply();
                        tV2.setText(username);
                        dialog.dismiss();
                    }
                }
            });
        }
    });
alertDialogAndroid.show();

希望这对你有帮助...如果你需要任何帮助你可以问

关于android - 在 AlertDialog 的 editText 中如何以及在何处添加 setError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53705273/

相关文章:

android - 如何在启动时自动运行 Android Chrome 主屏幕应用程序

android - 如何使用 cognito 在 android 中指定自定义错误消息

Android Edittext 线条颜色

android - Listview android中的Edittext

android - 如何连接到 rild 套接字

java - 安卓工作室。类别搜索停止工作。 (导航 -> 类...)

android - 如何修复 "TextInputLaout with TextInputEditText view I can not implement to show next focus Edittext using imeOption and nextFocustDown attribute"

android - AlertDialog背景颜色问题

android - 使用先前选择的值开始数字选择器

java - AlertDialog 内的 LinearLayout 不滚动