安卓程序设计实验 Intent和Activity的使用新建一个Android工程命名为test1。 修改main.xml布局文件,增加两个TextView,两个EditView和一个按钮。新建test2.xml布局文件,增加两个TextView,用来显示添加的用户名。 创建Test1.java和Test2.java两个Activity,分别实现从页面输入内容并存储,跳转至test2.xml页面并显示用户名的功能。 修改AndroidManifest.xml文件,将新增的Activity(test2)注册。 调试代码并在模拟器中运行程序。
时间: 2024-02-01 22:13:58 浏览: 37
很好,我可以帮你完成这个任务。首先,让我们从创建一个新的Android工程开始。
1. 创建一个新的Android工程
在Android Studio中,点击“Start a new Android Studio project”,然后按照向导完成创建新的Android工程。我们将工程命名为"test1"。
2. 修改main.xml布局文件
打开res/layout/main.xml文件,我们将增加两个TextView,两个EditView和一个按钮。可以参考如下代码:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入用户名:" />
<EditText
android:id="@+id/edittext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入用户名" />
<TextView
android:id="@+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入密码:" />
<EditText
android:id="@+id/edittext2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入密码"
android:inputType="textPassword" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交" />
</LinearLayout>
```
3. 新建test2.xml布局文件
我们需要在新的页面(test2.xml)中显示添加的用户名。可以参考如下代码:
```
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="您已经成功添加了以下用户名:" />
<TextView
android:id="@+id/textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="" />
</LinearLayout>
```
4. 创建Test1.java和Test2.java两个Activity
在src/main/java/com.example.test1目录下,我们新建Test1.java和Test2.java两个Activity。可以参考如下代码:
Test1.java:
```
package com.example.test1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Test1 extends AppCompatActivity {
private EditText mUsernameEditText;
private EditText mPasswordEditText;
private Button mSubmitButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test1);
mUsernameEditText = findViewById(R.id.edittext1);
mPasswordEditText = findViewById(R.id.edittext2);
mSubmitButton = findViewById(R.id.button1);
mSubmitButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Test1.this, Test2.class);
intent.putExtra("username", mUsernameEditText.getText().toString());
startActivity(intent);
}
});
}
}
```
Test2.java:
```
package com.example.test1;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
public class Test2 extends AppCompatActivity {
private TextView mUsernameTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test2);
mUsernameTextView = findViewById(R.id.textview4);
Intent intent = getIntent();
String username = intent.getStringExtra("username");
mUsernameTextView.setText(username);
}
}
```
5. 修改AndroidManifest.xml文件
打开AndroidManifest.xml文件,我们需要将新增的Activity(test2)注册。可以参考如下代码:
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.test1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Test1"></activity>
<activity android:name=".Test2"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
```
6. 调试代码并在模拟器中运行程序
现在我们已经完成了所有的代码,可以在模拟器中运行程序进行测试。在Test1页面输入用户名和密码,点击提交按钮,就可以跳转到Test2页面并显示添加的用户名了。
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)