layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
string.xml
<resources>
<string name="app_name">SpinnerDemo</string>
<string-array name="lunch">
<item>雞腿飯</item>
<item>魯肉飯</item>
<item>排骨飯</item>
<item>水餃</item>
<item>陽春麵</item>
</string-array>
</resources>
java-code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
ArrayAdapter<CharSequence> lunchList = ArrayAdapter.createFromResource(MainActivity.this,
R.array.lunch,
android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(lunchList);
}
}
simple_spinner_dropdown_item
Android 所提供的布局方式
https://developer.android.com/reference/android/R.layout.html#simple_spinner_dropdown_item
https://developer.android.com/reference/android/R.layout.html#simple_spinner_dropdown_item
2.動態選項
ayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin">
<Spinner
android:id="@+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
java-code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner);
final String[] lunch = {"雞腿飯", "魯肉飯", "排骨飯", "水餃", "陽春麵"};
ArrayAdapter<String> lunchList = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
lunch);
spinner.setAdapter(lunchList);
}
}
--------------------------------------------------------------------------------
java all
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//動態選單
Spinner spinner = (Spinner)findViewById(R.id.spinner);
final String[] lunch = {"雞腿飯", "魯肉飯", "排骨飯", "水餃", "陽春麵"};
ArrayAdapter<String> lunchList = new ArrayAdapter<>(MainActivity.this,
android.R.layout.simple_spinner_dropdown_item,
lunch);
spinner.setAdapter(lunchList);
//選單動作
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "你選的是" + lunch[position], Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
onItemSelected事件中所代表的是當使用者按下 Spinner 的某個 item
該怎麼處理它們,
該怎麼處理它們,
沒有留言:
張貼留言