- 什么是Intent
Intent是Android应用内不同组件之间通信的载体,当Android运行时需要连接不同的组件时,通常需要借助于Intent来实现。
通过Intent,你的程序可以向Android表达请求或意愿,Android会根据意愿的内容选择适当的组件来请求。 - 怎么用Intent
(1)intent传递数据
简单实例:在MainActivity中添加按钮,点击按钮,在OtherActivity中显示几行信息。
1)在activity_main.xml文件中添加一个按钮
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="使用intent进行值传递"
/>
2)编写MainActivity的逻辑代码
public class MainActivity extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//加载布局文件
button=(Button)this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {//给button添加监听器
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent=new Intent(MainActivity.this,OtherActivity.class);
//intent中传递数据
intent.putExtra("name", "张三");
intent.putExtra("age", 11);
//启动intent
startActivity(intent);
}
});
}
3)新建other.xml,添加TextView文本显示框
<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
4)新建OtherActivity类,来接受intent传递的参数
public class OtherActivity extends Activity{
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView=(TextView)this.findViewById(R.id.msg);
Intent intent=getIntent();
int age=intent.getIntExtra("age", 0);
String name=intent.getStringExtra("name");
textView.setText("name:"+name+"\n"+"age:"+age);
}
}
5)配置AndroidManifest.xml文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.android_intent.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=".OtherActivity"></activity>
</application>
6)运行结果
(2)intent返回数据
实例,在当前activity中显示A+B=?,在下一个Activity中计算值,并将值返回,最后结果A+B=C。
1)配置布局文件activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/one"
android:layout_width="40dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text="+" >
</TextView>
<EditText
android:id="@+id/two"
android:layout_width="40dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="20dp"
android:layout_height="wrap_content"
android:text="=" >
</TextView>
<EditText
android:id="@+id/result"
android:layout_width="40dp"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="计算结果" />
</LinearLayout>
2)编写MainActivity类
public class MainActivity extends Activity {
private Button button;
private final static int REQUESTCODE = 1;
private EditText one,two,result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
one=(EditText)this.findViewById(R.id.one);
two=(EditText)this.findViewById(R.id.two);
result=(EditText)this.findViewById(R.id.result);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int a=Integer.parseInt(one.getText().toString());
int b=Integer.parseInt(two.getText().toString());
Intent intent = new Intent(MainActivity.this,
OtherActivity.class);
intent.putExtra("a", a);
intent.putExtra("b", b);
//启动intent的时候,调用startActivityForResult方法,设置一个当前的requestCode
startActivityForResult(intent, REQUESTCODE);//表示可以返回结果
}
});
}
//必须重写onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if(resultCode==2)
{
if(requestCode==REQUESTCODE){
int three=data.getIntExtra("three", 0);
result.setText(String.valueOf(three));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
3)编写OtherActivity的布局文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
<EditText
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@null" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回结果" >
</Button>
</LinearLayout>
4)编写OtherActivity类
public class OtherActivity extends Activity {
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
button=(Button)this.findViewById(R.id.button2);
textView=(TextView)this.findViewById(R.id.msg);
editText=(EditText)this.findViewById(R.id.three);
Intent intent=getIntent();
int a=intent.getIntExtra("a",0);
int b=intent.getIntExtra("b", 0);
textView.setText(a+" + "+b+" = "+" ? ");
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//声明新的intent
Intent intent=new Intent();
int three=Integer.parseInt(editText.getText().toString());
intent.putExtra("three", three);
//通过Intent对象返回结果,setResult方法
setResult(2,intent);
finish();//结束当前的Activity的生命周期
}
});
}
}
5)配置AndroidManifest.xml文件
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.intentreturn.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=".OtherActivity"></activity>
</application>
6)运行结果
算是模拟计算机内部计算结果~~