首页 > 解决方案 > 我想添加自定义浮动微调器

问题描述

我想添加自定义浮动微调器。这样怎么加??我已经尝试在 TextInputLayout 中使用 AutoComplete textview,但它不能正常工作。 http://prntscr.com/mm8ksc

//SocietySpinnerLayout.axml
        <android.support.design.widget.TextInputLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_weight="50"
                    android:id="@+id/societySpinnerLayout"
                    app:errorTextAppearance="@style/ErrorText"
                    android:theme="@style/CommonTextStyleTheme">
                    <AutoCompleteTextView
                            android:id="@+id/societySpinner"
                            android:paddingBottom="@dimen/padding_20"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:textColor="#4C5375"
                            android:textSize="@dimen/textSize_14"
                            android:hint="SOCIETY"
                            android:paddingEnd="60dp"
                            android:textCursorDrawable="@null"
                            android:inputType="textPhonetic" />
                </android.support.design.widget.TextInputLayout>

//SocietySpinner.cs

     private void BindToMySociety()
            {
                //String[] arraySociety = Resources.GetStringArray(Resource.Array.arraySociety);

                ArrayAdapter adapter = new SpinnerSocietyAdapter(this, Resource.Layout.PublisherSpinnerItemLayout, HelperNavigation.LstSociety);
                _societySpinner.Adapter = adapter;
                if (_societySpinner.HasFocus)
                {
                    InputMethodManager imm = (InputMethodManager)GetSystemService(Context.InputMethodService);
                    imm.HideSoftInputFromWindow(Window.CurrentFocus.WindowToken, 0);
                }
               _societySpinner.SetOnTouchListener(this);
                _societySpinner.Focusable = false;
                // _societySpinner.Click+=_societySpinner_Click;
                _societySpinner.ItemClick += _societySpinner_ItemClick;
                _societySpinner.SetOnDismissListener(this);
                if(_selectedSociety!=null)
                {
                    _societySpinner.Text = _selectedSociety.name;
                }
            }

        private void _societySpinner_Click(object sender, EventArgs e)
        {
            try
            {
                // _societySpinner.Text = string.Empty;
                if (!string.IsNullOrEmpty(_societySpinner.Text))
                {
                    _societySpinner.Text = string.Empty;
                    _societySpinnerLayout.Typeface = ItalicFont;
                    _societySpinner.Typeface = ItalicFont;
                }
              ((AutoCompleteTextView)_societySpinner).ShowDropDown();

            }
            catch (Exception ex)
            {

            }
        }

        public bool OnTouch(View v, MotionEvent e)
        {
            try
            {
                // _societySpinner.Text = string.Empty;
                if (!string.IsNullOrEmpty(_societySpinner.Text))
                {
                    _societySpinner.Text = string.Empty;
                    _societySpinnerLayout.Typeface = ItalicFont;
                    _societySpinner.Typeface = ItalicFont;
                }
                ((AutoCompleteTextView)v).ShowDropDown();
                return false;
            }
            catch (Exception ex)
            {
                return false;
            }

        }

        public void OnDismiss()
        {
            if(_selectedSociety!=null)
            {
                if (!string.IsNullOrEmpty(_selectedSociety.name))
                {
                    _societySpinner.Text = _selectedSociety.name;
                    _societySpinnerLayout.Typeface = RegularFont;
                    _societySpinner.Typeface = RegularFont;
                }
            }
            else
            {
                _societySpinnerLayout.Typeface = ItalicFont;
                _societySpinner.Typeface = ItalicFont;
            }

        }

我已经尝试过上面的代码。但有时它会抛出异常。它不能正常工作。请给我解决如何创建浮动微调器的方法。

标签: xamarin.android

解决方案


我考虑了很长时间,因为我在早期的一个项目中做了类似的实现,然后我记得我正在使用Ganfra Material Spinner来实现这一点:

这里有一个示例项目

您可以在 XML 中使用它,如下所示:

<fr.ganfra.materialspinner.MaterialSpinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" 
app:ms_multiline="false"
app:ms_hint="hint"
app:ms_enableFloatingLabel="false"
app:ms_enableErrorLabel="false"
app:ms_floatingLabelText="floating label"
app:ms_baseColor="@color/base"
app:ms_highlightColor="@color/highlight"
app:ms_errorColor="@color/error"
app:ms_typeface="typeface.ttf"
app:ms_thickness="2dp"
app:ms_hintColor="@color/hint"
app:ms_arrowColor="@color/arrow"
app:ms_arrowSize="16dp"
app:ms_alignLabels="false"
app:ms_floatingLabelColor="@color/floating_label"/>

您可以设置提示和浮动标签文本。如果没有提供浮动标签文本,则将改为设置提示。

您可以像使用普通微调器一样使用它,为其设置适配器:

string[] ITEMS = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5", "Item 6"};
var adapter = new ArrayAdapter<String>(this, Android.Resource.Layout.SimpleSpinnerItem, ITEMS);
adapter.SetDropDownViewResource(Android.Resource.Layout.SimpleSpinnerDropDownItem);
var spinner = FindViewById<MaterialSpinner>(Resource.Id.spinner1);
spinner.Adapter = adapter;

如果您需要设置错误消息,您可以使用与以下相同的方式EditText

 // Activate
 spinner.Error = "Error";
 // Deactivate
 spinner.Error = null;

您可以选择滚动动画或使用ms_multilineXML 中的属性在多行上设置错误消息(默认为 true)。


推荐阅读