【问题标题】:InputTextLayout cannot perform "getText().toString()" from its EditText properlyInputTextLayout 无法从其 EditText 正确执行“getText().toString()”
【发布时间】:2019-06-14 03:52:45
【问题描述】:

我现在正在编写一个 AlertDialog 方法,它生成一个自定义的 AlertDialog,由一个 TextView 和 InputTextLayout (weight_InputLayout) 组成,供用户输入一些数字。

但是,在我测试了我的AlertDialog方法之后,我发现即使我的AlertDialog可以显示成功,我也无法正确获取EditText中的内容,并且总是返回空字符串,所以它总是抛出NumberFormatException。

我在我的程序中添加了足够多的日志,日志结果是这样的

V/userweightBtn: Showing the dialog for user
V/setWeightDialog: weight_InputLayout is not null
E/setWeightDialog: text is not null
I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@35e561b 
time:527753839
V/setWeightDialog: user clicked the button
V/weight: 50
V/setWeightDialog: User input is valid.
V/setWeightDialog: User's input has been saved

以下是我的 AlertDialog 方法的代码,注意 weight = 50 是我测试其他功能的虚拟数字,因此无需介意:

//create a dialog which lets user enter weight data
private AlertDialog setWeightDialog()
{


    LayoutInflater LI = LayoutInflater.from(MainActivity.this);
    View v =LI.inflate(R.layout.weight_dialog,null);

    final AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setCancelable(false);
    builder.setView(R.layout.weight_dialog);
    weight_InputLayout = v.findViewById(R.id.userWeightLayout);
    if (weight_InputLayout == null)
        Log.e("setWeightDialog","null weight_InputLayout");
    else
    {
        Log.v("setWeightDialog","weight_InputLayout is not null");
        EditText text = weight_InputLayout.getEditText();
        if (text == null)
            Log.v("setWeightDialog","text is null");
        else Log.e("setWeightDialog","text is not null");
    }
    builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialogInterface, int i) {
                Log.v("setWeightDialog","user clicked the button");
                String inputted_weight = weight_InputLayout.getEditText().getText().toString();
                Log.e("inputted_weight",inputted_weight);
                int weight = 50;//A dummy number which enables further test.
                //weight = Integer.parseInt(inputted_weight); this is what i want
                Log.v("weight",String.valueOf(weight));
                try
                {
                    if (weight > 0)
                    {
                        Log.v("setWeightDialog","User input is valid.");
                        //If weight input is valid, then save into preferences.
                        SharedPreferences.Editor editor = preferences.edit();
                        editor.clear();
                        editor.putInt(getString(R.string.UserWeight),weight);
                        editor.apply();//test: apply or commit?
                        Log.v("setWeightDialog","User's input has been saved");
                        //after saving, dismiss the dialog.
                        builder.create().dismiss();
                    } else
                    {
                        Log.e("setWeightDialog","User input is INVALID");
                        weight_InputLayout.getEditText().setText("");
                        weight_InputLayout.getEditText().setHint(getString(R.string.invalid_weight));
                    }
                } catch (Exception e)
                {
                    Log.e("setWeightDialog","Error in saving user's weight",e);
                }
        }
    });

    return builder.create();
}

weight_dialog 的布局只包含一个 TextView 和一个 InputTextLayout。

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/weight_dialogLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
    android:id="@+id/weight_prompt"
    android:layout_width="339dp"
    android:layout_height="wrap_content"
    android:layout_alignParentStart="true"
    android:layout_alignParentTop="true"
    android:layout_marginStart="15dp"
    android:layout_marginTop="40dp"
    android:text="@string/enter_Weight"
    android:textSize="24sp" />

<android.support.design.widget.TextInputLayout
    android:id="@+id/userWeightLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@id/weight_prompt">

    <android.support.design.widget.TextInputEditText
        android:id="@+id/userWeightEditText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number" />
</android.support.design.widget.TextInputLayout>

</RelativeLayout>

我希望字符串“inputted_weight”可以得到用户在inputTextLayout中输入的字符串。但是,这些代码-

            String inputted_weight = weight_InputLayout.getEditText().getText().toString();
            Log.e("inputted_weight",inputted_weight);

只是被我的应用程序省略了。

【问题讨论】:

  • builder.setView(R.layout.weight_dialog) 更改为builder.setView(v)。当您将布局 ID 传递给 AlertDialog.Builder 时,它会膨胀自己的布局实例,而不是您膨胀并在其中找到 TextInputLayout 的实例。
  • 哦,谢谢。从 R.layout.weight_dialog 更改为 v 后,它可以工作
  • 我没有注意到我应该将 R.layout 替换为 v

标签: android android-edittext android-textinputedittext


【解决方案1】:

您可以尝试直接从 id:userWeightEditText 获取 TextInputEditText 文本,而不是 textlayout 获取 edittext。

例如:

字符串文本 = findViewbyid(R.id.userWeightEditText).getText().toString()

【讨论】:

  • 感谢您的回复。我遵循了 Mike M. 的方法,它奏效了!
【解决方案2】:

您可以使用 view.getEditText().getText().toString() 从 InputTextLayout 获取文本。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-07-02
    • 2012-12-25
    • 1970-01-01
    • 1970-01-01
    • 2014-06-02
    • 2013-08-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多