零、本讲学习目标
一、导入新课
- 在安卓应用中经常需要用户输入信息,比如用户名、密码、电话号码之类,一般采用编辑框来接收用户输入的任何文本信息。
(一)编辑框常用属性
属性 | 含义 |
---|
text | 文本内容 |
textSize | 文本字号,单位:sp |
textColor | 文本颜色,#ff0000 - 红色 |
hint | 提示信息 |
singleLine | 单行(true or false) |
layout_height | 高度,单位:dp (wrap_content, match_parent) |
layout_weight | 宽度,单位:dp (wrap_content, match_parent) |
inputType | 输入类型(普通文本、密码、邮件……) |
maxLines | 最大行数 |
lines | 行数 |
(二)教学案例:用户注册
1、创建安卓应用
- 创建安卓应用 - UserRegistration
2、准备图片素材
- 将两张背景图片拷贝到drawable目录
3、主界面与主布局资源文件更名
- 将主界面类MainActivity更名为注册界面类RegistrationActivity
- 将主布局资源文件activity_main.xml更名为注册布局资源文件activity_registration.xml
4、创建息界面类
- 创建信息界面类 - InformationActivity
5、字符串资源文件
- 字符串资源文件 - strings.xml
<resources>
<string name="app_name">用户注册</string>
<string name="name">姓名:</string>
<string name="gender">性别:</string>
<string name="age">年龄:</string>
<string name="phone">电话:</string>
<string name="email">邮箱:</string>
<string name="home_page">主页:</string>
<string name="memo">备注:</string>
<string name="register">注册</string>
<string name="cancel">取消</string>
</resources>
6、注册界面布局资源文件
- 注册界面布局资源文件 - activity_registration.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:background="@drawable/info"
android:gravity="center"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/name"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/gender"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_gender"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/age"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_age"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/phone"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_phone"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/email"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_email"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textEmailAddress"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_homePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/home_page"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_homePage"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textUri"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="@+id/tv_memo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/memo"
android:textColor="#000000"
android:textSize="16sp" />
<EditText
android:id="@+id/edt_memo"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:ems="10"
android:lines="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<Button
android:id="@+id/btn_register"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doRegister"
android:text="@string/register" />
<Button
android:id="@+id/btn_cancel"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:onClick="doCancel"
android:text="@string/cancel" />
</LinearLayout>
</LinearLayout>
- 查看预览效果
7、信息界面布局资源文件
- 信息界面布局资源文件 - activity_information.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:background="@drawable/img"
android:orientation="vertical"
android:padding="20dp" >
<TextView
android:id="@+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_gender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_phone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="phone"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="email"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_homePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:autoLink="web"
android:textColor="#0000ff"
android:textSize="18sp" />
<TextView
android:id="@+id/tv_memo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textColor="#0000ff"
android:textSize="18sp" />
</LinearLayout>
- 查看预览效果
8、用户注册界面类实现功能
- 用户注册界面类 - RegistrationActivity
package net.hdl.userregistration;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import androidx.appcompat.app.AppCompatActivity;
public class RegistrationActivity extends AppCompatActivity {
private EditText edtName;
private EditText edtGender;
private EditText edtAge;
private EditText edtPhone;
private EditText edtEmail;
private EditText edtHomePage;
private EditText edtMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局资源文件设置用户界面
setContentView(R.layout.activity_registration);
// 通过资源标识符获得控件实例
edtName = findViewById(R.id.edt_name);
edtGender = findViewById(R.id.edt_gender);
edtAge = findViewById(R.id.edt_age);
edtPhone = findViewById(R.id.edt_phone);
edtEmail = findViewById(R.id.edt_email);
edtHomePage = findViewById(R.id.edt_homePage);
edtMemo = findViewById(R.id.edt_memo);
}
/**
* 注册按钮单击事件处理方法
*
* @param view
*/
public void doRegister(View view) {
// 获取用户输入数据
String strName = edtName.getText().toString();
String strGender = edtGender.getText().toString();
String strAge = edtAge.getText().toString();
String strPhone = edtPhone.getText().toString();
String strEmail = edtEmail.getText().toString();
String strHomePage = edtHomePage.getText().toString();
String strMemo = edtMemo.getText().toString();
// 将各项输入数据打包
Bundle data = new Bundle();
data.putString("name", strName);
data.putString("gender", strGender);
data.putString("age", strAge);
data.putString("phone", strPhone);
data.putString("email", strEmail);
data.putString("home_page", strHomePage);
data.putString("memo", strMemo);
// 创建意图,指定起始组件与目标组件
Intent intent = new Intent(RegistrationActivity.this, InformationActivity.class);
// 利用意图携带数据包
intent.putExtras(data);
// 按意图启动目标窗口
startActivity(intent);
}
/**
* 取消按钮单击事件处理方法
*
* @param view
*/
public void doCancel(View view) {
finish();
}
}
9、注册信息界面类实现功能
- 注册信息显示界面 - InformationActivity
package net.hdl.userregistration;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class InformationActivity extends AppCompatActivity {
private TextView tvName;
private TextView tvGender;
private TextView tvAge;
private TextView tvPhone;
private TextView tvEmail;
private TextView tvHomePage;
private TextView tvMemo;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// 利用布局文件设置用户界面
setContentView(R.layout.activity_information);
// 通过资源标识获得控件示例
tvName = (TextView) findViewById(R.id.tv_name);
tvGender = (TextView) findViewById(R.id.tv_gender);
tvAge = (TextView) findViewById(R.id.tv_age);
tvPhone = (TextView) findViewById(R.id.tv_phone);
tvEmail = (TextView) findViewById(R.id.tv_email);
tvHomePage = (TextView) findViewById(R.id.tv_homePage);
tvMemo = (TextView) findViewById(R.id.tv_memo);
// 获得意图
Intent intent = getIntent();
if (intent != null) {
// 获得意图携带的数据包
Bundle bundle = intent.getExtras();
// 从数据包里按键取值
String strName = bundle.getString("name");
String strGender = bundle.getString("gender");
String strAge = bundle.getString("age");
String strPhone = bundle.getString("phone");
String strEmail = bundle.getString("email");
String strHomePage = bundle.getString("home_page");
String strMemo = bundle.getString("memo");
// 设置各个标签的内容
tvName.setText("姓名:" + strName);
tvGender.setText("性别:" + strGender);
tvAge.setText("年龄:" + strAge);
tvPhone.setText("电话:" + strPhone);
tvEmail.setText("邮箱:" + strEmail);
tvHomePage.setText("主页:" + strHomePage);
tvMemo.setText("备注:" + strMemo);
}
}
}
10、启动应用,查看效果