0

我对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

        }

    });
4

2 回答 2

1

如果您不想创建日期选择器运行时,那么您应该在 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.
        }
    }
}
于 2013-06-26T13:19:10.970 回答
0

在 onItemSelected 方法的微调器上动态添加 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);
}   
    }
于 2013-06-26T12:49:59.730 回答