Android Spinner 验证
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22267842/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android Spinner validation
提问by Golia
I need to validate selected itemof Spinner
in Android
.
我需要验证所选项目的Spinner
在Android
。
I tried the following code, but it's not working.
我尝试了以下代码,但它不起作用。
if (Spinner1.getSelectedItem().toString().trim() == "Pick one") {
Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}
What is wrong with the code, and how can i fix it?
代码有什么问题,我该如何解决?
回答by Chirag Ghori
Use .equals
or .equalsIgnoreCase
to compare two strings in java/android
instead of ==
.
使用.equals
或.equalsIgnoreCase
来比较两个字符串java/android
而不是==
。
Try this
尝试这个
if (Spinner1.getSelectedItem().toString().trim().equals("Pick one")) {
Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}
回答by Darush
Create a new layout called spinner_item.xml:
创建一个名为 spinner_item.xml 的新布局:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
inside your activity MainActivity.java initialize your spinner view:
在您的活动 MainActivity.java 中初始化您的微调视图:
Spinner mySpinner= (Spinner) findViewById(R.id.mySpinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, R.array.items_array, R.layout.spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
Finally use the following method to validate your spinner:
最后使用以下方法来验证您的微调器:
boolean validateSpinner(Spinner spinner, String error){
View selectedView = spinner.getSelectedView();
if (selectedView != null && selectedView instanceof TextView) {
TextView selectedTextView = (TextView) selectedView;
if (selectedTextView.getText().equals("")) {
selectedTextView.setError(error);
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
return false;
}
}
return true;
}
回答by Saro Ta?ciyan
Use equals("Pick one")
and always use equals()
method when checking for equalityfor String
type in Java(unless you are checking for referenceequality) as follows;
使用equals("Pick one")
始终使用equals()
检查方法时,平等的String
在类型的Java(除非你检查参考平等)如下:
if (Spinner1.getSelectedItem().toString().trim().equals("Pick one")) {
Toast.makeText(CallWs.this, "Error", Toast.LENGTH_SHORT).show();
}
What's wrong with you code is; you are using ==
which results in checking if Spinner1.getSelectedItem().toString()
is the same referenceas "Pick one"
which will always be false since "Pick one" is a new String()
instance
你的代码有什么问题;您正在使用==
其结果在检查是否Spinner1.getSelectedItem().toString()
是相同的参考因为"Pick one"
这将永远是假的,因为“选择一个”是一个new String()
实例
For details, check:
有关详细信息,请检查:
回答by Piyush
Simply use this.
简单地使用这个。
else if (Spinner1.getSelectedItem().toString().trim().equalsIgnoreCase("Pick one")) {
Toast.makeText(CallWs.this, "Error",
Toast.LENGTH_SHORT).show();
回答by ESAVITHRI BE 2013 CSE
boolean validateSpinner(Spinner spinner, String error){
View selectedView = spinner.getSelectedView();
if (selectedView != null && selectedView instanceof TextView) {
TextView selectedTextView = (TextView) selectedView;
if (selectedTextView.getText().equals("Select Academic Year")) {
selectedTextView.setError(error);
Toast.makeText(this, error, Toast.LENGTH_LONG).show();
return false;
}
}
return true;
}
回答by Hariharan
Try this..
尝试这个..
else if (Spinner1.getSelectedItem().toString().trim().equals("Pick one")) {
Toast.makeText(CallWs.this, "Error",
Toast.LENGTH_SHORT).show();
==
always just compares two references(for non-primitives, that is) - i.e. it tests whether the two operands refer to the same object.
==
总是只比较两个引用(对于非原语,即) - 即它测试两个操作数是否引用同一个对象。
However, the equals
method can be overridden - so two distinct objects can still be equal...... For more info
但是,该equals
方法可以被覆盖 - 因此两个不同的对象仍然可以相等...... 更多信息