androidSpinner使用及根据选择项跳转到相应的Activity

androidSpinner使用及根据选择项跳转到相应的ActivityandroidSpinner使用及根据选择项跳转到相应的Activity

1、Spinner使用

给Spinner设置数据的方式有两种,一种直接使用字符串数组,另外一种是使用xml文件

这里只介绍使用xml文件的形式:

首先先在..res\values目录下新建string-array

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="choose">
       <item>iphone5</item>
       <item>V880</item>
       <item>Lumia920</item>
    </string-array>
</resources>


然后使用适配器:

ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.choose, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_choose.setAdapter(adapter);

有时候需要根据Spinner选择的项实现Activity跳转或者获取选择的内容,实现的方式很简单,Spinner提供了setOnItemSelectedListener监听器,下面是使用的示例:

package com.example.homework_3;



import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.AdapterView.OnItemSelectedListener;

public class MainActivity extends Activity {

	private TextView tv_choose;
	private Spinner sp_choose;
	private Button bt_ok;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		tv_choose = (TextView) findViewById(R.id.tv_choose);
		bt_ok = (Button) findViewById(R.id.bt_ok);
		sp_choose = (Spinner) findViewById(R.id.sp_list);

		// 下拉框,xml文件数据
		ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
				this, R.array.choose, android.R.layout.simple_spinner_item);
		adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
		sp_choose.setAdapter(adapter);

		// 实现setOnItemSelectedListener获取选择的下拉菜单的值
		sp_choose.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int arg2, long arg3) {
				// TODO Auto-generated method stub
				String chose = MainActivity.this.getResources().getStringArray(
						R.array.choose)[arg2];
				tv_choose.setText("您选择的手机是:" + chose);
			}

			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
				// TODO Auto-generated method stub

			}
		});

		bt_ok.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub

				// 根据选择的手机型号跳转到相应的Activity

				Intent intent = new Intent();
				switch (sp_choose.getSelectedItemPosition()) {
				case 0:
					intent.setClass(MainActivity.this, Iphone5Activity.class);
					startActivity(intent);
					break;
				case 1:
					intent.setClass(MainActivity.this, V880Activity.class);
					startActivity(intent);
					break;
				case 2:
					intent.setClass(MainActivity.this, Lumia920Activity.class);
					startActivity(intent);
					break;
				}
			}

		});

	}

	@Override
	public 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;
	}

}


你可能感兴趣的:(Activity)