EditText on Dialog returns no text

五迷三道 提交于 2019-12-02 21:54:35

问题


I am so tired on finding the mistake. I didn't find any mistake but I am getting no text from editText. please Look at following Code:

activity_pwd.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Enter PIN "
    android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
    android:id="@+id/pwdValue"
    android:layout_width="match_parent"
    android:maxLength="4"
    android:maxLines="1"
    android:inputType="numberPassword"
    android:layout_height="wrap_content">
</EditText>
</LinearLayout>

I called LockImmediately(this); on onCreate method of MainActivity

    public void LockImmediately(Context context) {
    if (lock_app) {
        View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null);
        enterPWD  = (EditText) myview.findViewById(R.id.pwdValue);

        AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false)
                .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String user_text = enterPWD.getText().toString();
                        Log.d("onCreate pws", " i "+user_text);
                        if (pwd.equals(user_text)) {
                            dialog.dismiss();
                            Log.d("onCreate", "Work done");
                        } else {
                            dialog.dismiss();
                            MainActivity.this.finish();
                        }
                    }
                }).create();
        alertDialog.show();
    }
}

Log.d

 02-15 23:15:30.840 29379-29379/com.developersqueen.wishlater D/onCreate: onCreate
 02-15 23:15:37.021 29379-29379/com.developersqueen.wishlater D/onCreate pws:  i 
 02-15 23:15:45.427 29379-29379/com.developersqueen.wishlater D/onCreate:  onCreate
 02-15 23:15:49.026 29379-29379/com.developersqueen.wishlater D/onCreate pws:  i 

You can see that log above that I have concatenate edittext value with "i" but it's returning no value.. I have tried clearing data, unistalling app, clean, build everything but same result always!! Any help would be appreciated..


回答1:


When you are setting the view to the AlertDialog, you should set the view, where you found the ID of the EditText.

You have used myview to find the ID of the EditText, but you are trying to get myView from the Activity, not from the Dialog.

If you set the view to be myview, you can get the text from the enterPWD EditText.

AlertDialog alertDialog = new AlertDialog.Builder(this).setCancelable(false)
                .setView(myview).setPositiveButton("OK", new DialogInterface.OnClickListener() {
...
}

You should create a separate layout for the Activity and for the Dialog itself. Not to use one layout for both.




回答2:


You inflate a View and drop it,

View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null); is NOT the View of the Dialog, its a useless instance.

You pass the id of the layout to the dialog, which inflates it's own version of it in .show(). Use alertDialog.findViewById(R.id.pwdValue) directly in onClick(..) instead.




回答3:


Try this

    public void LockImmediately(Context context) {
    if (lock_app) {
        AlertDialog alertDialog = new   AlertDialog.Builder(this).setCancelable(false)
                .setView(R.layout.activity_pwd).setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                            EditText enterPWD  = (EditText) myview.findViewById(R.id.pwdValue);
                            String user_text = enterPWD.getText().toString();
                            Log.d("onCreate pws", " i "+user_text);
                            if (pwd.equals(user_text)) {
                                dialog.dismiss();
                                Log.d("onCreate", "Work done");
                            } else {
                                dialog.dismiss();
                                MainActivity.this.finish();
                            }
                        }
                    }).create();
            alertDialog.show();
        }
    }


来源:https://stackoverflow.com/questions/42256602/edittext-on-dialog-returns-no-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!