Android——关于Activity跳转的返回(无返回值和有返回值)——无返回值

一、无返回值

 

跳转页面,并将第一页的Edittext输入的数据通过按钮Button传到第二页用Edittext显示,点击第二页的

返回按钮Button返回第一页(改变第二页的Edittext的内容不能传到第一页)

——普通方式,没有返回值的方式

1.给第一页面Edittext和Button设置id

2.设置Button的点击监听

(1)获取view实例,通过Edittext的id找到Edittext

(2)获取内容并转为文本形式

getText().toString()

(3)设置Intent(意图)告诉第二个页面,我要跳转了

Intent in = new Intent(this,excise2.class);

(4)在跳转的同时,通过Intent将输入的文本内容一并存储传过去

in.putExtra("myet",str);

(5)开始跳转

startActivity(in);

3.接受页面用Edittext接收

(1)给接受页面Edittext和Button设置id

(2)创建第二页面

public class excise2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.excise2);

(3)接收Intent意图 get

Intent in = getIntent();

(4)取存入的数据 get get

String str = in.getExtras().getString("myet");

(5)显示数据

操作View实例

EditText mytv= ( EditText)findViewById(R.id.jieshouzhi);

恢复输入框里面的内容(设置) set

mytv.setText(str);

4.设置按钮的点击监听

finish();

excise1.xml

<EditText
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:textSize="40dp"
        android:id="@+id/ett"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="传值至第三页"
        android:textSize="40dp"
        android:id="@+id/chuanzhi"
        android:layout_marginTop="100dp"
        android:onClick="clickcz"
        />

excise1.java

 public void  clickcz(View view)
    {
        //Toast静态方法 直接用类名调用,不需要实例化
        //构建了Toast方法  实例方法调用  方法链
        Toast.makeText(this,"这是鼠标点击监听触发的",Toast.LENGTH_LONG).show();

        //取得要传递的信息
        //获取View实例
        EditText ett = (EditText)findViewById(R.id.ett);
        //获取内容
        String str = ett.getText().toString();
        //跳转用意图
        Intent in = new Intent(this,excise2.class);
        //存储内容 通过Intent
        //Extra 扩展 实际上是一个HashMap,进行限制 putExtra 是一个bundle
        in.putExtra("myet",str);
        //开始跳转 无返回值的写法
        startActivity(in);

 

excise2.xml

<EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="excise2 第三页"
        android:textSize="40dp"
        android:id="@+id/jieshouzhi"
        />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="150dp"
        android:text="返回"
        android:textSize="40dp"
        android:id="@+id/clickec3"
        android:onClick="clickec3"
        />

excise2.java

public class excise2 extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.excise2);


        //接受信息

        //1.接受传递过来的意图
        Intent in = getIntent();
        //2.取数据
        String str = in.getExtras().getString("myet");
        //3.显示在哪里
        //操作View实例
        EditText mytv= ( EditText)findViewById(R.id.jieshouzhi);
        //恢复输入框里面的内容(设置) set
        mytv.setText(str);


    }public void clickec3(View v)
    {
        //关闭
        finish();
    }



}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值