首页 > 解决方案 > InputTextLayout 无法从其 EditText 正确执行“getText().toString()”

问题描述

我现在正在编写一个生成自定义 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);

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

标签: androidandroid-edittextandroid-textinputedittext

解决方案


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

例如:

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


推荐阅读