我正在創建一個練習應用程序,並且我逐字逐字地搜索,試圖找到如何創建一個顯示持續時間的微調,以及當用戶選擇持續時間,則選定的時間將出現在另一個活動的倒數計時器中。謝謝您的幫助!如何創建一個微調器,可以從
-2
A
回答
0
希望這個代碼可以幫助你得到的正是你想要的
在您的第一項活動初始化微調
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();
}
}
如果您有任何問題的理解或這不符合您的要求請做毫不猶豫地回覆。 快樂編碼!
0
創建一個具有所需值的數組,並將其顯示在微調框中,並通過有關選定數字的回調通知感興趣的類。 在那裏你應該實現處理數字的邏輯。 您的問題太寬泛,無法更詳細地回答。
0
你需要在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) 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。
[這些](https://www.mkyong.com/android/android-spinner-drop-down-list-example/)都是很好的例子。 – CookieMonster