我正在创建一个Dropdown(微调器),其中所有日子都必须显示,但我想设置不同的默认值。当前默认值和微调器的第一个值是相同的..请帮我怎么做...代码片段表示赞赏.... 我是=>
public class MainActivity extends Activity implements AdapterView.OnItemSelectedListener {
Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.days, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
TextView tv =(TextView)arg1;
Toast.makeText(this, "YOu have selected"+tv.getText(), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
activity_main.xml中
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Spinner
android:id="@+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
simple_spinner_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
String.xml
&#39;
<string name="app_name">Spinner</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string-array name="days">
<item>Choose A day</item>
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thrusday</item>
<item>Friday</item>
<item>Saturday</item>
</string-array>
&#39;
答案 0 :(得分:1)
您可以使用以下功能: setSelection(整数)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,
R.array.days, android.R.layout.simple_spinner_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
spinner.setSelection(positionSpinner);
}
编辑: 我认为唯一的解决方案是创建自己的微调器适配器,但将文本作为最后一项,并使用getCount以便不显示它。
class MySpinnerAdapter extends ArrayAdapter<String> {
public MySpinnerAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
}
@Override
public int getCount() {
return super.getCount() - 1; // This makes not show the last item
}
@Override
public SpinnerItem getItem(int position) {
return super.getItem(position);
}
@Override
public long getItemId(int position) {
return super.getItemId(position);
}
}
最后:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner =(Spinner) findViewById(R.id.spinner1);
ArrayList itemsSpinner = new ArrayList();
itemsSpinner.add("Day 1");
itemsSpinner.add("Day 2");
itemsSpinner.add("Choose a day"); // Last item
MySpinnerAdapter adapterSpinner = new MySpinnerAdapter(this, android.R.layout.simple_spinner_item, itemsSpinner );
adapterSpinner.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapterSpinner);
spinner.setSelection(items.size() - 1);
}
答案 1 :(得分:1)
使用提示定义您的R.array.days
作为最后一个元素。
<string-array name="days">
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
<item>Choose a Day</item> <!-- Hint -->
</string-array>
现在,实例化并将ArrayAdapter
设置为
spinner = (Spinner) findViewById(R.id.spinner1);
String[] dayValues = getResources().getStringArray(R.array.days);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, dayValues) {
@Override
public int getCount() {
int c = super.getCount();
if (spinner.getSelectedItemPosition() < c - 1) return c;
return c > 0 ? c - 1 : c;
}
};
spinner.setAdapter(adapter);
最后,将提示设置为默认的微调器值
spinner.setSelection(dayValues.length - 1);
答案 2 :(得分:0)
我已经开发了一个小组件来实现这个目标。只需在AutoCompleteTexview中添加提示文本,然后使用它构建CompleteSpinner。