安卓常用控件 - 编辑框

文章目录
一、继承关系图
二、编辑框常用属性
三、案例:用户注册
1、创建安卓应用
2、准备图片素材
3、主界面与主布局资源文件都更名
4、创建信息界面类
5、字符串资源文件
6、注册界面布局资源文件
7、信息界面类布局资源文件
8、用户注册界面类实现功能
9 、注册信息界面类
一、继承关系图
EditText是TextView的子类,用于接收用户输入的数据

课后大家可以去玩一玩EditText的子类AutoCompleteTextView

二、编辑框常用属性
属性 含义
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、创建安卓应用
基于Empty Activity创建安卓应用 UserRegistration
在这里插入图片描述

单击【finish】按钮
在这里插入图片描述

2、准备图片素材
将两张图片拷贝到drawable目录
在这里插入图片描述

3、主界面与主布局资源文件都更名
将主界面类MainActivity更名为注册界面类RegistrationActivity
将主布局资源文件activity_main.xml更名为注册布局资源文件activty_registration.xml。
在这里插入图片描述

4、创建信息界面类
基于Empty Activity模板创建信息界面类。
在这里插入图片描述
在这里插入图片描述
5、字符串资源文件
在这里插入图片描述

用户注册 姓名: 性别: 年龄: 电话: 邮箱: 主页: 备注: 注册 取消

6、注册界面布局资源文件
注册界面布局资源文件- activity_registration.xml
在这里插入图片描述

<?xml version="1.0" encoding="utf-8"?>

<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/et_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/et_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/et_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/et_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/et_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/et_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/et_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>

7、信息界面类布局资源文件
显示信息界面布局资源文件activity_information.xml
在这里插入图片描述

查看代码

<?xml version="1.0" encoding="utf-8"?>

<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" />

8、用户注册界面类实现功能
用户注册界面类RegistrationActivity
在这里插入图片描述

声明变量
在这里插入图片描述

查看完整代码
package net.xyx.userregistration;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

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.activty_registration);

        // 通过资源标识符获得控件实例
        edtName = findViewById(R.id.et_Name);
        edtGender = findViewById(R.id.et_Gender);
        edtAge = findViewById(R.id.et_Age);
        edtPhone = findViewById(R.id.et_Phone);
        edtEmail = findViewById(R.id.et_Email);
        edtHomePage = findViewById(R.id.et_HomePage);
        edtMemo = findViewById(R.id.et_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, information.class);
        // 利用意图携带数据包
        intent.putExtras(data);
        // 按意图启动目标窗口
        startActivity(intent);
    }

    /**
     * 取消按钮单击事件处理方法
     *
     * @param view
     */
    public void doCancel(View view) {
        finish();
    }
}

9 、注册信息界面类
注册信息显示界面- infromation
在这里插入图片描述

获取意图及其携带的数据,用来设置标签内容
在这里插入图片描述

查看代码

package net.xyx.userregistration;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class information 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);

    }
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值