java向另一activity输入_Activity经典实例一:两个Activity传递数据和对象
1、概述:
Activity類直接或者間接地繼承了Context、ContextWrapper、ContextThemeWrapper等基類,因此Activity可以直接調用它們的方法。
創建一個Activity需要實現某些方法,常見的是實現onCreate(Bundle status)方法,該方法將會在Activity創建時被回調,它調用setContentView(View view)方法來顯示要展示的View。
一個Android應用常常有多個Activity,但是只有一個作為程序的入口,其他的Activity通常都由入口Activity、及其后者啟動。
2、Activity啟動另一個Activity的方法:
startActivity(Intent intent):啟動其他Activity;
startActivityForResult(Intent intent, int requestCode):以指定請求碼(requestCode)啟動Activity,而且程序將會等到新啟動Activity的結果(通過重寫onActivityResult(...)方法來獲取結果)。
3、關閉Activity的方法:
finish():結束掉當前的Activity;
finishActivity(int requestCode):結束以startActivityForResult()方法啟動的Activity。
4、使用Bundle在Activity之間交換數據:
1)、Intent:主要通過Intent這個信使,將需要交換的數據放入即可。Intent提供了方法用于攜帶數據,如:
putExtras(Bundle data):向Intent中放入需要攜帶的數據;
2)、Bundle:就是一個簡單的數據包,該Bundle對象包含了多個方法來存入、取出數據,有:
putXxx(String key, Xxx data):向Bundle放入Int、Long等各種類型的數據;
putSerializable(String key, Serializable data):向Bundle放入一個可序列化的對象;
getXxx(String key):從Bundle取出Int、Long等各種類型的數據;
getSerializable(String key):從Bundle取出一個可序列化的對象。
5、開發實例:注冊用戶信息
項目簡介:程序包含兩個Activity,一個給用戶填寫信息,另一個顯示注冊結果。
完整代碼:
RegisterActivity.java源代碼:
package com.xsjayz.ac;
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;
import android.widget.RadioButton;
import android.widget.Toast;
public class RegisterActivity extends Activity {
private Button regButton;
private EditText nameEdit;
private EditText passwdEdit;
private RadioButton maleButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
regButton = (Button) findViewById(R.id.register_now);
nameEdit = (EditText) findViewById(R.id.name_edit);
passwdEdit = (EditText) findViewById(R.id.password_edit);
maleButton = (RadioButton) findViewById(R.id.male_btn);
regButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String name = nameEdit.getText().toString();
String passwd = passwdEdit.getText().toString();
String gender = maleButton.isChecked() ? "男" : "女";
// 如果輸入信息不完整,則不允許注冊。
if (name.equals("") || passwd.equals("")) {
Toast.makeText(RegisterActivity.this, "請填寫完整的信息!", 3000)
.show();
} else {
// 創建Person對象,一個可序列化的對象。
Person person = new Person(name, passwd, gender);
Bundle bundle = new Bundle();
bundle.putSerializable("person", person);
Intent intent = new Intent(RegisterActivity.this,
ResultActivity.class);
intent.putExtras(bundle);
// 啟動另一個Activity。
startActivity(intent);
}
}
});
}
}
ResultActivity.java源代碼:
package com.xsjayz.ac;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends Activity {
private TextView nameText;
private TextView passwdText;
private TextView genderText;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
nameText = (TextView) findViewById(R.id.name_text);
passwdText = (TextView) findViewById(R.id.passwd_text);
genderText = (TextView) findViewById(R.id.gender_text);
// 獲取啟動該ResultActivity的Intent
Intent intent = getIntent();
// 獲取該Intent所攜帶的數據
Bundle bundle = intent.getExtras();
// 從bundle數據包中取出數據
Person person = (Person) bundle.getSerializable("person");
// 顯示注冊結果
nameText.setText("您的帳號:" + person.getName());
passwdText.setText("您的密碼:" + person.getPasswd());
genderText.setText("您的性別:" + person.getGender());
}
}
main.xml布局文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/register_info_text"
android:textSize="20sp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/user_name"
android:textSize="16sp" />
android:id="@+id/name_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/set_name"
android:selectAllOnFocus="true" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/user_password"
android:textSize="16sp" />
android:id="@+id/password_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="@string/set_password"
android:password="true"
android:selectAllOnFocus="true" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/user_sex"
android:textSize="16sp" />
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
android:id="@+id/male_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/male_btn"
android:checked="true"
android:textSize="16sp" />
android:id="@+id/female_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/female_btn"
android:textSize="16sp" />
android:id="@+id/register_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/register_btn"
android:textSize="16sp" />
result.xml布局文件:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
android:id="@+id/name_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
android:id="@+id/passwd_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
android:id="@+id/gender_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
可序列化的對象Person:
package com.xsjayz.ac;
import java.io.Serializable;
public class Person implements Serializable {
private static final long serialVersionUID = 1L;
private String name;
private String passwd;
private String gender;
public Person(String name, String passwd, String gender) {
this.name = name;
this.passwd = passwd;
this.gender = gender;
}
public String getName() {
return name;
}
public String getPasswd() {
return passwd;
}
public String getGender() {
return gender;
}
}
總結
以上是生活随笔為你收集整理的java向另一activity输入_Activity经典实例一:两个Activity传递数据和对象的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 鸟哥的linux私房菜简答题答案,《鸟哥
- 下一篇: 地磅称重软件源码_【漯河衡器】导致地磅称