使用Intent的傳遞進行Activity之間的跳轉、傳值。
實現效果:
實現思路:
第一步:寫上兩個界面。
第一個界面包含:用戶名EditText控件,跳轉到寫內容的Button按鈕,展示內容信息的TextView控件。
第二個界面包含:展示用戶名的TextView控件,編寫內容的EditText控件,確定按鈕
第二步:在Activity中實現點擊方法,通過Bundle進行放值,Intent進行傳值。
第三步:在onCreate()方法中拿到通過Intent傳遞的值設置到頁面。
實現代碼:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.g160628_android12_activitytest.MainActivity">
<!--用戶名-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="請輸入用戶名"
android:id="@+id/et_main_uname"
/>
<!--跳轉到寫內容的頁面-->
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="寫內容"
android:onClick="writeContent"
/>
<!--用來展示書寫的內容-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_main_showContent"
/>
</LinearLayout>
activity_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.g160628_android12_activitytest.ContentActivity">
<!--展示你好+用戶名-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tv_content_showname"
/>
<!--編寫內容與確定按鈕-->
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:lines="5"
android:hint="請書寫內容"
android:id="@+id/et_content_text"
android:gravity="top"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="確定"
android:onClick="ok"
/>
</LinearLayout>
MainActivity.java
package com.example.g160628_android12_activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText et_main_uname;
private TextView tv_main_showContent;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//獲得activity_content中的展示姓名的TextView控件
et_main_uname = (EditText) findViewById(R.id.et_main_uname);
//獲得從activity_content跳轉回來展示內容的控件
tv_main_showContent = (TextView) findViewById(R.id.tv_main_showContent);
}
//跳轉到寫內容的頁面,同時將EditText中的用戶名傳遞到activity_content中
public void writeContent(View view){
//實例化Intent
Intent intent=new Intent(this,ContentActivity.class);
//獲得TextView控件的值
String uname=et_main_uname.getText().toString();
//實例化Bundle
Bundle bundle=new Bundle();
//將用戶名存入到bundle中
bundle.putString("uname",uname);
//將bundle數據類型綁定到intent上
intent.putExtra("bundle",bundle);
//用爲目的的開始方法,參數爲intent,與請求碼
startActivityForResult(intent,0x201);
}
//特意用來接收結果值的方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//根據ID獲得Intent中的bundle
Bundle bundle=data.getBundleExtra("bundle");
//獲得內容
String content=bundle.getString("content");
//獲得尾綴
String end=bundle.getString("end");
//將值展示到界面
tv_main_showContent.setText(content+"\n\t\t\t\t"+end);
}
}
ContentActivity.java
package com.example.g160628_android12_activitytest;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
public class ContentActivity extends AppCompatActivity {
private TextView tv_content_showname;
private EditText et_content_text;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content);
//獲得傳遞的bundle值
Bundle bundle=getIntent().getBundleExtra("bundle");
//根據姓名獲得uname
String uname=bundle.getString("uname");
//獲得展示姓名的TextView控件
tv_content_showname = (TextView) findViewById(R.id.tv_content_showname);
//設置值
tv_content_showname.setText("你好"+uname);
//獲得編寫內容的EditText控件
et_content_text = (EditText) findViewById(R.id.et_content_text);
}
//編寫內容完畢的點擊方法
public void ok(View view){
//實例化Intent
Intent intent=new Intent();
//獲得內容的值
String content=et_content_text.getText().toString();
//實例化bundle的值
Bundle bundle=new Bundle();
//將content內容綁定到bundle上
bundle.putString("content",content);
//綁定一個尾綴到bundle上
bundle.putString("end","一隻今年18的小壁花");
//傳到頁面上
intent.putExtra("bundle",bundle);
//設置結果
setResult(0x101,intent);
//自殺的操作
finish();
}
}
然後配置要記得配,注意的地方
1、
//用爲目的的開始方法,參數爲intent,與請求碼
startActivityForResult(intent,0x201);
2、//設置結果
setResult(0x101,intent);
//自殺的操作
finish();
3、配置
關於傳值的分享如上,歡迎各位指點一二