Android_页面之间的数据传递

来源:互联网 发布:陈一发儿淘宝店倒闭 编辑:程序博客网 时间:2024/06/07 12:49

下面是一个将值从一个页面传到另一个页面,再从另一个页面返回上一个页面的效果图及编码:

                

                

实现思路:

1.编写主页面的布局

2.设置按钮的点击事件

3.在点击事件中new一个快递员,也就是Intent,传入两个参数,第一个为当前页面,第二个参数为想要跳转到的页面

4.利用Intent的putEatra()方法传值,第一个参数为该值的键名,第二个参数为所要传的值

5.startActivity()中传入所写的快递员,进行跳转页面

注:如果想要跳出去,再跳回来,而不是产生新的界面,就需要用到startActivityForResult()第一个参数为所写的Intent,第二个参数为请求码,可传随意的数字,最好用16进制(在数字前加上0x)

6.跳转到的页面可以通过getIntent().getStringExtra("uname")方法来获取值,参数为你想要的值的键名7.通过id可以找到文本框,进行赋值8.通过现显示的界面的按钮的点击事件,回到原来的界面,并进行传值9.new一个快递员(Intent),应为是返回,所以里面不需要传入任何参数10.获取编写的心情内容,通过putEatra()方法放入,并设置键名11.通过setResult(0x101,intent);设置结果,前面为结果编码,可随意编写16进制的数,后面为所写的Intent12.通过finish();结束该Activity,出现上一个Activity13.在主Activity中重写onActivityResult方法,该方法在从另一个页面跳回该页面时会自动调用
14.在onActivityResult方法中通过参数data获和值得键名获取传过来的值15.给主页面的文本框设值16.到这里效果就可以出现了

下面是实现效果的编码:

MainActivity的布局文件:
<?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:orientation="vertical"    android:layout_height="match_parent" tools:context="com.zking.admin.android_6_16.MainActivity">    <EditText        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:hint="请输入用户名:"        android:id="@+id/tv_main_text"        />    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:id="@+id/b_main_button"        android:onClick="writethink"        android:text="写心情"        />    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv_main_showThink"        /></LinearLayout>

ThinkActivity的布局文件:
<?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.zking.admin.android_6_16.ThinkActivity">    <TextView        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:id="@+id/tv_think_text"/>    <EditText        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:lines="5"        android:id="@+id/et_think_edit"/>    <Button        android:layout_width="match_parent"        android:layout_height="wrap_content"        android:text="确认"        android:onClick="ok"/></LinearLayout>

MainActivity的java文件:
import android.content.Intent;import android.os.Build;import android.support.v7.app.AppCompatActivity;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;import com.zking.entity.Person;import java.util.ArrayList;import java.util.List;public class MainActivity extends AppCompatActivity {    private EditText tv_main_text;    private TextView tv_main_showThink;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        tv_main_text = (EditText) findViewById(R.id.tv_main_text);        tv_main_showThink = (TextView) findViewById(R.id.tv_main_showThink);    }    public void writethink(View view){        Intent intent=new Intent(this,ThinkActivity.class);        intent.putExtra("uname",tv_main_text.getText().toString());                startActivityForResult(intent,0x100);    }    @Override    protected void onActivityResult(int requestCode, int resultCode, Intent data) {        super.onActivityResult(requestCode, resultCode, data);        String content=data.getStringExtra("content");        tv_main_showThink.setText(content);    }}

ThinkActivity的java文件:
package com.zking.admin.android_6_16;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;import com.zking.entity.Person;import java.util.List;public class ThinkActivity extends AppCompatActivity {    private TextView tv_think_text;    private EditText et_think_edit;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_think);        tv_think_text = (TextView) findViewById(R.id.tv_think_text);        //接收快递员的快递       String uname=getIntent().getStringExtra("uname");        tv_think_text.setText("你好:"+uname);        et_think_edit = (EditText) findViewById(R.id.et_think_edit);    }    public void ok(View view){        Intent intent=new Intent();        intent.putExtra("content",et_think_edit.getText().toString());        setResult(0x101,intent);        finish();    }}


原创粉丝点击