目录
一.本文章要学习的内容
二.代码部分
(一)创建项目
(二)如何创建文件
1.在java文件夹下的com.example.demo包下右键新建Activity里的Empty Views Activity
2.找到res文件夹下的layout文件夹下的.xml文件
3.打开AndroidManifest.xml文件
(三)项目结构
(四)MainActivity.java
(五)StudentDetailsActivity.java
(六)activity_main.xml
(七)activity_student_details.xml
(八)常见问题
三.运行部分
(一)打开Device Manager
(二)选择上次创建的虚拟机(具体见博主上一篇发布的Android文章)
(三)运行
(四)运行成功
一.本文章要学习的内容
1.掌握Activity的基本功能;
2.掌握preference的基本功能;
3.掌握断点的设置,调试程序;
任务1:通过intent实现跳转,完成Activity之间的跳转;
任务2:intent数据的传递;
任务3:采用用preference实现随数据的存储;
任务4:掌握在虚拟机和真机环境下,对程序的调试;
1、实现Android界面,并通过intent实现跳转,界面显示学生的姓名,学号,email.
2、要求intent的实现传递姓名,学号,email等数据,到第二个activity;
3、同时要求可以在虚拟机及手机上运行结果;
4、采用preference实现对如上数据的存储,存储及读取。
5、学会如何设置断点,并学会debug模式下,调试程序。
二.代码部分
(一)创建项目
(二)如何创建文件
1.在java文件夹下的com.example.demo包下右键新建Activity里的Empty Views Activity
2.找到res文件夹下的layout文件夹下的.xml文件
编辑代码界面在下图红色框位置
3.打开AndroidManifest.xml文件
android:name=".MainActivity"指定了主活动的全限定名,即当前包下的MainActivity类。android:exported="true"表示这个Activity可以被其他应用通过Intent直接访问,通常对于提供给用户交互的主界面设置为true。
(三)项目结构
(四)MainActivity.java
package com.example.demo;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class MainActivity extends AppCompatActivity {
Button button;
EditText edit1,edit2,edit3;
String s1,s2,s3;
SharedPreferences.Editor mEditor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_main);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
SharedPreferences sharedPreferences = getSharedPreferences("userinfo",MODE_PRIVATE);
edit1 = (EditText)findViewById(R.id.edit1);
edit2 = (EditText)findViewById(R.id.edit2);
edit3 = (EditText)findViewById(R.id.edit3);
String id = sharedPreferences.getString("id","123456");
String name = sharedPreferences.getString("name","dalaodedalao");
String email = sharedPreferences.getString("email","123@qq.com");
edit1.setText(id);
edit2.setText(name);
edit3.setText(email);
mEditor = sharedPreferences.edit();
button=findViewById(R.id.btn_MainActivity);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Toast.makeText(MainActivity.this,"你点击了按钮",Toast.LENGTH_LONG).show();表示点击时会显示跳转按钮
Intent intent=new Intent(MainActivity.this,StudentDetailsActivity.class);
s1 = edit1.getText().toString();
s2 = edit2.getText().toString();
s3 = edit3.getText().toString();
mEditor.putString("id",s1);
mEditor.putString("name",s2);
mEditor.putString("email",s3);
mEditor.commit();
Bundle bundle = new Bundle();
bundle.putString("edit1",s1);
bundle.putString("edit2",s2);
bundle.putString("edit3",s3);
intent.putExtras(bundle);
startActivity(intent);
}
});
}
}
(五)StudentDetailsActivity.java
package com.example.demo;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
public class StudentDetailsActivity extends AppCompatActivity {
Button button2;
TextView txt11, txt22, txt33;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
EdgeToEdge.enable(this);
setContentView(R.layout.activity_student_details);
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
return insets;
});
txt11 = (TextView) findViewById(R.id.txt11);
txt22 = (TextView) findViewById(R.id.txt22);
txt33 = (TextView) findViewById(R.id.txt33);
button2 = (Button) findViewById((R.id.btn2));
Bundle bb = this.getIntent().getExtras();
String str1 = bb.getString("edit1");
String str2 = bb.getString("edit2");
String str3 = bb.getString("edit3");
txt11.setText(str1);
txt22.setText(str2);
txt33.setText(str3);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent2 = new Intent(StudentDetailsActivity.this,MainActivity.class);
intent2.setClass(StudentDetailsActivity.this,MainActivity.class);
startActivityForResult(intent2, 0);
}
});
}
}
(六)activity_main.xml
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" tools:context=".MainActivity"> android:id="@+id/txt" android:layout_width="96dp" android:layout_height="57dp" android:layout_marginTop="24dp" android:text="第一页" android:textSize="30dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.526" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> android:id="@+id/edit1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/txt" android:layout_centerHorizontal="true" android:layout_marginTop="140dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.127" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/edit2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/edit1" android:layout_centerHorizontal="true" android:layout_marginTop="256dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.127" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/edit3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/edit2" android:layout_centerHorizontal="true" android:layout_marginTop="404dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.127" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
(七)activity_student_details.xml
xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/main" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_gravity="center_horizontal" tools:context=".StudentDetailsActivity"> android:id="@+id/txt" android:layout_width="96dp" android:layout_height="57dp" android:layout_marginTop="64dp" android:text="第二页" android:textSize="30dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.498" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent"> android:id="@+id/txt11" android:layout_width="158dp" android:layout_height="58dp" android:layout_below="@id/txt" android:layout_centerHorizontal="true" android:layout_marginTop="168dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.159" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/txt22" android:layout_width="167dp" android:layout_height="61dp" android:layout_below="@id/txt11" android:layout_centerHorizontal="true" android:layout_marginTop="284dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.161" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> android:id="@+id/txt33" android:layout_width="169dp" android:layout_height="66dp" android:layout_below="@id/txt22" android:layout_centerHorizontal="true" android:layout_marginTop="384dp" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.156" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" />
(八)常见问题
This view is not constrained. It only has designtime positions, so it will jump to (0,0) at runtime unless you add the constraints less... (Ctrl+F1)
Inspection info:The layout editor allows you to place widgets anywhere on the canvas, and it records the current position with designtime attributes (such as layout_editor_absoluteX). These attributes are not applied at runtime, so if you push your layout on a device, the widgets may appear in a different location than shown in the editor. To fix this, make sure a widget has both horizontal and vertical constraints by dragging from the edge connections.
这个情况是在使用ConstraintLayout布局中出现的,它并不是错误,只是个警告,忽略它仍然能够运行,但是view没有使用约束布局提供的参数,所以这个view在运行时会跳到(0,0)的位置。所以在使用View时应该为它设置如下参数:
app:layout_constraintStart_toStartOf app:layout_constraintEnd_toEndOf app:layout_constraintTop_toTopOf app:layout_constraintBottom_toBottomOf
当然并不需要全部设置,只需要设置其中几个,能够确定它的位置就行了,
如设置:
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
这样就表示控件在最上方,它表示该控件的左边“贴着”ConstraintLayout的左边,右边“贴着”ConstraintLayout的右边,上边“贴着”ConstraintLayout的上边。这样就能确定控件的位置了。
或者:
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
这样也能确定它的位置。
三.运行部分
(一)打开Device Manager
(二)选择上次创建的虚拟机(具体见博主上一篇发布的Android文章)
(三)运行
(四)运行成功