上周五又照虎画猫的学习了一下 , Android的 Activity 事件..
代码还是自已写出来的, 记忆比较深刻.. 作为一个小的积累, 记录一下.
Android 的 Activity 需要在 AndroidMainfest.xml 中注册, 注册方法 也很简单:
如下.
在xml中添加<activity> 来声明.
<activity
android:name=".Activity2"
android:label="Activity2">
</activity>
Activity01: Activity02:
代码如下:
src/main/AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.youtwo.testactivity">
<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>
<activity
android:name=".Activity2"
android:label="Activity2">
</activity>
</application>
</manifest>
layout/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" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/textView"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用Activity02"
android:id="@+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="调用外部Activity"
android:id="@+id/button2" />
</LinearLayout>
</RelativeLayout>
layout/activity2.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">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:layout_width="115dp"
android:layout_height="wrap_content"
android:text="后退"
android:id="@+id/btnBack" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/imageView"
android:src="#ff5d7d59" />
</LinearLayout>
</LinearLayout>
com/example/youtwo/testactivity/MainActivity.java
package com.example.youtwo.testactivity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
Button btnActivity_1;
Button btnOutside;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.e("Activity1", "onCreate()");
// 初始化按钮
btnActivity_1 = (Button) this.findViewById(R.id.button1);
btnActivity_1.setOnClickListener(new ClickEvent());
btnOutside = (Button) this.findViewById(R.id.button2);
btnOutside.setOnClickListener(new ClickEvent());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onDestroy() {
super.onDestroy();
Log.e("Activity1", "onDestroy()");
}
@Override
public void onStart() {
super.onStart();
Log.e("Activity1", "onStart()");
}
@Override
public void onResume() {
super.onResume();
Log.e("Activity1", "onResume()");
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if (v == btnActivity_1) {
Intent intent = new Intent();
intent.setClass(MainActivity.this, Activity2.class);
// // new一个Bundle对象,并将要传递的数据传入
// Bundle bundle = new Bundle();
// // bundle.putString("Text",tbBundle.getText().toString());
//
// // 将Bundle对象assign给Intent
// intent.putExtras(bundle);
// 调用Activity2
startActivity(intent);
// //会触发onDestroy();
// MainActivity.this.finish();
} else if (v == btnOutside) {
Uri uri = Uri.parse("http://www.baidu.com");
Intent it = new Intent(Intent.ACTION_VIEW, uri);
startActivity(it);
}
}
}
}
com/example/youtwo/testactivity/Activity2.java
package com.example.youtwo.testactivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
/**
* Created by YouTwo on 2015/1/16.
*/
public class Activity2 extends Activity {
Button btnBack;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity2);
Log.e("Activity2", "onCreate()");
btnBack = (Button)this.findViewById(R.id.btnBack);
btnBack.setOnClickListener(new ClickEvent());
}
class ClickEvent implements View.OnClickListener {
@Override
public void onClick(View v) {
if(v == btnBack) {
//Intent intent = new Intent();
//intent.setClass(Activity2.this, Activity.class);
//startActivity(intent);
finish();
}
}
}
}
本文只测试了简单的 Activity 调用方法.
Acivity 的传值, 使用Bundle,
// new一个Bundle对象,并将要传递的数据传入
Bundle bundle = new Bundle();
bundle.putString("Text",tbBundle.getText().toString());
// 将Bundle对象assign给Intent
intent.putExtras(bundle);
接收数据使用 getIntent().getExtras();.方法:
//得Intent中的Bundle对象
Bundle bunde = this.getIntent().getExtras();
//取得Bundle对象中的数据
bunde.getString("Text")