序文
アクティビティが Android の最も重要なコンポーネントの 1 つであることは誰もが知っています。ここでは、アクティビティの具体的な内容については詳しく説明しません。重要なのは、優先順位を区別することです。さらに詳しく知りたい場合は、これについては、著者の以前の記事を読むか、著者はこの記事が非常に詳細であると考えています
リンクhttp://t.csdnimg.cn/YXOph< /span>
目的
1. アクティビティの基本機能をマスターする;
2. 好みの基本機能をマスターする;
3. ブレークポイントの設定とデバッグをマスターするプログラム;
実験内容
タスク 1: インテントを介してジャンプして、アクティビティ間のジャンプを完了します。
タスク 2: インテント データの転送。
タスク 3: 設定を使用して、ランダム データを保存する;
タスク 4: 仮想マシンおよび実マシン環境でのプログラムのデバッグをマスターする;
実験要件
1. Android インターフェースを実装し、インテントを実行します。インターフェースには、学生の名前、学生番号、電子メールが表示されます。
2. インテントの実装では、名前を渡す必要があります。 、学生番号、電子メール、その他のデータを入力し、2 番目のアクティビティに進みます。
3. 同時に、結果を仮想マシンと携帯電話で実行する必要があります。 5. ブレークポイントの設定方法と、デバッグ モードでプログラムをデバッグする方法を学びます。
4. 設定を使用して、上記のデータの保存、保存、読み取りを実装します。
コード
主な活動
//mainActivity
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class mainActivity extends Activity {
//定义控件变量
private EditText etName;//姓名输入框
private EditText etNumber; // 学号输入框
private EditText etEmail; // email输入框
private Button btnSubmit; // 提交按钮
@SuppressLint({
"WrongViewCast", "MissingInflatedId"})
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化控件变量
etName = findViewById(R.id.et_name);
etNumber = findViewById(R.id.et_number);
etEmail = findViewById(R.id.et_email);
btnSubmit = findViewById(R.id.btn_submit);
// 设置按钮的点击事件监听器
btnSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// 获取输入框中的数据
String name = etName.getText().toString();
String number = etNumber.getText().toString();
String email = etEmail.getText().toString();
// 创建一个intent对象,用于跳转到ResultActivity
Intent intent = new Intent(mainActivity.this, ResultActivity.class);
// 将数据放入intent中,使用键值对的形式
intent.putExtra("name", name);
intent.putExtra("number", number);
intent.putExtra("email", email);
// 启动ResultActivity
startActivity(intent);
}
});
}
}
結果アクティビティ
// ResultActivity.java
// 这个类是程序的第二个界面,用于显示从MainActivity传递过来的数据
// 采用preference实现对数据的存储和读取
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.TextView;
public class ResultActivity extends mainActivity {
// 定义控件变量
private TextView tvName; // 姓名显示框
private TextView tvNumber; // 学号显示框
private TextView tvEmail; // email显示框
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
// 初始化控件变量
tvName = findViewById(R.id.tv_name);
tvNumber = findViewById(R.id.tv_number);
tvEmail = findViewById(R.id.tv_email);
// 获取从MainActivity传递过来的intent对象
Intent intent = getIntent();
// 从intent中获取数据,使用相同的键名
String name = intent.getStringExtra("name");
String number = intent.getStringExtra("number");
String email = intent.getStringExtra("email");
// 将数据显示在控件上
tvName.setText("姓名:" + name);
tvNumber.setText("学号:" + number);
tvEmail.setText("邮箱:" + email);
// 获取一个SharedPreferences对象,用于存储数据
SharedPreferences sp = getSharedPreferences("student_info", MODE_PRIVATE);
// 获取一个SharedPreferences.Editor对象,用于编辑数据
SharedPreferences.Editor editor = sp.edit();
// 将数据写入SharedPreferences中,使用相同的键名
editor.putString("name", name);
editor.putString("number", number);
editor.putString("email", email);
// 提交数据的修改
editor.apply();
// 调用show方法,从SharedPreferences中读取并打印数据
show();
}
public void show() {
// 获取一个SharedPreferences对象,用于读取数据
SharedPreferences sp = getSharedPreferences("student_info", MODE_PRIVATE);
// 从SharedPreferences中获取数据,使用相同的键名,如果没有找到,则使用默认值""
String name = sp.getString("name", "");
String number = sp.getString("number", "");
String email = sp.getString("email", "");
// 打印数据到控制台上,方便调试
System.out.println("姓名:" + name);
System.out.println("学号:" + number);
System.out.println("邮箱:" + email);
}
}
アクティビティメイン
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- 姓名输入框 -->
<EditText
android:id="@+id/et_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="请输入姓名" />
<!-- 学号输入框 -->
<EditText
android:id="@+id/et_number"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="请输入学号" />
<!-- email输入框 -->
<EditText
android:id="@+id/et_email"
android:layout_width="match_parent"
android:layout_height="48dp"
android:hint="请输入邮箱" />
<!-- 提交按钮 -->
<Button
android:id="@+id/btn_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="提交" />
</LinearLayout>
活動結果
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ExtraText">
android:orientation="vertical"
android:padding="16dp">
<!-- 姓名显示框 -->
<TextView
android:id="@+id/tv_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 学号显示框 -->
<TextView
android:id="@+id/tv_number"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- email显示框 -->
<TextView
android:id="@+id/tv_email"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
もちろん、自分が書いたページをmainifestに登録しないと認識されないので忘れずに。
結果表示