1

我正在尝试制作一个非常简单的应用程序。我只需要从 TimePicker 元素中获取时间,然后根据值为 1-8 的 Spinner 添加 30 分钟增量。也就是说,如果用户选择值'3'和时间'8:30am'程序应该做

 (30min) * 3 = 90min

然后

 (8:30am) + 90min = 10:00am

有谁知道如何从 Spinner 获取输入(值 1-8),进行简单计算,然后将其添加到来自 TimePicker 的数据输入中?

对此主题的任何帮助将不胜感激。

4

1 回答 1

0

这里有几件事: 1. 设置一个侦听器以获取从选择器中选择的值,然后获取时间选择器的值:

public class MainActivity extends Activity{
    Integer[] numbers = new Integer[]{0,1,2,3,4,5,6,7,8};


        final TimePicker tp = (TimePicker)findViewById(R.id.timePicker1);
        tp.setIs24HourView(true);
        Spinner s1 = (Spinner) findViewById(R.id.spinner1);
        ArrayAdapter<Integer> adapter = new ArrayAdapter<Integer>(this, 
                android.R.layout.simple_list_item_single_choice, numbers);

s1.setAdapter(adapter);
        s1.setOnItemSelectedListener(new OnItemSelectedListener(){   
            public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)

            {
                int index = arg0.getSelectedItemPosition();
                switch(index)
                {
                case 0: 
                    int min = tp.getCurrentMinute();
                    int hour = tp.getCurrentHour();
                    //Do your calculations here etc

                }
            }

            public void onNothingSelected(AdapterView<?> arg0) {


            }
        });

}

  1. 这可能对你也有帮助,如何为时间选择器设置监听器,(如果有人可以帮助我并编辑它,不能让它正确格式化):

    final TimePicker tp = (TimePicker)findViewById(R.id.timePicker1);
            TimePicker.OnTimeChangedListener listen = new  
    
            TimePicker.OnTimeChangedListener() {
    
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            // TODO Auto-generated method stub
            //do some calculations with hourOfDay and minute
    
        }
    };
    

    tp.setOnTimeChangedListener(听);

于 2012-09-07T18:47:09.817 回答