自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

Android學(xué)習(xí)筆記:Activity跳轉(zhuǎn)

移動(dòng)開發(fā) Android
Activity就是Android應(yīng)用與用戶的接口,用戶接口是手機(jī)開發(fā)最重要的部分。本文通過(guò)一個(gè)開發(fā)實(shí)例,給大家講解了Android開發(fā)過(guò)程中如何實(shí)現(xiàn)Activity跳轉(zhuǎn)。

手機(jī)開發(fā)最重要的是用戶接口,Android中的Activity就是Android應(yīng)用與用戶的接口!

學(xué)習(xí)Android開發(fā)之前最起碼要學(xué)過(guò)J2SE,因?yàn)槲覀兪怯肑ava語(yǔ)言開發(fā)Android應(yīng)用,當(dāng)然要會(huì)Java語(yǔ)言了。學(xué)習(xí)Java的途徑很多,推薦在網(wǎng)上找些好的視頻邊看邊學(xué)(我曾經(jīng)就是這樣學(xué)java的)。今天的任務(wù)是實(shí)現(xiàn)Activity跳轉(zhuǎn)(就是J2SE中的界面跳轉(zhuǎn)),在PC機(jī)上這個(gè)功能非常簡(jiǎn)單,但是在Android手機(jī)上好像還要費(fèi)一番功夫!

首先來(lái)看看Android應(yīng)用的目錄結(jié)構(gòu):src目錄:這個(gè)不用多說(shuō)是放我們編寫的源代碼的。gen目錄:這個(gè)目錄需要注意,里面有一個(gè)R.java,是定義一些組件ID值的,一般不需要我們修改。接著是assets目錄:這個(gè)目錄可以放一些資源文件,還有個(gè)res目錄:這個(gè)目錄也是放資源文件的,但這里的資源都要在R.java中注冊(cè)ID值,一般是自動(dòng)注冊(cè)的。res目錄下還有幾個(gè)子目錄,前三個(gè)是放圖片的(drawable-hdpi,drawable-ldpi,drawable-mdpi)分別代表不同的分辨率的圖片,layout目錄是存放布局文件的,這個(gè)非常重要,我們要經(jīng)常使用。還有個(gè)values目錄,這里存放一些其他資源的。需要特別注意的是在res目錄以及其子目錄下的文件都需要在R.java里注冊(cè)ID值。還有個(gè)文件非常重要,那就是Android的配置文件AndroidManifest.xml,我們創(chuàng)建的每一個(gè)Activity都要在這個(gè)文件里配置。

下面來(lái)看開發(fā)實(shí)例:功能描述:第一個(gè)Activity里有一行文字和一個(gè)按鈕,當(dāng)點(diǎn)擊按鈕時(shí),界面跳轉(zhuǎn)到第二個(gè)Activity,并將從第一個(gè)Activity里傳來(lái)的值顯示在界面上。下面是源代碼:

HelloActivity.java:

  1. package guxia.android;     
  2.     
  3. import android.app.Activity;     
  4. import android.content.Intent;     
  5. import android.os.Bundle;     
  6. import android.view.View;     
  7. import android.view.View.OnClickListener;     
  8. import android.widget.Button;     
  9. import android.widget.TextView;     
  10.     
  11. public class HelloActivity extends Activity {     
  12.     /** Called when the activity is first created. */    
  13.     @Override    
  14.     public void onCreate(Bundle savedInstanceState) {     
  15.         super.onCreate(savedInstanceState);     
  16.         setContentView(R.layout.main);     
  17.         TextView myTextView=(TextView)findViewById(R.id.myTextView);     
  18.         Button myButton = (Button)findViewById(R.id.myButton);     
  19.         myTextView.setText("welcome to myAndroid");     
  20.         myButton.setText("my Button");     
  21.         myButton.setOnClickListener(new OnClickListener(){     
  22.     
  23.             @Override    
  24.             public void onClick(View arg0) {     
  25.                 Intent intent=new Intent();     
  26.                 intent.putExtra("myname""這是從HelloActivity傳過(guò)來(lái)的值");     
  27.                 intent.setClass(HelloActivity.this, Activity01.class);     
  28.                 HelloActivity.this.startActivity(intent);            
  29.             }                
  30.         });     
  31.     }        
  32. }  

 

Activity01.java:

  1. package guxia.android;     
  2.     
  3. import android.app.Activity;     
  4. import android.content.Intent;     
  5. import android.os.Bundle;     
  6. import android.widget.TextView;     
  7.     
  8. public class Activity01 extends Activity{     
  9.     private TextView myTextView=null;     
  10.     @Override    
  11.     protected void onCreate(Bundle savedInstanceState) {     
  12.         // TODO Auto-generated method stub     
  13.          super.onCreate(savedInstanceState);     
  14.         setContentView(R.layout.android01);     
  15.         myTextView=(TextView)findViewById(R.id.android01TextView);     
  16.         Intent inte=getIntent();     
  17.         String myname=inte.getStringExtra("myname");     
  18.         myTextView.setText(myname);     
  19.     }     
  20.          
  21.     
  22. }   

R.java

  1. /* AUTO-GENERATED FILE.  DO NOT MODIFY.    
  2.  *    
  3.  * This class was automatically generated by the    
  4.  * aapt tool from the resource data it found.  It    
  5.  * should not be modified by hand.    
  6.  */    
  7.     
  8. package guxia.android;     
  9.     
  10. public final class R {     
  11.     public static final class attr {     
  12.     }     
  13.     public static final class drawable {     
  14.         public static final int icon=0x7f020000;     
  15.     }     
  16.     public static final class id {     
  17.         public static final int android01TextView=0x7f050000;     
  18.         public static final int myButton=0x7f050002;     
  19.         public static final int myTextView=0x7f050001;     
  20.     }     
  21.     public static final class layout {     
  22.         public static final int android01=0x7f030000;     
  23.         public static final int main=0x7f030001;     
  24.     }     
  25.     public static final class string {     
  26.         public static final int android01=0x7f040002;     
  27.         public static final int app_name=0x7f040001;     
  28.         public static final int hello=0x7f040000;     
  29.     }     
  30. }   

main.xml(Layout目錄下,HelloActivity的布局文件):

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     >    
  7. <TextView       
  8.     android:id="@+id/myTextView"    
  9.     android:layout_width="fill_parent"      
  10.     android:layout_height="wrap_content"      
  11.     />    
  12.  <Button    
  13.     android:id="@+id/myButton"    
  14.     android:layout_width="fill_parent"    
  15.     android:layout_height="wrap_content"        
  16.     />    
  17. </LinearLayout>   

Activity01.xml(Activity01的布局文件):

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
  3.     android:orientation="vertical"    
  4.     android:layout_width="fill_parent"    
  5.     android:layout_height="fill_parent"    
  6.     >    
  7. <TextView       
  8.     android:id="@+id/android01TextView"    
  9.     android:layout_width="fill_parent"      
  10.     android:layout_height="wrap_content"      
  11.     />    
  12. </LinearLayout>  

strings.xml(values目錄下):

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <resources>    
  3.     <string name="hello">Hello World, HelloActivity!</string>    
  4.     <string name="app_name">helloword</string>    
  5.     <string name="android01">Android</string>    
  6. </resources>   

配置文件AndroidManifest.xml:

  1. <?xml version="1.0" encoding="utf-8"?>    
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"    
  3.       package="guxia.android"    
  4.       android:versionCode="1"    
  5.       android:versionName="1.0">    
  6.     <uses-sdk android:minSdkVersion="8" />    
  7.     
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">    
  9.         <activity android:name=".HelloActivity"    
  10.                   android:label="@string/app_name">    
  11.             <intent-filter>    
  12.                 <action android:name="android.intent.action.MAIN" />    
  13.                 <category android:name="android.intent.category.LAUNCHER" />    
  14.             </intent-filter>    
  15.         </activity>    
  16.         <activity android:name=".Activity01" android:label="@string/android01">    
  17.         </activity>    
  18.          
  19.     </application>    
  20. </manifest>   

【編輯推薦】

  1. Android學(xué)習(xí)筆記:Layout.xml屬性
  2. Android用戶界面設(shè)計(jì):線性布局
  3. Android用戶界面設(shè)計(jì):布局基礎(chǔ)
  4. Android開發(fā)實(shí)例詳解之IMF
  5. Kindle和Android開發(fā)的比較

 

責(zé)任編輯:佚名 來(lái)源: iteye
相關(guān)推薦

2011-05-30 14:00:35

Android Activity Intent

2010-01-28 14:12:20

Android Act

2011-05-30 17:02:56

Android Activity 傳參與跳轉(zhuǎn)

2015-03-30 14:03:13

ActivityGroActivity跳轉(zhuǎn)

2021-09-18 14:45:26

鴻蒙HarmonyOS應(yīng)用

2011-06-03 13:11:19

Android Activity

2011-09-09 13:59:17

Android wid

2011-09-09 16:38:51

Android Wid源碼

2011-09-07 10:34:48

Android Wid

2013-04-03 14:58:43

Android學(xué)習(xí)筆記實(shí)用代碼合集

2011-09-07 11:15:25

2013-01-08 13:33:07

Android開發(fā)Activity入門指南

2015-10-20 15:54:16

android源碼滑動(dòng)關(guān)閉

2012-02-17 17:07:30

Android安全Activity劫持

2011-04-08 09:46:28

Layout.xmlAndroid

2011-03-30 17:32:28

androidmaniAndroid開發(fā)

2010-02-06 10:14:36

Android Act

2014-08-08 10:36:12

ActivityAndroid

2011-06-02 11:13:10

Android Activity

2010-02-05 14:48:04

Android手機(jī)操作
點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)