Android中下拉列表框操作
16lz
2021-01-23
public class MainActivity extends Activity {//声明spinner对象private Spinner spinner;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//根据id获取对象spinner = (Spinner) findViewById(R.id.spinner1);//显示的数据final String arr[]=new String[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};//adpater对象//ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, arr);//List<? extends Map<String, ?>> List<Map<String,Object>> data = new ArrayList<Map<String,Object>>();Map<String, Object> map = new HashMap<String, Object>();map.put("logo", R.drawable.ic_launcher);map.put("text", "xxxx说明操作");Map<String, Object> map2 = new HashMap<String, Object>();map2.put("logo", R.drawable.ic_launcher);map2.put("text", "xxx说明xxx");data.add(map);data.add(map2);SimpleAdapter simpleAdapter = new SimpleAdapter(this, data, R.layout.activity_item, new String[]{"logo","text"}, new int[]{R.id.imageView1,R.id.textView1});//设置显示的数据//spinner.setAdapter(arrayAdapter);spinner.setAdapter(simpleAdapter);Toast.makeText(getApplicationContext(), "main Thread"+spinner.getItemAtPosition(spinner.getSelectedItemPosition()), Toast.LENGTH_LONG).show();//注册事件spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {@Overridepublic void onItemSelected(AdapterView<?> parent, View view,int position, long id) {Spinner spinner = (Spinner) parent;Toast.makeText(getApplicationContext(), "xxxx"+spinner.getItemAtPosition(position), Toast.LENGTH_LONG).show();}@Overridepublic void onNothingSelected(AdapterView<?> parent) {Toast.makeText(getApplicationContext(), "没有改变的处理", Toast.LENGTH_LONG).show();}});}@Overridepublic boolean onCreateOptionsMenu(Menu menu) {// Inflate the menu; this adds items to the action bar if it is present.getMenuInflater().inflate(R.menu.main, menu);return true;}}
更多相关文章
- android异步取数据
- Android数据存储路径位置
- android中Json数据保存方式
- [置顶] Android加载数据过程中的菊花显示
- android webview 中网页数据与js交互
- Android 解决 RecyclerView 嵌套 ScrollView 数据显示不全的问题
- 通过CMD 查看android APP数据库表
- android 利用socket 发送Json数据demo
- Android 简单数据库(增删改查)