Android項目頁面跳轉小Demo
近期在做Android項目的開發,剛剛接觸會有非常多新東西須要學習,從環境的搭建到語言的熟悉都是須要一步步完畢的,接下來就拿一個頁面跳轉的樣例來和大家分享一下自己的心得體會。
採用的架構:
Demo中採用的是src/res/Manifest File架構。因為自己是新手。就依照這個傳統的架構來做了。
整體結構:
項目中主要須要在src文件裏寫自己的java類、res的layout文件裏寫自己頁面的xml文件,還有就是在mainifest中完畢對java類的配置。
該樣例實現的是當單擊“helloword”頁面中的button按鈕時跳轉到“second”頁面中,效果例如以下:
跳轉前
跳轉後
首先我們須要建立兩個java類:MainActivity & SecondActivity,代碼例如以下:
【MainActivity】
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example; import android.R.color; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.graphics.pdf.PdfDocument; import android.view.Menu; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; public class MainActivity extends Activity { //定義自己的 private Button btnButton; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnButton = (Button)findViewById(R.id.button1); btnButton.setWidth(200); btnButton.setHeight(50); btnButton.setBackgroundColor(Color.RED); btnButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent); } }); } @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; } } </span>
【SecondActivity】
<span style="font-family:KaiTi_GB2312;font-size:18px;">package com.example; import android.app.Activity; import android.os.Bundle; public class SecondActivity extends Activity{ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); } } </span>
同一時候在layout目錄先相應建立xml文件。以完畢頁面的設計,
【activity_main】
<span style="font-family:KaiTi_GB2312;font-size:18px;"><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".MainActivity" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/textView1" android:layout_marginTop="119dp" android:layout_toRightOf="@+id/textView1" android:text="Button" /> </RelativeLayout></span>
【activity_second】
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?
> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="second" /> </LinearLayout> </span>
接下來須要我們在manifestfile文件裏完畢對建立的兩個java類的配置:
<span style="font-family:KaiTi_GB2312;font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="16" android:targetSdkVersion="17" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.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="com.example.SecondActivity" android:label="@string/app_name" > <intent-filter> </intent-filter> </activity> </application> </manifest> </span>
須要強調的是每加入一個java類。都須要在AndroidManifest中完畢對應的配置!
這樣一個簡單的Android小Demo就成型了。樣例非常easy,很多其它的是對開發環境的熟悉和了解。希望能夠對剛剛接觸Android開發的朋友們有所幫助,有問題也能夠隨時聯系我。
Android項目頁面跳轉小Demo