您的位置:首页 > 移动开发 > Android开发

Android-startActivityForResult

2015-08-10 08:57 393 查看
MainActivity

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity implements
OnClickListener{

private TextView mText01;
private TextView mText02;
private Button button01;
private Button button02;
private Intent mIntent;
private int requestCode;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mText01 = (TextView) findViewById(R.id.mText01);
mText02 = (TextView) findViewById(R.id.mText02);
button01 = (Button) findViewById(R.id.mButton01);
button02 = (Button) findViewById(R.id.mButton02);
button01.setOnClickListener(this) ;
button02.setOnClickListener(this);
mText01.setText("01");
mText02.setText("02");

mIntent = new Intent();
mIntent.setClass(MainActivity.this,
Activity02.class);
}

@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.mButton01:
// 请求码的值随便设置,但必须>=0
requestCode = 0;
startActivityForResult(mIntent, requestCode);
break;
case R.id.mButton02:
requestCode = 2;
startActivityForResult(mIntent, requestCode);
break;
default:
break;
}
}

// 回调方法,从第二个页面回来的时候会执行这个方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
String change01 = data.getStringExtra("change01");
String change02 = data.getStringExtra("change02");
// 根据上面发送过去的请求吗来区别
switch (requestCode) {
case 0:
mText01.setText(change01);
break;
case 2:
mText02.setText(change02);
break;
default:
break;
}
}

@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);
}
}

Activity02

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public class Activity02 extends Activity {
private int resultCode = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity02);
Intent mIntent = new Intent();
mIntent.putExtra("change01", "1000");
mIntent.putExtra("change02", "2000");
// 设置结果,并进行传送
this.setResult(resultCode, mIntent);
// this.finish();
}

}

Manifest

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.lee.myapplication" >

<application
android:allowBackup="true"
android:icon="@mipmap/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=".Activity02" />
</application>

</manifest>

Activity02
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="文本的值已经改变"
/>
</LinearLayout>

activity_main
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/mText01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/mText02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/mButton01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="改变第一行文本的值"
/>
<Button
android:id="@+id/mButton02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="改变第二行文本的值"
/>
</LinearLayout>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: