PutExtra.java
package
com.example.extra;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
public class PutExtra extends Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.putextra);
final EditText et =
(EditText)findViewById(R.id.et);
Button bn = (Button)findViewById(R.id.bn);
bn.setOnClickListener(new
OnClickListener() {
public void onClick(View
arg0) {
Intent intent = new
Intent(PutExtra.this, getExtra.class);
intent.putExtra("text",
et.getText().toString());
startActivity(intent);
}
});
}
}
getExtra.java
package
com.example.extra;
import
android.app.Activity;
import
android.content.Intent;
import
android.os.Bundle;
import
android.widget.TextView;
public class getExtra extends Activity {
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.getextra);
TextView tv =
(TextView)findViewById(R.id.tv);
Intent intent = getIntent();
String extra = intent.getStringExtra("text");
tv.setText(extra);
}
}
putextra.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#AA66CC"
android:orientation="vertical"
>
<EditText
android:id="@+id/et"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Put Extra
Data" />
<Button
android:id="@+id/bn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="putExtra"/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="300dp"
android:textSize="26dp"
android:textColor="#0000FF"
android:text="putextra.xml"
android:textAppearance="?android:attr/textAppearanceMedium"
/>
</LinearLayout>
getextra.xml
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FF4444"
android:orientation="vertical"
>
<TextView
android:id="@+id/tv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Get
Extra"
android:textColor="#000000"
android:background="#FFBB33"
android:textSize="30dp"
/>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="300dp"
android:text="getextra.xml"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
</LinearLayout>
Passing Data Using an Intent Object
How It Works
To
use the Intent object to carry data to
the target activity, you made use of a Bundle object:
Bundle extras = new
Bundle();
extras.putString(“Name”, “Your name here”);
i.putExtras(extras);
Bundle extras = getIntent().getExtras();
if (extras!=null)
{
defaultName = extras.getString(“Name”);
}
//---get the EditText view---
EditText txt_username =
(EditText) findViewById(R.id.txt_username);
txt_username.setHint(defaultName);
Android Control ควบคุมอุปกรณ์ต่างๆ ด้วย Android
สอนเขียน Android สอนเขียนโปรแกรม Android
http://androidcontrol.blogspot.com/2012/01/beginning-android-training-android.html
ติดต่อ amphancm@gmail.com
ติดต่อ amphancm@gmail.com