如何以编程方式将textinputlayout放入我自动生成的edittext中

问题描述 投票:0回答:1

我想把TextInputLayout放在我自动生成的EditText中。

示例:如果我单击Button,该应用程序将生成SpinnerEditText,我想在我的TextInputLayout上放一个EditText

btn.Click += (sender, e) =>
{
    tr = new TableRow(this);
    _spinner = new Spinner(this);
    _td1 = new EditText(this);
    _td2 = new EditText(this);
    TextInputLayout textInputLayout = new TextInputLayout(this);
    _td1.SetHint(Resource.String.qty);
    _td2.SetHint(Resource.String.unit);
    //textInputLayout.AddView(_td1);
    //textInputLayout.AddView(_td2);
    //_td1.SetBackgroundResource(Resource.Drawable.EditDesign);
    //_td2.SetBackgroundResource(Resource.Drawable.EditDesign);
    //_spinner.SetBackgroundResource(Resource.Drawable.EditTxtStyle);
    ArrayAdapter<string> _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, prodList);
    _spinner.Adapter = _adapter;
    tr.AddView(textInputLayout);
    tr.AddView(_spinner);
    tr.AddView(_td1);
    tr.AddView(_td2);
    tbleLayout.AddView(tr);
};
c# xamarin.android
1个回答
1
投票

TextInputLayout是22.2.0的新增功能,它与EditText(或EditText的子类)一起使用,并且只能包含EditText的一个子类(或EditText的子类):

你的代码基本上是正确的,你应该这样改变:

 button.Click += (sender, e) =>
     {
         tr = new TableRow(this);
         _spinner = new Spinner(this);
         _td1 = new EditText(this);
         _td2 = new EditText(this);
         TextInputLayout textInputLayout1 = new TextInputLayout(this);
         TextInputLayout textInputLayout2 = new TextInputLayout(this);
         _td1.SetHint(Resource.String.qty);
         _td2.SetHint(Resource.String.unit);
        ArrayAdapter<string> _adapter = new ArrayAdapter<string>(this, Android.Resource.Layout.SimpleListItem1, prodList);
        _spinner.Adapter = _adapter;
        textInputLayout1.AddView(_td1);
        textInputLayout2.AddView(_td2);
        tr.AddView(textInputLayout);
        tr.AddView(_spinner);
        tbleLayout.AddView(tr);
    };
© www.soinside.com 2019 - 2024. All rights reserved.