目录
最终要实现的效果
1. 观察有七个变量,在字符串资源里面定义变量
2.然后该线性布局
3.编写注册布局资源文件
代码提示:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:orientation="vertical"
tools:ignore="Suspicious0dp"
android:background="@mipmap/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center"
tools:ignore="Suspicious0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome1">
</TextView>
<EditText
android:id="@+id/edt_Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome2">
</TextView>
<EditText
android:id="@+id/edt_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome3">
</TextView>
<EditText
android:id="@+id/edt_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome4">
</TextView>
<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome5">
</TextView>
<EditText
android:id="@+id/edt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome6">
</TextView>
<EditText
android:id="@+id/edt_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome7">
</TextView>
<EditText
android:id="@+id/edt_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/button_border"
android:onClick="doLogin"
android:text="@string/login"
android:textSize="20dp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:onClick="doCancel"
android:text="@string/cancel"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
4.编写注册界面代码
1.注册界面
2.声明变量
3.通过控件资源标识符获取控件实例
4.获取用户输入的数据
5.如果有一个为空,则注册失败
6.编写事件处理方法
7.在注册页面插入背景
运行一下代码,看一下效果:
8.把注册界面的文件更名
9.基于模板创建主页面
10.修改注册页面
package net.yuanjing.userlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView tvMessage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tvMessage = findViewById(R.id.tv_message);
Intent intent = getIntent();
if (intent != null) {
String username = intent.getStringExtra("username");
String gender = intent.getStringExtra("gender");
String age = intent.getStringExtra("age");
String phone = intent.getStringExtra("phone");
String email = intent.getStringExtra("email");
String index = intent.getStringExtra("index");
String note = intent.getStringExtra("note");
String message = "注册成功\n姓名:" + username + "\n性别:" +gender +"\n年龄:" + age + "\n电话:" + phone + "\n邮箱:" + email + "\n主页:" +index + "\n备注:" + note;
tvMessage.setText(message);
}
}
}
11.LoginActivity.java
package net.yuanjing.userlogin;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
public class LoginActivity extends AppCompatActivity {
private EditText edname;
private EditText edgender;
private EditText edage;
private EditText edphone;
private EditText edemail;
private EditText edindex;
private EditText ednote;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//利用布局资源设置用户界面
setContentView(R.layout.activity_login);
//通过资源标识符获取控件实例
edname = findViewById(R.id.edt_Username);
edgender = findViewById(R.id.edt_gender);
edage = findViewById(R.id.edt_age);
edphone = findViewById(R.id.edt_phone);
edemail = findViewById(R.id.edt_email);
edindex = findViewById(R.id.edt_index);
ednote = findViewById(R.id.edt_note);
//注册时间处理方法
}
public void doLogin (View v){
//获取用户输入的数据
String username = edname.getText().toString().trim();
String gender = edgender.getText().toString().trim();
String age = edage.getText().toString().trim();
String phone = edphone.getText().toString().trim();
String email = edemail.getText().toString().trim();
String index = edindex.getText().toString().trim();
String note = ednote.getText().toString().trim();
if (!"".equals(username) && !"".equals(gender)&& !"".equals(age)&& !"".equals(phone)&& !"".equals(email)&& !"".equals(index)&& !"".equals(note)) {
Toast.makeText(this, "注册成功", Toast.LENGTH_SHORT).show();
//创建意图 intent(参数1:起始组件:参数2:目标组件)
Intent intent = new Intent(LoginActivity.this,MainActivity.class);
//携带数据 (键值对)
intent.putExtra("username", username);
intent.putExtra("gender", gender);
intent.putExtra("age", age);
intent.putExtra("phone", phone);
intent.putExtra("email", email);
intent.putExtra("index", index);
intent.putExtra("note", note);
//按意图实现界面跳转
startActivity(intent);
} else {
Toast.makeText(this, "注册失败", Toast.LENGTH_SHORT).show();
//清空用户名和密码文本框
edname.setText("");
edgender.setText("");
edage.setText("");
edphone.setText("");
edemail.setText("");
edindex.setText("");
ednote.setText("");
//让用户名文本框获取焦点
edname.requestFocus();
}
}
public void doCancel (View v){
//关闭当前窗口
finish();
}
}
12.activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:orientation="vertical"
tools:ignore="Suspicious0dp"
android:background="@mipmap/background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
android:gravity="center"
tools:ignore="Suspicious0dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome1">
</TextView>
<EditText
android:id="@+id/edt_Username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome2">
</TextView>
<EditText
android:id="@+id/edt_gender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome3">
</TextView>
<EditText
android:id="@+id/edt_age"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome4">
</TextView>
<EditText
android:id="@+id/edt_phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome5">
</TextView>
<EditText
android:id="@+id/edt_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome6">
</TextView>
<EditText
android:id="@+id/edt_index"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/welcome7">
</TextView>
<EditText
android:id="@+id/edt_note"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType=""
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_horizontal">
<Button
android:id="@+id/btn_login"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:background="@drawable/button_border"
android:onClick="doLogin"
android:text="@string/login"
android:textSize="20dp" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:background="@drawable/button_border"
android:onClick="doCancel"
android:text="@string/cancel"
android:textSize="20sp" />
</LinearLayout>
</LinearLayout>
运行的结果:
13.在活动栏上显示图标
14.修改一下主页面的字体边距
运行看哈效果: