An example of using the spinner drop down list in the anroid development tutorial
- 2020-06-01 10:58:13
- OfStack
First, add the Spinner control to the xml file:
<Spinner
android:id="@+id/mySpinner"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
/>
Write in Activity:
package com.example.spinner;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends Activity {
private Spinner spinner;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
spinner = (Spinner)this.findViewById(R.id.mySpinner);
// Set data source
String[] mydata = {" Beijing ", " Shanghai "," guangdong "," guangxi "};
// The statement 1 a ArrayAdapter And associate the data source with it
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,mydata);
// Sets the style of the pop-up drop-down list
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// will arrayAdapter Object add in Spinner Go to the
spinner.setAdapter(adapter);
// Add listener
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
Toast.makeText(MainActivity.this, " Select the: " + arg2, Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
}
}