如何创build一个下拉列表?
我在Android的编程新手,我想知道如何创build一个下拉列表,我试过一个滚动查看,但它不完全是我所需要的。
最好的方法是:
预习:
XML:
<Spinner android:id="@+id/spinner1" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@android:drawable/btn_dropdown" android:spinnerMode="dropdown" />
Java的:
//get the spinner from the xml. Spinner dropdown = (Spinner)findViewById(R.id.spinner1); //create a list of items for the spinner. String[] items = new String[]{"1", "2", "three"}; //create an adapter to describe how the items are displayed, adapters are used in several places in android. //There are multiple variations of this, but this is the basic variant. ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items); //set the spinners adapter to the previously created one. dropdown.setAdapter(adapter);
更多信息:
这是基础知识,但还有更多需要自学的实验。
https://developer.android.com/guide/topics/ui/controls/spinner.html
微调xml:
<Spinner android:id="@+id/spinner" android:layout_width="wrap_content" android:layout_height="match_parent" />
Java的:
public class MainActivity extends Activity implements OnItemSelectedListener{ private Spinner spinner; private static final String[]paths = {"item 1", "item 2", "item 3"}; @Override protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); spinner = (Spinner)findViewById(R.id.spinner); ArrayAdapter<String>adapter = new ArrayAdapter<String>(MainActivity.this, android.R.layout.simple_spinner_item,paths); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); spinner.setOnItemSelectedListener(this); } public void onItemSelected(AdapterView<?> parent, View v, int position, long id) { switch (position) { case 0: // Whatever you want to happen when the first item gets selected break; case 1: // Whatever you want to happen when the second item gets selected break; case 2: // Whatever you want to happen when the thrid item gets selected break; } } }
这是它的代码。
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Spinner android:id="@+id/static_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginBottom="20dp" android:layout_marginTop="20dp" /> <Spinner android:id="@+id/dynamic_spinner" android:layout_width="fill_parent" android:layout_height="wrap_content" />
strings.xml中
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Ahotbrew.com - Dropdown</string> <string-array name="brew_array"> <item>Cappuccino</item> <item>Espresso</item> <item>Mocha</item> <item>Caffè Americano</item> <item>Cafe Zorro</item> </string-array>
主要活动
Spinner staticSpinner = (Spinner) findViewById(R.id.static_spinner); // Create an ArrayAdapter using the string array and a default spinner ArrayAdapter<CharSequence> staticAdapter = ArrayAdapter .createFromResource(this, R.array.brew_array, android.R.layout.simple_spinner_item); // Specify the layout to use when the list of choices appears staticAdapter .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); // Apply the adapter to the spinner staticSpinner.setAdapter(staticAdapter); Spinner dynamicSpinner = (Spinner) findViewById(R.id.dynamic_spinner); String[] items = new String[] { "Chai Latte", "Green Tea", "Black Tea" }; ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, items); dynamicSpinner.setAdapter(adapter); dynamicSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { Log.v("item", (String) parent.getItemAtPosition(position)); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub } });
这个例子来自http://www.ahotbrew.com/android-dropdown-spinner-example/
你需要一个Spinner
。 这里是一个例子:
spinner_1 = (Spinner) findViewById(R.id.spinner1); spinner_1.setOnItemSelectedListener(this); List<String> list = new ArrayList<String>(); list.add("RANJITH"); list.add("ARUN"); list.add("JEESMON"); list.add("NISAM"); list.add("SREEJITH"); list.add("SANJAY"); list.add("AKSHY"); list.add("FIROZ"); list.add("RAHUL"); list.add("ARJUN"); list.add("SAVIYO"); list.add("VISHNU"); ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_1.setAdapter(adapter); spinner_2 = (Spinner) findViewById(R.id.spinner_two); spinner_2.setOnItemSelectedListener(this); List<String> city = new ArrayList<String>(); city.add("KASARGOD"); city.add("KANNUR"); city.add("THRISSUR"); city.add("KOZHIKODE"); city.add("TRIVANDRUM"); city.add("ERNAMKULLAM"); city.add("WAYANAD"); city.add("PALAKKAD"); city.add("ALAPUZHA"); city.add("IDUKKI"); city.add("KOTTAYAM"); city.add("PATHANAMTHITTA"); city.add("KOLLAM"); city.add("MALAPPURAM"); ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, city); adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner_2.setAdapter(adapter2); @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub Toast.makeText(this, "YOUR SELECTION IS : " + parent.getItemAtPosition(position).toString(), Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { // TODO Auto-generated method stub }
这段代码对我来说很好,希望它对你也有帮助。
item.xml
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="1dip" > <TextView android:id="@+id/spinnerItem" android:layout_width="match_parent" android:layout_height="wrap_content" android:padding="2dp" android:textSize="12sp" > </TextView> </RelativeLayout>
details.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:layout_marginBottom="20dp" android:background="#FF00FF"> <Spinner android:id="@+id/dropStatus" android:layout_width="250dp" android:layout_height="30dp" android:layout_marginBottom="7dp" android:drawSelectorOnTop="true"/> </LinearLayout>
适配器类:
import java.util.ArrayList; import android.content.Context; import android.content.res.Resources; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.TextView; public class StatusAdapter extends ArrayAdapter<StatusBean> { private Context context; private ArrayList<StatusBean> statuses; public Resources res; StatusBean currRowVal = null; LayoutInflater inflater; public StatusAdapter(Context context, int textViewResourceId, ArrayList<StatusBean> statuses, Resources resLocal) { super(context, textViewResourceId, statuses); this.context = context; this.statuses = statuses; this.res = resLocal; inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); } @Override public View getDropDownView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } @Override public View getView(int position, View convertView, ViewGroup parent) { return getCustomView(position, convertView, parent); } public View getCustomView(int position, View convertView, ViewGroup parent) { View row = inflater.inflate(R.layout.status_item, parent, false); currRowVal = null; currRowVal = (StatusBean) statuses.get(position); TextView label = (TextView) row.findViewById(R.id.spinnerItem); if (position == 0) { label.setText("Please select status"); } else { label.setText(currRowVal.getStatus()); } return row; } }
StatusBean类:
public class StatusBean { private String status; private String statusCode; public StatusBean() { } public StatusBean(String status, String statusCode) { this.status = status; this.statusCode = statusCode; } public String getStatus() { return status; } public void setStatus(String status) { this.status = status; } public String getStatusCode() { return statusCode; } public void setStatusCode(String statusCode) { this.statusCode = statusCode; } }
活动课:
里面onCreate方法:
static ArrayList<StatusBean> STATUS_LIST = new ArrayList<StatusBean>(); for(int i=0;i<=10;i++) { STATUS_LIST.add(new StatusBean(“Status ”+i, “Stattus ”+i)); } final Spinner dropStatus = (Spinner)findViewById(R.id.dropStatus); Resources res = getResources(); StatusAdapter adapter = new StatusAdapter(this, R.layout.item, SessionData. STATUS_LIST, res); dropStatus.setAdapter(adapter);
下拉菜单在Android中被称为spinners。 最好的指导是 –
http://developer.android.com/guide/topics/ui/controls/spinner.html
在Android中创buildCombobox非常简单。 在Android中称为Spinner。
第1步:将Spinner添加到XML中
第2步:创buildstring数组到string.xml
步骤3:右击Java代码来实现Spinner的点击事件。
这里是完整的一步一步的指导与video。 点击这里