Intent:协助完成各个组件之间的通讯
页面跳转:
1、startActivity(intent):无返回值跳转
2、startActivityForResult(intent,requestCode):有返回值跳转
配合方法:
setResult(resultCode,data):跳转后的页面设置数据
onActivityResult(int requestCode,int resultCode,Intent data):跳转前页面得到数据
下面给出demo和效果图:
activity_main.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.three.MainActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/apple" />
<CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/banana" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="@string/man" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/women" />
</RadioGroup>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start"
/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/start2"
/>
</LinearLayout>
</FrameLayout>
MainActivity.java
package com.example.three;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioGroup;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity implements
android.widget.RadioGroup.OnCheckedChangeListener {
private CheckBox cb;
private RadioGroup rg;
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb = (CheckBox) findViewById(R.id.checkBox1);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, AAAActivity.class);
MainActivity.this.startActivity(intent);
}
});
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent(MainActivity.this, BBBActivity.class);
//请求码
MainActivity.this.startActivityForResult(intent, 1);
}
});
cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton button,
boolean isChecked) {
if (isChecked) {
String text = button.getText().toString();
Toast.makeText(MainActivity.this, "男", Toast.LENGTH_SHORT)
.show();
Log.i("Tag", text);
}
}
});
rg.setOnCheckedChangeListener(this);
}
@Override
public void onCheckedChanged(RadioGroup radioGroup, int radioId) {
Log.i("1", "sdasdsa");
switch (radioId) {
case R.id.radioButton1:
Toast.makeText(this, "男", Toast.LENGTH_SHORT).show();
break;
case R.id.radioButton2:
Toast.makeText(this, "女", Toast.LENGTH_SHORT).show();
break;
default:
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if(requestCode==1&&resultCode==2){
Toast.makeText(this, intent.getStringExtra("content"), Toast.LENGTH_SHORT).show();
}
}
}
activity_aaa.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.three.AAAActivity"
tools:ignore="MergeRootFrame" >
<ImageView
android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/ic_launcher" />
</FrameLayout>
AAAActivity.java
package com.example.three;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class AAAActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_aaa);
}
}
activity_bbb.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.three.BBBActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/back" />
</FrameLayout>
BBBActivity.java
package com.example.three;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class BBBActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bbb);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Intent intent = new Intent();
//添加数据
intent.putExtra("content", "你好!");
//添加返回码和intent对象
setResult(2, intent);
//关闭??
finish();
}
});
}
}
效果图:
启动按钮:无返回值跳转
启动2按钮:有返回值跳转
网友评论