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 方法的代码,请注意,权重 = 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);

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

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

例如:

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

您可以使用

view.getEditText().getText().toString() 从InputTextLayout获取文本。

相关内容

  • 没有找到相关文章

最新更新