我对android很新。我想根据微调器中的选定选项更改活动内容。例如,我在微调器“Option2”和“Option2”中有两个选项和两个日期选择器。我希望我的活动以这种方式工作,当我选择选项1时,我应该看到一个日期选择器,当我选择选项2时,我会动态地看到两个日期选择器。
这是我的微调器代码
Spinner spinner= (Spinner) findViewById(R.id.spinner);
final List<String> List=new ArrayList<String>();
List.add("Option1");
List.add("Option2");
ArrayAdapter<String> Adapter=new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,List);
Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
triptype.setAdapter(Adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
selectedOption=spinner.getSelectedItem().toString();
prefs = getSharedPreferences(prefName, MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
//---save the values in the EditText view to preferences---
editor.putInt("index", pos1);
editor.putString("value", selectedoption);
//---saves the values---
editor.commit();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
答案 0 :(得分:1)
如果您不想创建datepicker运行时,那么您应该在xml文件中添加两个日期选择器。记住,将日期选择器的两个位置保持在xml上是相同的。并将两个可见性设置为“GONE”。
类文件:
spinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
//set visibility of datepicker
if(position==0)
{
set "visible" to datepicker1,
set "gone" to datepicker2.
}
if(position==1)
{
set "gone" to datepicker1,
set "visible" to datepicker2.
}
}
}
答案 1 :(得分:0)
在onItemSelected方法
上的spinners上动态添加timePicker @Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int position, long id) {
// TODO Auto-generated method stub
int pos1=position;
for(int i=0;i<mainlayout.getChildCount();i++)
{
View v=mainlayout.getChildAt(i)
if(v instanceof TimePicker)
mainlayout.removeView(v);
}
if(pos==0)
{
TimePicker t=new TimePicker (context);
mainlayout.addView(t);
}
else
{
TimePicker t1=new TimePicker (context);
mainlayout.addView(t1);
TimePicker t2=new TimePicker (context);
mainlayout.addView(t2);
}
}