Android学习笔记02——Intent的使用
学习内容:
创建Activity的要点:
1.一个Activity就是一个类,并且该类继承自Activity
2.需要覆写onCreate方法
.每一个Activity都需要在AndroidMainfest.xml文件当中进行配置
4.为每一个Activity添加必要的控件
Intent的使用一个Intent对象包含了一组信息:
Componentname :要启动的对象,如Activity,service等
Action :指定对象的动作(详见下图)
Data :类型
Category :
Extras :“键值对”
Flags :
注:Intent可以在不同程序中的两个Activity传递信息
演示效果:
相关代码:
intentActivity.java
package wml.android.intentActivity;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class IntentActivityActivity extends Activity {
/** Called when the activity is first created. */
private Button myBut=null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myBut=(Button)findViewById(R.id.but);
//添加监听器
myBut.setOnClickListener(new MyButtonListener());
}
class MyButtonListener implements OnClickListener{
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(); //创建Intent对象
intent.setClass(IntentActivityActivity.this, OtherActivity.class); //实现从本Activity到OtherActivity的跳转 IntentActivityActivity.this.startActivity(intent); //Activity通过intent启动OtherActivity
}
}
}
OtherActivity :
package wml.android.intentActivity;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity{
private TextView myTextView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
myTextView=(TextView)findViewById(R.id.othertext);
}
}
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:background="#ffff0000"
android:orientation="vertical" >
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="40px"
android:text="练习使用intent对象" />
<Button
android:id="@+id/but"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="点我" />
</LinearLayout>
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:background="#fff00fff"
android:orientation="vertical" >
<TextView
android:id="@+id/othertext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30px"
android:text="我是被intent唤出的Activity 成功!" />
</LinearLayout>
string.xml
string.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World, IntentActivityActivity!</string>
<string name="app_name">第二个Activity、</string>
<string name="other">第二个Activity</string>
</resources>
需要配置AndroidManiFest.xml文件(加粗字为添加的代码)
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="wml.android.intentActivity"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".IntentActivityActivity"
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=".OtherActivity"
android:label="@string/other" > </activity>
</application>
</manifest>