Android下拉框如何实现?
扫描二维码
随时随地手机看文章
Android下拉框
Activity_main.xml
MainActivity.java
package pw.gutao.android_08_0628; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.AutoCompleteTextView; import android.widget.MultiAutoCompleteTextView; import android.widget.Spinner; import android.widget.Toast; public class MainActivity extends AppCompatActivity { private AutoCompleteTextView autoCompleteTextView; private MultiAutoCompleteTextView multiAutoCompleteTextView; private Spinner spinner; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //获取下拉列表 spinner = (Spinner) findViewById(R.id.s_main_1); //定义数组,保存省份 final String[] provinces = {"湖南","湖北","河南","河北","山东","山西"}; //创建适配器 ArrayAdapter adapters = new ArrayAdapter(this,android.R.layout.simple_list_item_1,provinces); //设置适配器 spinner.setAdapter(adapters); //获取当前选中的选项 spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { Toast.makeText(MainActivity.this,provinces[position]+"省",Toast.LENGTH_SHORT).show(); } @Override public void onNothingSelected(AdapterView parent) { } }); } }