如何创建一个可以 select 倒计时的微调器
How to create a spinner that one can select a time to countdown from
我正在创建一个锻炼应用程序,我到处搜索以尝试找到如何创建一个显示持续时间的微调器,当用户选择持续时间时,所选时间将显示在另一个 activity 中的倒数计时器。感谢您的帮助!
创建一个包含所需值的数组,将其显示在微调器中,并通过有关所选数字的回调通知感兴趣的 class。
在那里你应该实现处理数字的逻辑。
您的问题太宽泛,无法更详细地回答。
您需要在 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。
希望这段代码能帮助您得到您想要的东西
在您的第一个 activity 中初始化微调器
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) {
}
});
}
和另一个activity
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();
}
}
如果您在理解上有任何问题,或者这不能满足您的要求,请不要犹豫回复。
快乐编码!!!
我正在创建一个锻炼应用程序,我到处搜索以尝试找到如何创建一个显示持续时间的微调器,当用户选择持续时间时,所选时间将显示在另一个 activity 中的倒数计时器。感谢您的帮助!
创建一个包含所需值的数组,将其显示在微调器中,并通过有关所选数字的回调通知感兴趣的 class。 在那里你应该实现处理数字的逻辑。 您的问题太宽泛,无法更详细地回答。
您需要在 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。
希望这段代码能帮助您得到您想要的东西
在您的第一个 activity 中初始化微调器
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) {
}
});
}
和另一个activity
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();
}
}
如果您在理解上有任何问题,或者这不能满足您的要求,请不要犹豫回复。 快乐编码!!!