Skip to content

Instantly share code, notes, and snippets.

@ssd863419
Created October 10, 2014 06:32
Show Gist options
  • Save ssd863419/90ff0ffd30f0b942e030 to your computer and use it in GitHub Desktop.
Save ssd863419/90ff0ffd30f0b942e030 to your computer and use it in GitHub Desktop.
Android 像另一個Activity取返回值, implements OnClickListener, switch, startActivityForResult, Intent
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.administrator.test" >
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme">
<activity
android:name=".TestActivity"
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=".TestActivityReturn"></activity>
</application>
</manifest>
package com.example.administrator.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Administrator on 2014/10/10.
*/
public class TestActivity extends Activity implements View.OnClickListener {
private TextView mTextView1, mTextView2;
private Button mButton1, mButton2;
private int flag;
private Intent intent;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.testactivity);
mTextView1 = (TextView) findViewById(R.id.myTextView1);
mTextView2 = (TextView) findViewById(R.id.myTextView2);
mButton1 = (Button) findViewById(R.id.myButton1);
mButton2 = (Button) findViewById(R.id.myButton2);
mButton1.setOnClickListener(this);
mButton2.setOnClickListener(this);
intent = new Intent();
intent.setClass(TestActivity.this, TestActivityReturn.class);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.myButton1:
flag = 0;
startActivityForResult(intent, 1);
break;
case R.id.myButton2:
flag = 2;
startActivityForResult(intent, 1);
break;
default:
break;
}
}
/* 從第二頁面返回時, 執行此方法 */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
String str1 = intent.getStringExtra("change01");
String str2 = intent.getStringExtra("change02");
switch (flag) {
case 0:
mTextView1.setText(str1);
break;
case 2:
mTextView2.setText(str2);
break;
default:
break;
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第一行的值"
android:id="@+id/myTextView1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="第二行的值"
android:id="@+id/myTextView2" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改變第一行"
android:id="@+id/myButton1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="改變第二行"
android:id="@+id/myButton2" />
</LinearLayout>
</LinearLayout>
package com.example.administrator.test;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Administrator on 2014/10/10.
*/
public class TestActivityReturn extends Activity {
private TextView mTextView;
private Button mButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.returntest);
mTextView = (TextView) findViewById(R.id.myTextView);
mButton = (Button) findViewById(R.id.myButton);
mTextView.setText("沒事做!!!!");
mButton.setOnClickListener(new Button.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent();
intent.putExtra("change01", "你好, 我好, 大家好, 按鈕1");
intent.putExtra("change02", "中國一定強, 按鈕2");
setResult(1, intent);
finish();
}
});
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment