java源码如下:
1、MainActivity.java源码
package com.example.responsetest;
import android.app.Activity;
import android.content.Intent;
import android.content.Loader.ForceLoadContentObserver;
import android.graphics.Color;
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.ImageButton;
import android.widget.TextView; //方法六:通用场景,特别是某个控件的点击方法需要实现的逻辑较为复杂的时候
public class MainActivity extends Activity{
TextView textview;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview=(TextView)findViewById(R.id.textview);//1.初始化值
}
public void onTextViewClick(View view) { //2.传入形参
//跳转页面
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent); //textview.setTextColor(Color.GREEN);//点击变色
}
} /*
//方法五:适用于不同类型的控件,使需要实现的功能在一个onClick()方法中实现
public class MainActivity extends Activity implements OnClickListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
ImageButton button3=(ImageButton)findViewById(R.id.imagebutton);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
button3.setOnClickListener(this);
} @Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
case R.id.button2:
case R.id.imagebutton:
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
*/ /*
//方法四:与方法三差别不大:
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1=(Button)findViewById(R.id.button1);
Button button2=(Button)findViewById(R.id.button2);
button1.setOnClickListener(new MyOnClick());
button2.setOnClickListener(new MyOnClick());
}
private class MyOnClick implements OnClickListener{
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
case R.id.button2:
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
default:
break;
}
}
}
}
*/
/*
//方法三:适用于多个同类型的控件(比如Button)等
public class MainActivity extends Activity{
Button[] buttons;//定义一个Button数组,封装多个同类型的Button控件(成员变量有默认的初始值)
//初始化函数
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons=new Button[]{(Button)findViewById(R.id.button1),(Button)findViewById(R.id.button2)};
for(Button button:buttons) {
button.setOnClickListener(listener);
}}
//成员变量:
private OnClickListener listener=new OnClickListener() {
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.button1:
case R.id.button2:
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
break;
default:
break;
}}};
}
*/ /* public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button=(Button)findViewById(R.id.button2);
button.setOnClickListener(new MyOnclickListener());
}
//事件响应方法二:定义内部类实现监听接口
public class MyOnclickListener implements OnClickListener{
@Override
public void onClick(View arg0) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
}
} */
/*
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); //事件响应方法一:使用匿名内部类,适用于任何场景
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});;
}
}
*/
2、SecondActivity.java源码
package com.example.responsetest; import android.app.Activity;
import android.os.Bundle; public class SecondActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
xml代码如下:
1、activity_main.xml代码(第一界面):
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:id="@+id/main_activity"
4 android:layout_width="match_parent"
5 android:layout_height="match_parent"
6 android:orientation="vertical"
7 android:paddingBottom="@dimen/activity_vertical_margin"
8 android:paddingLeft="@dimen/activity_horizontal_margin"
9 android:paddingRight="@dimen/activity_horizontal_margin"
10 android:paddingTop="@dimen/activity_vertical_margin"
11 tools:context="com.example.responsetest.MainActivity" >
12
13 <Button
14 android:id="@+id/button1"
15 android:layout_width="match_parent"
16 android:layout_height="wrap_content"
17 android:text="匿名内部类" />
18
19 <Button
20 android:id="@+id/button2"
21 android:layout_width="match_parent"
22 android:layout_height="wrap_content"
23 android:text="内部类"/>
24 <ImageButton
25 android:id="@+id/imagebutton"
26 android:layout_width="100dip"
27 android:layout_height="100dip"
28 android:scaleType="fitCenter"
29 android:src="@drawable/ic_launcher"
30 />
31 <TextView
32 android:id="@+id/textview"
33 android:layout_width="match_parent"
34 android:layout_height="wrap_content"
35 android:onClick="onTextViewClick"
36 android:text="点击事件"
37 android:textSize="50sp"
38 />
39
40 </LinearLayout>
2、activity_second.xml源码(第二界面):
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
2 xmlns:tools="http://schemas.android.com/tools"
3 android:layout_width="match_parent"
4 android:layout_height="match_parent"
5 android:paddingBottom="@dimen/activity_vertical_margin"
6 android:paddingLeft="@dimen/activity_horizontal_margin"
7 android:paddingRight="@dimen/activity_horizontal_margin"
8 android:paddingTop="@dimen/activity_vertical_margin"
9 tools:context="com.example.responsetest.MainActivity" >
10
11 <TextView
12 android:id="@+id/secondactivity"
13 android:layout_width="match_parent"
14 android:layout_height="wrap_content"
15 android:text="欢迎您" />
16
17 </LinearLayout>
3、AndroidManifest.xml源码:
1 <?xml version="1.0" encoding="utf-8"?>
2 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3 package="com.example.responsetest"
4 android:versionCode="1"
5 android:versionName="1.0" >
6
7 <uses-sdk
8 android:minSdkVersion="18"
9 android:targetSdkVersion="22" />
10
11 <application
12 android:allowBackup="true"
13 android:icon="@drawable/ic_launcher"
14 android:label="@string/app_name"
15 android:theme="@style/AppTheme" >
16 <activity
17 android:name=".MainActivity"
18 android:label="@string/app_name" >
19 <intent-filter>
20 <action android:name="android.intent.action.MAIN" />
21
22 <category android:name="android.intent.category.LAUNCHER" />
23 </intent-filter>
24 </activity>
25 <activity
26 android:name="com.example.responsetest.SecondActivity"
27 android:excludeFromRecents="true"
28 android:noHistory="true"/>
29 </application>
30
31 </manifest>
代码分析:
1.两个java类:
MainActivity.java :主类,代码的主要执行者,操作第一界面
SecondActivity.java:跳转后的页面,操作第二界面
2.三个xml文件:
activity_main.xml:布局第一个手机界面
activity_second.xml:布局第二个界面(跳转后的界面)
AndroidManifest.xml:Android代码的执行入口