当前位置: 代码迷 >> Android >> Android银幕切换
  详细解决方案

Android银幕切换

热度:74   发布时间:2016-05-01 16:55:16.0
Android屏幕切换

点击“银行”进入“易惠通 - 银行”屏:

?

GridView屏对应MainActivity;ListView屏对应BankActivity。

这两个Activity都必须定义在xml文件中。

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.pervcom.android.eeway"      android:versionCode="1"      android:versionName="1.0">    <application android:icon="@drawable/eeway" android:label="@string/app_name">        <activity android:name="MainActivity"                  android:label="@string/app_name">            <intent-filter>                <action android:name="android.intent.action.MAIN" />                <category android:name="android.intent.category.LAUNCHER" />            </intent-filter>        </activity>        <activity android:name="BankActivity"                  android:label="@string/bank">        </activity>        <activity android:name="EBActivity"                  android:label="@string/ebbank">        </activity>    </application>    <uses-permission android:name="android.permission.INTERNET"></uses-permission>    <uses-sdk android:minSdkVersion="5" /></manifest> 

?

点击“银行”时对应的代码段如下,切换到“易惠通 - 银行”调用的方法是startActivity。

@Overridepublic void onCreate(Bundle savedInstanceState){	super.onCreate(savedInstanceState);	setContentView(R.layout.main);		//取得GridView对象	GridView gridview = (GridView) findViewById(R.id.gridview);	//添加元素给gridview	gridview.setAdapter(new MainAdapter(this));		//事件监听	gridview.setOnItemClickListener(new OnItemClickListener() {		public void onItemClick(AdapterView parent, View v, int position, long id)		{			try{				ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);				if(app.intent!=null){					startActivity(app.intent);				}			}catch(Exception ex){				Log.e(LOG_TAG,"OnItemClickListener",ex);			}		}	});}

?

ListView对应的数据存储在MainAdapter中,定义如下:

public class MainAdapter extends BaseAdapter {	// 定义Context	private Context		mContext;		// 定义易惠通子应用信息	List<ApplicationInfo> appInfos = new LinkedList<ApplicationInfo>();	public MainAdapter(Context c)	{		mContext = c;		buildAppInfos();	}		public void addAppInfo(int iconID, String title, Intent intent){		ApplicationInfo appInfo = new ApplicationInfo();		appInfo.icon = this.mContext.getResources().getDrawable(iconID);		appInfo.icon.setBounds(0, 0, 85, 85);		appInfo.title = title;		appInfo.intent = intent;		appInfos.add(appInfo);	}		public void buildAppInfos()	{		addAppInfo(R.drawable.bank2, "银行", new Intent(mContext,BankActivity.class));		addAppInfo(R.drawable.shopping_cart, "超市", null);		addAppInfo(R.drawable.market, "购物中心", null);		addAppInfo(R.drawable.film_camera_35mm_b, "影剧院", null);		addAppInfo(R.drawable.food2, "餐饮", null);		addAppInfo(R.drawable.boot, "公交", null);		addAppInfo(R.drawable.pay23, "缴费", null);		addAppInfo(R.drawable.set2, "易惠通设置", null);	}		public int getCount() {			return appInfos.size();	}		public Object getItem(int position) {			return appInfos.get(position);			}		public long getItemId(int position) {			return position;	}		public View getView(int position, View convertView, ViewGroup parent) {			ApplicationInfo appInfo = appInfos.get(position);		TextView textView = new TextView(mContext); 		textView.setCompoundDrawables(null, appInfo.icon, null, null);		textView.setText(appInfo.title);		textView.setTextSize(16);		textView.setGravity(Gravity.CENTER);	    textView.setTag(appInfo.intent);					return textView;	}}

?

在本文中,最重要的几行代码是:

ApplicationInfo app = (ApplicationInfo) parent.getItemAtPosition(position);if(app.intent!=null){	startActivity(app.intent);}

?

  相关解决方案