【安卓学习笔记】Android Studio第3课——EditText控件

EditText和TextView很相似,主要不同的是EditText是用户可以在上面编辑本文的,而TextView只能用app本身去改变和显示。

做了一个简单的登录界面:


xml代码如下

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/L3_text_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="用户名"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_marginTop="40dp"
        android:layout_marginLeft="10dp"/>

    <EditText
        android:id="@+id/L3_edt_name"
        android:layout_toRightOf="@id/L3_text_name"
        android:layout_alignBottom="@+id/L3_text_name"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:hint="Name"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:maxLines="1"/>
    <TextView
        android:id="@+id/L3_text_password"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="密码"
        android:textSize="20sp"
        android:textColor="#000000"
        android:layout_below="@+id/L3_text_name"
        android:layout_marginTop="20dp"
        android:layout_alignRight="@id/L3_text_name" />
    <EditText
        android:id="@+id/L3_edt_password"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:hint="PassWord"
        android:layout_toRightOf="@id/L3_text_password"
        android:layout_alignBottom="@id/L3_text_password"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:inputType="textPassword"
        android:paddingLeft="20dp"
        android:paddingRight="20dp"
        android:maxLines="1"/>

    <Button
        android:id="@+id/L3_button_login"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:text="登录"
        android:textSize="30sp"/>
</RelativeLayout>

这里主要说明一下EditText控件的属性,除了基本的属性外,有一个hint属性inputType(输入类型)属性。

1、hint属性:主要是在EditText框中做提示作用,显示为浅灰色背景,比如用户名框在用户没有输入任何字符的时候就显示一行“请输入用户名”的提示信息,当用户输入后信息消失。

2、inputType属性有很多常用的输入类型,这里用到密码输入就选用:textPassword,即输入的时候密码不可见,其他的也不多深究,以后用到再说。

下面是Activity的代码

package com.example.urien.secondapp;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class Lesson3_Activity extends AppCompatActivity {

    //1.声明控件
    private EditText L3_edt_name;
    private EditText L3_edt_password;
    private Button L3_button_login;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lesson3_);

        //2.找到相应的控件
        L3_edt_name = findViewById(R.id.L3_edt_name);
        L3_edt_password = findViewById(R.id.L3_edt_password);
        L3_button_login = findViewById(R.id.L3_button_login);


        //4.设置相应的监听器
        L3_button_login.setOnClickListener(L3_button_login_listener);
    }

    //3.实现监听器方法体内容
    Button.OnClickListener L3_button_login_listener = new Button.OnClickListener(){
        @Override
        public void onClick(View v) {
            String login = "登录成功";
            String name = L3_edt_name.getText().toString();
            String password = L3_edt_password.getText().toString();
            Toast.makeText(Lesson3_Activity.this,login + " " + name + " " + password,Toast.LENGTH_LONG).show();

        }
    };
}

代码也很简单,当用户电机登录按钮后,触发点击事件,处理的时候获取两个EditText中的内容并转成字符串,然后把两个都打印到屏幕上。

By Urien 2018年5月9日 16:37:25

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值