有勇气的牛排博客

安卓 激活应用组件 intent

有勇气的牛排 1106 安卓 2021-10-17 00:38:45

注册

<activity android:name=".One" />

MainActivity.java

package com.example.study; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends AppCompatActivity { EditText edit1; EditText edit2; Button btn1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); edit1 = (EditText) findViewById(R.id.editText1); edit2 = (EditText) findViewById(R.id.editText2); btn1 = (Button) findViewById(R.id.button1); btn1.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String username = edit1.getText().toString(); String password = edit2.getText().toString(); Intent intent = new Intent(MainActivity.this, One.class); // 方法一:传单个数据 // intent.putExtra("username", username); // 方法二:传数据集 Bundle bundle = new Bundle(); bundle.putString("username", username); bundle.putString("password", password); // 做成一个对象 传 intent.putExtras(bundle); // 启动intent 意图 startActivity(intent); } }); } }

One.java

package com.example.study; import android.app.Activity; import android.content.Intent; import android.graphics.Color; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class One extends Activity { TextView textView1; TextView textView2; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.one); textView1 = findViewById(R.id.One); textView2 = findViewById(R.id.Two); // 接收数据 Intent intent = getIntent(); // 方法一:接收 单个数据 // String username = intent.getStringExtra("username"); // 方法二:接收 数据集 Bundle bundle = intent.getExtras(); String username = bundle.getString("username"); String password = bundle.getString("password"); textView1.setText(username); textView1.setTextColor(Color.RED); textView1.setTextSize(20); textView2.setText(password); textView2.setTextColor(Color.RED); textView2.setTextSize(20); } }

页面

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="${relativePackage}.${activityClass}" > <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_marginLeft="7dp" android:layout_marginTop="32dp" android:ems="10" android:text="输入用户名"> </EditText> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText1" android:layout_marginTop="19dp" android:ems="10" android:text="密码" /> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_marginLeft="243dp" android:text="Button" /> </RelativeLayout>

one.xml

<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:id="@+id/One" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="One" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="104dp" tools:layout_editor_absoluteY="116dp" /> <TextView android:id="@+id/Two" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="One" tools:ignore="MissingConstraints" tools:layout_editor_absoluteX="104dp" tools:layout_editor_absoluteY="116dp" /> </androidx.constraintlayout.widget.ConstraintLayout>

留言

专栏
文章
加入群聊