xamarin.android - 我想添加自定义浮动微调器
问题描述
我想添加自定义浮动微调器。这样怎么加??我已经尝试在 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;
}
}
我已经尝试过上面的代码。但有时它会抛出异常。它不能正常工作。请给我解决如何创建浮动微调器的方法。
解决方案
我考虑了很长时间,因为我在早期的一个项目中做了类似的实现,然后我记得我正在使用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_multiline
XML 中的属性在多行上设置错误消息(默认为 true)。
推荐阅读
- java - 在 Java 中将日期与字符串进行比较
- shopify - shopify中变量对象的访问值
- c - 将 uint8_t 数组转换为 uint16_t
- react-native - 将本机地图方向一个位置反应到另一个位置
- java - Liquibase maven 插件,我的数据库驱动程序出现问题
- python - 无法使用 Ã 字符解码不正确编码的字符串
- git - Git删除并重命名目录
- file-upload - 量角器:面临使用发送密钥在 Safari 浏览器中上传文件/图像的问题
- java - 无法索引类 module-info.class atlog4j-api.jar:java.lang.IllegalStateException:未知标签!pos=4 池数 = 24
- javascript - 关于 li 特定类的就绪位置