对话框上的EditText不返回任何文本 [英] EditText on Dialog returns no text

查看:124
本文介绍了对话框上的EditText不返回任何文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现错误太累了。我没有发现任何错误,但我没有从editText获取任何文本。请查看以下代码:

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>

我在 onCreate上打电话给 LockImmediately(this); 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 

你可以看到上面的日志,我已将edittext值与i连接,但它没有返回任何值..我已经尝试清除数据,unistalling app,清理,构建一切但总是相同的结果!!任何帮助将不胜感激..

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..

推荐答案

当你将视图设置为AlertDialog时,你应该设置视图,你在哪里找到了EditText的ID。

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

您已使用 myview 查找EditText的ID,但是您尝试从中获取myView活动,而不是来自对话框。

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.

如果您将视图设置为 myview ,则可以从 enterPWD <获取文本/ strong> EditText。

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() {
...
}

您应该为Activity和Dialog本身创建一个单独的布局。不要为两者使用一个布局。

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

这篇关于对话框上的EditText不返回任何文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆