一、Hello Android
1. AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
< manifest xmlns: android= " http://schemas.android.com/apk/res/android"
package = " com.qs.helloandroid"
android: versionCode= " 1"
android: versionName= " 1.0" >
< uses-sdk
android: minSdkVersion= " 18"
android: targetSdkVersion= " 18" />
< application
android: allowBackup= " true"
android: icon= " @drawable/ic_launcher"
android: label= " @string/app_name"
android: theme= " @style/AppTheme" >
< 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>
</ application>
</ manifest>
2. MainActivity
package com. qs. helloandroid ;
import android. app. Activity ;
import android. os. Bundle ;
import android. util. Log ;
import android. view. View ;
import android. widget. Button ;
import android. widget. Toast ;
public class MainActivity extends Activity {
private Button button_download;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
Log . i ( "i" , "打印日志信息" ) ;
Log . e ( "e" , "打印错误信息" ) ;
setContentView ( R . layout. activity_main) ;
button_download = ( Button ) findViewById ( R . id. button_download) ;
button_download. setOnClickListener ( new View. OnClickListener ( ) {
@Override
public void onClick ( View v) {
Toast toast = Toast . makeText ( MainActivity . this , "开始下载..." , Toast . LENGTH_LONG) ;
toast. show ( ) ;
button_download. setText ( "正在下载中..." ) ;
}
} ) ;
}
}
3. activity_main.xml
< 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"
tools: context= " ${relativePackage}.${activityClass}" >
< Button
android: id= " @+id/button_download"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_alignParentRight= " true"
android: layout_below= " @+id/textView1"
android: text= " @string/download" />
< TextView
android: id= " @+id/textView1"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentTop= " true"
android: layout_centerHorizontal= " true"
android: text= " @string/home"
android: textSize= " 30sp" />
</ RelativeLayout>
4. size_text.xml
<?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= " 200dp"
android: layout_height= " wrap_content"
android: text= " 骑士一"
android: background= " #ff0000"
android: textSize= " 20sp" />
< TextView
android: id= " @+id/textView2"
android: layout_width= " 200px"
android: layout_height= " wrap_content"
android: text= " 骑士二"
android: background= " #00ff00"
android: textSize= " 20dp" />
</ LinearLayout>
5. strings.xml
<?xml version="1.0" encoding="utf-8"?>
< resources>
< string name = " app_name" > HelloAndroid</ string>
< string name = " hello_world" > Hello world!</ string>
< string name = " home" > 骑士Home!</ string>
< string name = " download" > 下载</ string>
</ resources>
6. styles.xml
< resources>
< style name = " AppBaseTheme" parent = " android:Theme.Light" >
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</ style>
< style name = " AppTheme" parent = " AppBaseTheme" >
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</ style>
</ resources>
二、页面跳转
1. MainActivity
package com. qs. activity ;
import android. app. Activity ;
import android. os. Bundle ;
import android. view. Menu ;
import android. view. MenuItem ;
import android. view. View ;
import android. view. View. OnClickListener ;
import android. widget. Button ;
import android. widget. EditText ;
import android. widget. Toast ;
public class MainActivity extends Activity implements OnClickListener {
private EditText text_main_message;
private Button button_main_start_1;
private Button button_main_start_2;
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_main) ;
text_main_message = ( EditText ) findViewById ( R . id. text_main_message) ;
button_main_start_1 = ( Button ) findViewById ( R . id. button_main_start_1) ;
button_main_start_2 = ( Button ) findViewById ( R . id. button_main_start_2) ;
button_main_start_1. setOnClickListener ( this ) ;
button_main_start_2. setOnClickListener ( this ) ;
}
@Override
public void onClick ( View v) {
if ( v == button_main_start_1) {
Toast . makeText ( this , "一般启动" , Toast . LENGTH_LONG) . show ( ) ; ;
text_main_message. setText ( "骑士家" ) ;
button_main_start_1. setText ( "一般启动后" ) ;
} else if ( v == button_main_start_2) {
Toast . makeText ( this , "带回调启动" , Toast . LENGTH_SHORT) . show ( ) ; ;
}
}
}
2. activity_main.xml
< 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"
tools: context= " ${relativePackage}.${activityClass}" >
< EditText
android: id= " @+id/text_main_message"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_alignParentRight= " true"
android: layout_alignParentTop= " true"
android: ems= " 10"
android: hint= " 请输入消息!" >
< requestFocus />
</ EditText>
< Button
android: id= " @+id/button_main_start_1"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_below= " @+id/text_message"
android: text= " 一般启动" />
< Button
android: id= " @+id/button_main_start_2"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_below= " @+id/button_start_1"
android: text= " 带回调启动" />
</ RelativeLayout>
3. SecondActivity
package com. qs. activity ;
import android. app. Activity ;
import android. os. Bundle ;
import android. view. Menu ;
import android. view. MenuItem ;
public class SecondActivity extends Activity {
@Override
protected void onCreate ( Bundle savedInstanceState) {
super . onCreate ( savedInstanceState) ;
setContentView ( R . layout. activity_second) ;
}
}
4. activity_second.xml
< 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"
tools: context= " ${relativePackage}.${activityClass}" >
< EditText
android: id= " @+id/text_second_message"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_alignParentRight= " true"
android: layout_alignParentTop= " true"
android: ems= " 10"
android: hint= " 请输入消息!" >
< requestFocus />
</ EditText>
< Button
android: id= " @+id/button_second_back_1"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_below= " @+id/text_second_message"
android: text= " 一般返回" />
< Button
android: id= " @+id/button_second_back_2"
android: layout_width= " wrap_content"
android: layout_height= " wrap_content"
android: layout_alignParentLeft= " true"
android: layout_below= " @+id/button_second_back_1"
android: text= " 带结果返回" />
</ RelativeLayout>