如果用户没有选择选项,我正在尝试在visual studio中的xamarin中验证C#中的微调器。类似于TextView中的setError,我在SO中看到了例子,但是如果我没有错的话,所有这些都是针对java的。
这是我的微调器
Spinner spinnerJobTypes = FindViewById<Spinner>(Resource.Id.spinnerJobTypes);
这对我不起作用
TextView errorText = (TextView)mySpinner.getSelectedView();
如果用户没有选择任何内容,我想显示此内容
else if (spinnerJobTypes.SelectedItem.ToString() == "-Select-")
{
//Display validation error
}
感谢任何帮助,
答案 0 :(得分:0)
https://developer.xamarin.com/guides/android/user_interface/spinner/
供参考errorText.SetCompoundDrawablesWithIntrinsicBounds(0, 0, Resource.Drawable.errorIcon, 0);
<强>编辑:强> 使用此选项并检查
func makeAddressDefault(_ sender: UIButton) {
let address = addresses[sender.tag]
let params:Parameters = ["userid": Globals.sharedInstance.user!.id, "addressid": address.id!]
let url = "/makeaddressdefault?userid=" + Globals.sharedInstance.user!.id
RestApiManager.sharedInstance.makePostRequest(vc:self, url: url, post:params, successCompletionHandler:{
(data) in
self.addresses.removeAll() // array of addresses
self.reloadAddresses() // in this reload method in last i have added self.tableview.reload()
}, failureCompletionHandler:{(data) in })
}
答案 1 :(得分:0)
我的建议是,添加一个&#34;没有选择&#34;查看Spinner并使用bool参数来识别用户是否选择了某个项目:
SpinnerAdapter.cs
public class SpinnerAdapter : BaseAdapter<String>
{
Context context;
List<String> list;
public SpinnerAdapter(Context c, List<String> list)
{
context = c;
this.list = list;
}
public override string this[int position] => list[position-1];
public override int Count => this.list.Count+1;
public override long GetItemId(int position)
{
return 0;
}
public override View GetView(int position, View convertView, ViewGroup parent)
{
View view;
if (position == 0)
{
view = LayoutInflater.From(context).Inflate(Resource.Layout.spinner_error,null);
var txtView = view.FindViewById<TextView>(Resource.Id.tvErr);
view.FindViewById<TextView>(Resource.Id.tvErr).Text = "None Selected";
//uncomment the following line if you want to show the error icon inside of spinner
//view.FindViewById<TextView>(Resource.Id.tvErr).Error = "";
}
else
{
view = convertView;
if (view == null|| view.FindViewById<TextView>(Resource.Id.tvItem)==null)
{
view = LayoutInflater.From(context).Inflate(Resource.Layout.spinner_item, null);
}
view.FindViewById<TextView>(Resource.Id.tvItem).Text = list[position-1];
}
return view;
}
}
MainActivity:
public class MainActivity : Activity
{
Spinner mSpinner;
bool validatePass=false;
TextView tvResult;
protected override void OnCreate(Bundle bundle)
{
base.OnCreate(bundle);
// Set our view from the "main" layout resource
SetContentView (Resource.Layout.Main);
mSpinner = FindViewById<Spinner>(Resource.Id.mSpinner);
var list = InitList();
SpinnerAdapter adapter = new SpinnerAdapter(this, list);
tvResult = FindViewById<TextView>(Resource.Id.tvResult);
mSpinner.Adapter = adapter;
mSpinner.ItemSelected += MSpinner_ItemSelected;
}
private void MSpinner_ItemSelected(object sender, AdapterView.ItemSelectedEventArgs e)
{
//use validatePass to indicate if user has selected something.
if (e.Position != 0)
{
validatePass = true;
}
else
{
validatePass = false;
}
tvResult.Text ="Validate Result: "+ validatePass.ToString();
}
private List<string> InitList()
{
List<string> list = new List<string>();
for (int i = 0; i < 15; i++)
{
list.Add("Item: " + i);
}
return list;
}
}
但是如果您真的想在微调器内显示错误图标,可以在TextView.Error
内设置GetView
。
以下是我完整的演示:HelloSpinner。