Android Intent隐式的实现,在第一界面输入用户名,点击按钮,跳转到第二个界面,显示用户名。

废话少说,先上截图!(可以给个关注吗,发了五个作品都没人关注我大哭)

 

 

首先要在配置文件设置隐式跳转的条件,我的是这样。

<activity android:name=".MainActivity2">是被指定跳转的页面

 

1.第一个页面代码

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity1 extends Activity {
        //定义控件变量
        private EditText editText;
        private Button button;

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity1);
            // 绑定变量
            editText = (EditText)findViewById(R.id.editText);
            button = (Button)findViewById(R.id.button);

            // 为Button设置监听事件
            button.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View arg0) {
                    String text = "";
                   text = editText.getText().toString();
                    // 将信息放入Bundle
                    Bundle bundle = new Bundle ();
                  
                    bundle.putString("desc",text);

                    // 隐式页面跳转
                    Intent intent = new Intent("com.example.experience4.AVTION_START");
         //里面com.example.experience4.AVTION_START,这个可以自己命名,不过要和 配置文件的命名要一致        
                    intent.putExtras(bundle);
                    startActivity(intent);
                }

            });
        }
    }

布局代码如下
activity1:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
        />

    <TextView
        android:layout_width="200px"
        android:layout_height="100px"
        android:layout_marginTop="40dp"
        android:text="用户名:"
        />

    <!--<EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30px"
        android:ems="10"
        />-->
    <EditText
        android:id="@+id/editText"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="30px"
         />

    <Button
        android:id="@+id/button"
        android:layout_width="600px"
        android:layout_height="wrap_content"
        android:layout_marginLeft="50px"
        android:layout_marginTop="50px"
        android:text="显示TextView文本框" />

</LinearLayout>

2.第二个页面代码

package com.example.experience4;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class MainActivity2 extends Activity {

        private TextView message;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity2);
            Bundle bundle = this.getIntent().getExtras();
            // 接收信息并显示
            message = (TextView)findViewById(R.id.message);
            String desc = bundle.getString("desc");

            message.setText( desc+"你好!欢迎使用Android");
        }

    }

布局代码如下

activity2:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

    <TextView
        android:id="@+id/message"
        android:layout_width="match_parent"
        android:layout_marginLeft="100px"
        android:layout_height="66dp" />
</LinearLayout>
 


版权声明:本文为weixin_50974618原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。