我想向spinner
添加一些操作(正如您在下面的代码中看到的,我添加了一些Toasts
)。
MainActivity.java:
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
String[] items = {"Item 1", "Item 2", "Item 3", "Item 4", "Item 5"};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
ArrayAdapter AA = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
AA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(AA);
}
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Item 1")) {
Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
}
String selectedItem2 = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Item 2")) {
Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
}
String selectedItem3 = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Item 3")) {
Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
}
String selectedItem4 = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Item 4")) {
Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
}
String selectedItem5 = parent.getItemAtPosition(position).toString();
if (selectedItem.equals("Item 5")) {
Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
activity_main.xml中:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="89dp"></Spinner>
</RelativeLayout>
所以,实际上这段代码工作得很好但是如果我打开应用程序,第一个Toast(带有文本“Item 1”)已经可见了。但我不希望这样。那么,我需要做什么,这不会发生?
提前致谢!
答案 0 :(得分:5)
试试这个
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener{
String[] items = { "Item 1", "Item 2", "Item 3", "Item 4", "Item 5" };
boolean app_started = true;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
spinner1.setOnItemSelectedListener(this);
ArrayAdapter AA = new ArrayAdapter(this, android.R.layout.simple_spinner_item, items);
AA.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(AA);
}
@Override
public void onItemSelected(AdapterView parent, View view, int position, long id) {
String selectedItem = parent.getItemAtPosition(position).toString();
if(!app_started )
{
if(selectedItem.equals("Item 1")) {
Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
}
String selectedItem2 = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 2")) {
Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
}
String selectedItem3 = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 3")) {
Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
}
String selectedItem4 = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 4")) {
Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
}
String selectedItem5 = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 5")) {
Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
}
}
app_started = false;
}
@Override
public void onNothingSelected(AdapterView parent) {
}
}
答案 1 :(得分:2)
试试这个 -
String selectedItem = parent.getItemAtPosition(position).toString();
if(selectedItem.equals("Item 1")) {
Toast.makeText(getApplicationContext(), "Item 1", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 2")) {
Toast.makeText(getApplicationContext(), "Item 2", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 3")) {
Toast.makeText(getApplicationContext(), "Item 3", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 4")) {
Toast.makeText(getApplicationContext(), "Item 4", Toast.LENGTH_LONG).show();
}
else if(selectedItem.equals("Item 5")) {
Toast.makeText(getApplicationContext(), "Item 5", Toast.LENGTH_LONG).show();
}
答案 2 :(得分:0)
不幸的是,谷歌一开始没有提供“null”值的功能。您可以查看this link了解详情。希望它有所帮助。 :)