創建一個新的XML文件, res/values/arrays.xml
<?xml version="1.0" encoding="UTF-8"?>
<resources>
<string-array name="dayOfWeek">
<item>Sunday</item>
<item>Monday</item>
<item>Tuesday</item>
<item>Wednesday</item>
<item>Thursday</item>
<item>Friday</item>
<item>Saturday</item>
</string-array>
</resources>
修改主程序:
package com.AndroidSpinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.Toast;
public class AndroidSpinner extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
Button myButton = (Button)findViewById(R.id.button);
ArrayAdapter<CharSequence> adapter =
ArrayAdapter.createFromResource(this, R.array.dayOfWeek,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mySpinner.setAdapter(adapter);
myButton.setOnClickListener(
new Button.OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast
.makeText(AndroidSpinner.this,
(CharSequence) mySpinner.getSelectedItem(),
Toast.LENGTH_LONG)
.show();
}
});
}
}
main.xml跟原先的一樣; 可參考下拉列表(Spinner).
沒有留言:
發佈留言