【发布时间】:2016-08-18 05:25:38
【问题描述】:
我正在创建一个锻炼应用程序,我到处搜索,试图找到如何创建一个显示持续时间的微调器,当用户选择持续时间时,所选时间将出现在倒计时计时器上在另一个活动中。感谢您的帮助!
【问题讨论】:
-
These 都是很好的例子。
标签: java android timer spinner
我正在创建一个锻炼应用程序,我到处搜索,试图找到如何创建一个显示持续时间的微调器,当用户选择持续时间时,所选时间将出现在倒计时计时器上在另一个活动中。感谢您的帮助!
【问题讨论】:
标签: java android timer spinner
希望这段代码能帮助你得到你想要的东西
在你的第一个活动中初始化一个微调器
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();
}
}
如果您对理解有任何问题或这不能满足您的要求,请随时回复。 快乐编码!!!
【讨论】:
创建一个包含所需值的数组,将其显示在微调器中,并通过回调通知感兴趣的类所选择的数字。 在那里你应该实现处理数字的逻辑。 您的问题太宽泛,无法更详细地回答。
【讨论】:
您需要在 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.
【讨论】: