展开全部
spinner item无法被隐藏,只能删除掉。代码如下:
package com.turboradio.googlesdk;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Ex04_09_Spinner extends Activity {
private static final String [] countriesStr = {
"北京市","上海市","天津市","重庆市"
};
private TextView myTextView;
private EditText myEditText;
private Spinner mySpinner;
private ArrayAdapter<String> adapter;
private List<String> allCountries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_04_09);
allCountries = new ArrayList<String>();
for(int i = 0;i<countriesStr.length;i++){
allCountries.add(countriesStr[i]);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allCountries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
myTextView = (TextView)findViewById(R.id.text1);
myEditText = (EditText)findViewById(R.id.edit);
mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myTextView.setText(parent.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
public void addSpinnerItem(View v){
String newCountry = myEditText.getText().toString();
for(int i = 0;i<adapter.getCount();i++){
if(newCountry.equals(adapter.getItem(i))){
return;
}
}
if(!newCountry.equals("")){
adapter.add(newCountry);
int position = adapter.getPosition(newCountry);
mySpinner.setSelection(position);
myEditText.setText("");
}
}
public void removeSpinnerItem(View v){
if(mySpinner.getSelectedItem() != null){
adapter.remove(mySpinner.getSelectedItem().toString());
myEditText.setText("");
if(adapter.getCount() == 0){
myTextView.setText("");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="addSpinnerItem"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="removeSpinnerItem"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
package com.turboradio.googlesdk;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Ex04_09_Spinner extends Activity {
private static final String [] countriesStr = {
"北京市","上海市","天津市","重庆市"
};
private TextView myTextView;
private EditText myEditText;
private Spinner mySpinner;
private ArrayAdapter<String> adapter;
private List<String> allCountries;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex_04_09);
allCountries = new ArrayList<String>();
for(int i = 0;i<countriesStr.length;i++){
allCountries.add(countriesStr[i]);
}
adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,allCountries);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
myTextView = (TextView)findViewById(R.id.text1);
myEditText = (EditText)findViewById(R.id.edit);
mySpinner = (Spinner)findViewById(R.id.spinner);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
myTextView.setText(parent.getSelectedItem().toString());
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}});
}
public void addSpinnerItem(View v){
String newCountry = myEditText.getText().toString();
for(int i = 0;i<adapter.getCount();i++){
if(newCountry.equals(adapter.getItem(i))){
return;
}
}
if(!newCountry.equals("")){
adapter.add(newCountry);
int position = adapter.getPosition(newCountry);
mySpinner.setSelection(position);
myEditText.setText("");
}
}
public void removeSpinnerItem(View v){
if(mySpinner.getSelectedItem() != null){
adapter.remove(mySpinner.getSelectedItem().toString());
myEditText.setText("");
if(adapter.getCount() == 0){
myTextView.setText("");
}
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/text1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/add"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="添加"
android:onClick="addSpinnerItem"
/>
<Button
android:id="@+id/delete"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="删除"
android:onClick="removeSpinnerItem"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只能把item删除掉
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
只能把item删除掉
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |