Andriod 透過Intent轉換Activity
Andriod 透過Intent轉換Activity
在網路上發現有用的Intent簡易範例,趕緊備份,歡迎有興趣同好一起C/P一下。
資料來源:http://www.dotblogs.com.tw/psjhuo/archive/2013/04/18/101811.aspx
Activity1
package com.example.intent_activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
////////////////////////////////
import android.content.Intent;
import android.view.View;
import android.widget.EditText;
////////////////////////////////
public class Activity1 extends Activity {
private EditText etName;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout1);
etName = (EditText)findViewById(R.id.etName);
}
public void onClick(View v) {
switch(v.getId()){
case R.id.btGo:
Intent intent = new Intent();
intent.setClass(Activity1.this, Activity2.class);
Bundle bundle = new Bundle();
bundle.putString("name", etName.getText().toString());
intent.putExtras(bundle);
startActivityForResult(intent,0);
//startActivity(intent);
//Activity1.this.finish();
break;
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
switch(resultCode){
case RESULT_OK:
Bundle bundle = data.getExtras();
etName.setText(bundle.getString("name"));
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.activity1, menu);
return true;
}
}
|
layout1
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name"/>
<EditText
android:id="@+id/etName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"/>
<Button
android:id="@+id/btGo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="go"/>
</TableRow>
</TableLayout>
|
Activity2
package com.example.intent_activity;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
/////////////////////////////////////////
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
////////////////////////////////////////
public class Activity2 extends Activity {
private TextView tvMsg;
private Intent intent;
private Bundle bundle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout2);
intent = this.getIntent();
bundle = intent.getExtras();
tvMsg = (TextView)findViewById(R.id.tvMsg);
tvMsg.setText("Hi "+bundle.getString("name")+"!!!");
}
public void onClick(View v) {
switch(v.getId()){
case R.id.btBack:
Activity2.this.setResult(RESULT_OK,intent);
Activity2.this.finish();
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.activity2, menu);
return true;
}
}
|
layout2
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/tvMsg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="@+id/btBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="back"/>
</TableLayout>
|