InputTextLayout无法从其EditText正确执行“ getText()。toString()”

时间:2019-01-20 16:19:12

标签: android android-edittext android-textinputedittext

我现在正在编写一个AlertDialog方法,该方法生成一个自定义AlertDialog,该对话框包含一个TextView和InputTextLayout(weight_InputLayout),供用户输入一些数字。

但是,在测试AlertDialog方法后,我发现即使AlertDialog可以成功显示,我也无法正确获取EditText中的内容,并且它始终返回null字符串,因此始终抛出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);

我的应用程序只是忽略了它。

2 个答案:

答案 0 :(得分:1)

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

例如:

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

答案 1 :(得分:1)

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