【问题标题】:How to create a spinner that one can select a time to countdown from如何创建一个可以选择倒计时时间的微调器
【发布时间】:2016-08-18 05:25:38
【问题描述】:

我正在创建一个锻炼应用程序,我到处搜索,试图找到如何创建一个显示持续时间的微调器,当用户选择持续时间时,所选时间将出现在倒计时计时器上在另一个活动中。感谢您的帮助!

【问题讨论】:

  • These 都是很好的例子。

标签: java android timer spinner


【解决方案1】:

希望这段代码能帮助你得到你想要的东西

在你的第一个活动中初始化一个微调器

public class MainActivity extends AppCompatActivity {

//Declare the ui component
private Spinner spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //initialize the ui component
    spinner = (Spinner) findViewById(R.id.spinner);

    //create a collection holding your desired time with a note first, we will check that later
    // to find if the user selects a time

    ArrayList<String> timer = new ArrayList<>();
    timer.add("Select your time");
    timer.add("10");
    timer.add("20");
    timer.add("30");
    timer.add("40");

    //create a adpter for the spinner and set that to the spinner
    ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, timer);
    spinner.setAdapter(spinnerAdapter);


    // create a onItemSelectedListener to get the user input from spinner
    spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        @Override
        public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
            //get the selected text from spinner
            String spinnerText = spinner.getSelectedItem().toString();


            //if the value is not "Select your time" then send them to another activity with intent
            if (!spinnerText.contentEquals("Select your time")) {

                Intent intent = new Intent(MainActivity.this, CountDownActivity.class);
                intent.putExtra("time", spinnerText);
                startActivity(intent);
            }
        }

        @Override
        public void onNothingSelected(AdapterView<?> adapterView) {

        }
    });
}

和其他活动

public class CountDownActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_count_down);

    //initialize the ui component
    textView = (TextView) findViewById(R.id.timeText);

    //get the text from intent
    String timerText = getIntent().getStringExtra("time");
    Toast.makeText(CountDownActivity.this, timerText, Toast.LENGTH_SHORT).show();

    //convert the string into integer
    int time = Integer.valueOf(timerText);

    //Initialize a CountDownTimer class with the time data from previous activity
    //which will set the text view with countDown time

    new CountDownTimer(time * 1000, 1000) {
        public void onTick(long millisUntilFinished) {
            //set the remaining time in the textView
            textView.setText("seconds remaining: " + millisUntilFinished / 1000);
        }

        public void onFinish() {
            textView.setText("done!");
        }
    }.start();
}

}

如果您对理解有任何问题或这不能满足您的要求,请随时回复。 快乐编码!!!

【讨论】:

  • 谢谢。我有几个问题。 1)我怎样才能编码,所以时间以分钟为单位? 2) 计时器值中可以有文本吗? (调试器另有说明) 3)是否有延迟计时器的启动直到用户按下按钮?感谢您的帮助!
  • 1.您可以在几分钟内获取用户输入,最后在 CountDownTimer 类中将它们转换为秒。如果您想以分钟为单位显示倒计时,请在 CountDownTimer 类中使用 textView.setText("seconds remaining: " + millisUntilFinished / 1000/60)
  • 3.您可以在按钮单击侦听器或您想要的任何其他侦听器中实例化 CountDownTimer 类。对不起我的英语不好
【解决方案2】:

创建一个包含所需值的数组,将其显示在微调器中,并通过回调通知感兴趣的类所选择的数字。 在那里你应该实现处理数字的逻辑。 您的问题太宽泛,无法更详细地回答。

【讨论】:

    【解决方案3】:

    您需要在 XML 中创建一个字符串数组:

    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <string-array name="planets_array">
            <item>Mercury</item>
            <item>Venus</item>
            <item>Earth</item>
            <item>Mars</item>
            <item>Jupiter</item>
            <item>Saturn</item>
            <item>Uranus</item>
            <item>Neptune</item>
        </string-array>
    </resources>
    

    之后,使用适配器将数组设置为 Spinner:

    Spinner spinner = (Spinner) findViewById(R.id.spinner);
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
        R.array.planets_array, android.R.layout.simple_spinner_item);
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinner.setAdapter(adapter);
    

    显然,不是行星,而是时间。

    更多信息here.

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-22
      • 1970-01-01
      • 2016-06-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      相关资源
      最近更新 更多