【问题标题】:EditText on Dialog returns no text对话框上的 EditText 不返回任何文本
【发布时间】:2017-02-15 17:46:41
【问题描述】:

我已经厌倦了寻找错误。我没有发现任何错误,但我没有从 editText 获得任何文本。请看以下代码:

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>

我在MainActivityonCreate方法上调用了LockImmediately(this);

    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”连接,但它没有返回任何值。我已尝试清除数据、卸载应用程序、清理、构建所有内容,但结果始终相同!任何帮助将不胜感激..

【问题讨论】:

  • 尝试将enterPWD.getText().toString();更改为alertDialog.findViewById(R.id.pwdValue).getText().toString();,看看是否有效
  • 不,编译非法字符 \u200c 时出错
  • 然后删除该非法字符并尝试

标签: java android string android-edittext


【解决方案1】:

当您将视图设置为 AlertDialog 时,您应该设置视图,您可以在其中找到 EditText 的 ID。

您已使用 myview 来查找 EditText 的 ID,但您试图从 Activity 中获取 myView,而不是从 Dialog 中。

如果将视图设置为myview,则可以从enterPWD EditText 中获取文本。

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

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

【讨论】:

    【解决方案2】:

    你给View 充气然后放下它,

    View myview = LayoutInflater.from(context).inflate(R.layout.activity_pwd,null); 不是对话框的View,它是一个无用的实例。

    您将布局的 id 传递给对话框,它会在 .show() 中扩展它自己的版本。 直接在onClick(..) 中使用alertDialog.findViewById(R.id.pwdValue)

    【讨论】:

    • 让我检查一下
    【解决方案3】:

    试试这个

        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();
            }
        }
    

    【讨论】:

    • 先生您做了哪些更改?
    • 我删除了你的布局膨胀和初始编辑文本在点击@appleappala
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 1970-01-01
    • 1970-01-01
    • 2014-11-08
    • 1970-01-01
    相关资源
    最近更新 更多