Android手機軟體開發介面跳轉一例

來源:互聯網
上載者:User

 

Android手機軟體開發介面跳轉一例 

 

開發環境配置: 

window server 2008 

Eclipse 3.7  

JDK1.6 

Android2.2 

ADT14 

 

//應用程式配置xml檔案 

//AndroidManifest.xml 

//-------------------------------------------------------------------------------- 

<?xml version="1.0" encoding="utf-8"?> 

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

    package="LC.HelloWorld" 

    android:versionCode="1" 

    android:versionName="1.0" > 

    <uses-sdk android:minSdkVersion="7" /> 

    <application 

        android:icon="@drawable/ic_launcher" 

        android:label="@string/app_name" > 

        <activity  

            android:name=".SecPage" 

            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:label="@string/app_name" 

            android:name=".HelloWorldActivity" >             

        </activity> 

        <activity 

            android:label="@string/app_name" 

            android:name=".OtherActivity" >  

        </activity> 

    </application> 

</manifest> 

 

//手機介面布局xml檔案 

res/layout/main.xml 

//---------------------------------------------------------------------------- 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

    <TextView 

        android:id="@+id/txt_display" 

        android:layout_width="fill_parent" 

        android:layout_height="wrap_content" 

        android:text="@string/hello" /> 

    <TextView 

            android:id="@+id/myTextView" 

            android:layout_width="170dp" 

            android:layout_height="wrap_content" 

            android:text="TextView" />     

    <Button 

        android:id="@+id/myButton" 

        android:layout_width="fill_parent" 

        android:layout_height="69dp" 

        android:text="Button" /> 

</LinearLayout> 

 

res/layout/other.xml 

//------------------------------------------------------------------------- 

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:layout_width="fill_parent" 

    android:layout_height="fill_parent" 

    android:orientation="vertical" > 

    <TextView 

        android:id="@+id/myTextView2" 

        android:layout_width="170dp" 

        android:layout_height="wrap_content" 

        android:text="TextView" /> 

    <Button 

        android:id="@+id/btn_back" 

        android:layout_width="wrap_content" 

        android:layout_height="wrap_content" 

        android:text="back回退" /> 

</LinearLayout> 

 

res/values/strings.xml 

//------------------------------------------------------------------------- 

<?xml version="1.0" encoding="utf-8"?> 

<resources> 

    <string name="hello">Hello World, HelloWorldActivity!</string> 

    <string name="app_name">HelloWorld</string> 

    <string name="other">otherActivity</string> 

</resources> 

 

//原始碼 

//SecPage.java 

//------------------------------------------------------------------------- 

package LC.HelloWorld; 

 

import android.app.Activity;  //是Activity所在的包 

import android.content.DialogInterface; 

import android.content.DialogInterface.OnClickListener; 

import android.content.Intent; 

import android.os.Bundle;     //用於映射字串的值 

import android.view.View; 

import android.widget.Button; 

import android.widget.TextView; 

 

public class SecPage extends Activity 

    Button myButton=null; 

    @Override 

    public void onCreate(Bundle savedInstanceState)  

    { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.main); //設定當前視圖為main主介面布局R.Layout.main 

        //給按鈕添加單擊事件 

         myButton=(Button)this.findViewById(R.id.myButton); 

         myButton.setText("跳轉到另一個介面Activity"); 

         myButton.setOnClickListener(new MyButtonListener()); 

    } 

    class MyButtonListener implements android.view.View.OnClickListener 

    { 

        @Override 

        public  void onClick(View v) 

        { 

             //產生一個Intent對象 

            //intent是提供Activity直接互動的抽象描述,可以被startActivty運行。 

            //intent提供了一個媒介,提供組件的相互調用, 

            //實現調用者和被調用者之間的解耦. 

            Intent intent=new Intent(); 

            intent.setClass(SecPage.this,OtherActivity.class); 

            intent.putExtra("testExtra", "haha我開始跳了哦!"); 

            SecPage.this.startActivity(intent); 

        }        

    }    

 

//OtherActivity.java 

//--------------------------------------------------------------------------- 

/**

 * 

 */ 

package LC.HelloWorld; 

 

import android.app.Activity; 

import android.content.Intent; 

import android.view.View; 

import android.os.Bundle; 

import android.widget.TextView; 

import android.widget.Button; 

 

/**

 * @author Administrator

 *

 */ 

public class OtherActivity extends Activity { 

     

    @Override 

    protected void onCreate(Bundle savedInstanceState) 

    {        

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.other); 

        //設定 

        TextView myTextView=null; 

        //擷取上頁設定的擴充參數? 

        Intent intent=this.getIntent(); 

        String mytext=intent.getStringExtra("testExtra"); 

        if(mytext!=null && mytext.length()<=0) 

        { 

            mytext="haha_1"; 

        } 

        mytext+=",我已跳過來了!"; 

        //設定文字框的內容 

        myTextView=(TextView)this.findViewById(R.id.myTextView2); 

        myTextView.setText(mytext); 

         

        //btn_back 添加回退按鈕單擊處理事件 

        Button btn_back=(Button)this.findViewById(R.id.btn_back); 

        btn_back.setOnClickListener(new BackClickListener()); 

    } 

    //定義添加回退按鈕單擊處理事件 

    class BackClickListener implements android.view.View.OnClickListener 

    { 

        @Override 

        public  void onClick(View v) 

        { 

            Intent intent=new Intent(); 

            intent.setClass(OtherActivity.this,SecPage.class); 

            intent.putExtra("testExtra", "haha我開始跳了哦!"); 

            OtherActivity.this.startActivity(intent); 

        } 

    } 

//--the---end---- 

//2011-11-17 

//vp:hsg

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.