注册
<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);
Bundle bundle = new Bundle();
bundle.putString("username", username);
bundle.putString("password", password);
intent.putExtras(bundle);
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();
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>
<h2><a id="_0"></a>注册</h2>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-tag"><<span class="hljs-name">activity</span> <span class="hljs-attr">android:name</span>=<span class="hljs-string">".One"</span> /></span>
</code></div></pre>
<h2><a id="MainActivityjava_6"></a>MainActivity.java</h2>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> com.example.study;
<span class="hljs-keyword">import</span> androidx.appcompat.app.AppCompatActivity;
<span class="hljs-keyword">import</span> android.content.Intent;
<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.view.View;
<span class="hljs-keyword">import</span> android.widget.Button;
<span class="hljs-keyword">import</span> android.widget.EditText;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">MainActivity</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">AppCompatActivity</span> {
EditText edit1;
EditText edit2;
Button btn1;
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> {
<span class="hljs-built_in">super</span>.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(<span class="hljs-keyword">new</span> <span class="hljs-title class_">View</span>.OnClickListener() {
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onClick</span><span class="hljs-params">(View view)</span> {
<span class="hljs-type">String</span> <span class="hljs-variable">username</span> <span class="hljs-operator">=</span> edit1.getText().toString();
<span class="hljs-type">String</span> <span class="hljs-variable">password</span> <span class="hljs-operator">=</span> edit2.getText().toString();
<span class="hljs-type">Intent</span> <span class="hljs-variable">intent</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Intent</span>(MainActivity.<span class="hljs-built_in">this</span>, One.class);
<span class="hljs-comment">// 方法一:传单个数据</span>
<span class="hljs-comment">// intent.putExtra("username", username);</span>
<span class="hljs-comment">// 方法二:传数据集</span>
<span class="hljs-type">Bundle</span> <span class="hljs-variable">bundle</span> <span class="hljs-operator">=</span> <span class="hljs-keyword">new</span> <span class="hljs-title class_">Bundle</span>();
bundle.putString(<span class="hljs-string">"username"</span>, username);
bundle.putString(<span class="hljs-string">"password"</span>, password);
<span class="hljs-comment">// 做成一个对象 传</span>
intent.putExtras(bundle);
<span class="hljs-comment">// 启动intent 意图</span>
startActivity(intent);
}
});
}
}
</code></div></pre>
<h2><a id="Onejava_63"></a>One.java</h2>
<pre><div class="hljs"><code class="lang-java"><span class="hljs-keyword">package</span> com.example.study;
<span class="hljs-keyword">import</span> android.app.Activity;
<span class="hljs-keyword">import</span> android.content.Intent;
<span class="hljs-keyword">import</span> android.graphics.Color;
<span class="hljs-keyword">import</span> android.os.Bundle;
<span class="hljs-keyword">import</span> android.view.View;
<span class="hljs-keyword">import</span> android.widget.TextView;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">class</span> <span class="hljs-title class_">One</span> <span class="hljs-keyword">extends</span> <span class="hljs-title class_">Activity</span> {
TextView textView1;
TextView textView2;
<span class="hljs-meta">@Override</span>
<span class="hljs-keyword">protected</span> <span class="hljs-keyword">void</span> <span class="hljs-title function_">onCreate</span><span class="hljs-params">(Bundle savedInstanceState)</span> {
<span class="hljs-built_in">super</span>.onCreate(savedInstanceState);
setContentView(R.layout.one);
textView1 = findViewById(R.id.One);
textView2 = findViewById(R.id.Two);
<span class="hljs-comment">// 接收数据</span>
<span class="hljs-type">Intent</span> <span class="hljs-variable">intent</span> <span class="hljs-operator">=</span> getIntent();
<span class="hljs-comment">// 方法一:接收 单个数据</span>
<span class="hljs-comment">// String username = intent.getStringExtra("username");</span>
<span class="hljs-comment">// 方法二:接收 数据集</span>
<span class="hljs-type">Bundle</span> <span class="hljs-variable">bundle</span> <span class="hljs-operator">=</span> intent.getExtras();
<span class="hljs-type">String</span> <span class="hljs-variable">username</span> <span class="hljs-operator">=</span> bundle.getString(<span class="hljs-string">"username"</span>);
<span class="hljs-type">String</span> <span class="hljs-variable">password</span> <span class="hljs-operator">=</span> bundle.getString(<span class="hljs-string">"password"</span>);
textView1.setText(username);
textView1.setTextColor(Color.RED);
textView1.setTextSize(<span class="hljs-number">20</span>);
textView2.setText(password);
textView2.setTextColor(Color.RED);
textView2.setTextSize(<span class="hljs-number">20</span>);
}
}
</code></div></pre>
<h2><a id="_110"></a>页面</h2>
<p>activity_main.xml</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-tag"><<span class="hljs-name">RelativeLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
<span class="hljs-attr">xmlns:tools</span>=<span class="hljs-string">"http://schemas.android.com/tools"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>
<span class="hljs-attr">tools:context</span>=<span class="hljs-string">"${relativePackage}.${activityClass}"</span> ></span>
<span class="hljs-tag"><<span class="hljs-name">EditText</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/editText1"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_alignParentLeft</span>=<span class="hljs-string">"true"</span>
<span class="hljs-attr">android:layout_marginLeft</span>=<span class="hljs-string">"7dp"</span>
<span class="hljs-attr">android:layout_marginTop</span>=<span class="hljs-string">"32dp"</span>
<span class="hljs-attr">android:ems</span>=<span class="hljs-string">"10"</span>
<span class="hljs-attr">android:text</span>=<span class="hljs-string">"输入用户名"</span>></span>
<span class="hljs-tag"></<span class="hljs-name">EditText</span>></span>
<span class="hljs-tag"><<span class="hljs-name">EditText</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/editText2"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_below</span>=<span class="hljs-string">"@+id/editText1"</span>
<span class="hljs-attr">android:layout_marginTop</span>=<span class="hljs-string">"19dp"</span>
<span class="hljs-attr">android:ems</span>=<span class="hljs-string">"10"</span>
<span class="hljs-attr">android:text</span>=<span class="hljs-string">"密码"</span> /></span>
<span class="hljs-tag"><<span class="hljs-name">Button</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/button1"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_alignLeft</span>=<span class="hljs-string">"@+id/editText2"</span>
<span class="hljs-attr">android:layout_marginLeft</span>=<span class="hljs-string">"243dp"</span>
<span class="hljs-attr">android:text</span>=<span class="hljs-string">"Button"</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">RelativeLayout</span>></span>
</code></div></pre>
<p>one.xml</p>
<pre><div class="hljs"><code class="lang-xml"><span class="hljs-meta"><?xml version="1.0" encoding="utf-8"?></span>
<span class="hljs-tag"><<span class="hljs-name">androidx.constraintlayout.widget.ConstraintLayout</span> <span class="hljs-attr">xmlns:android</span>=<span class="hljs-string">"http://schemas.android.com/apk/res/android"</span>
<span class="hljs-attr">xmlns:tools</span>=<span class="hljs-string">"http://schemas.android.com/tools"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"match_parent"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"match_parent"</span>></span>
<span class="hljs-tag"><<span class="hljs-name">TextView</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/One"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_alignParentLeft</span>=<span class="hljs-string">"true"</span>
<span class="hljs-attr">android:layout_alignParentTop</span>=<span class="hljs-string">"true"</span>
<span class="hljs-attr">android:text</span>=<span class="hljs-string">"One"</span>
<span class="hljs-attr">tools:ignore</span>=<span class="hljs-string">"MissingConstraints"</span>
<span class="hljs-attr">tools:layout_editor_absoluteX</span>=<span class="hljs-string">"104dp"</span>
<span class="hljs-attr">tools:layout_editor_absoluteY</span>=<span class="hljs-string">"116dp"</span> /></span>
<span class="hljs-tag"><<span class="hljs-name">TextView</span>
<span class="hljs-attr">android:id</span>=<span class="hljs-string">"@+id/Two"</span>
<span class="hljs-attr">android:layout_width</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_height</span>=<span class="hljs-string">"wrap_content"</span>
<span class="hljs-attr">android:layout_alignParentLeft</span>=<span class="hljs-string">"true"</span>
<span class="hljs-attr">android:layout_alignParentTop</span>=<span class="hljs-string">"true"</span>
<span class="hljs-attr">android:text</span>=<span class="hljs-string">"One"</span>
<span class="hljs-attr">tools:ignore</span>=<span class="hljs-string">"MissingConstraints"</span>
<span class="hljs-attr">tools:layout_editor_absoluteX</span>=<span class="hljs-string">"104dp"</span>
<span class="hljs-attr">tools:layout_editor_absoluteY</span>=<span class="hljs-string">"116dp"</span> /></span>
<span class="hljs-tag"></<span class="hljs-name">androidx.constraintlayout.widget.ConstraintLayout</span>></span>
</code></div></pre>
留言