Android开发:EditText组件的基本使用

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/CC1991_/article/details/81558672

      在Android开发过程中,EditText组件是一个常用控件,也是一个比较重要的组件,它的基本属性需要熟练使用才行,接下来就来讲一下在Android开发中EditText组件的基本使用。

一、EditText简介
       ED(EditText的简称)在开发中也是经常使用到而且比较重要的一个控件,它是用户跟应用进行数据传输的窗口,比如实现一个登陆界面, 需要用户输入账号和密码,然后我们开发者获取到用户输入的内容,提交给后台服务器进行判断再做相应的处理。

二、EditText支持的XML属性以及基础的用法    

setText(CharSequence text) 设置文本内容

setTextColor(int color) 设置字体颜色值

setHint(int resid) 内容为空时候显示的文本

void setHintTextColor(int color) 内容为空时候显示的文本颜色值

setInputType(int type)      限制输入类型

number:整数类型

numberDecimal:小数点类型

date:日期类型

text:文本类型(默认值)

phone:拨号键盘

textPassword:密码

textVisiblePassword:可见密码

textUri:网址

setMaxLines(int maxlines) 设置文本的最小行数

setGravity(int gravity) 设置文本位置,如设置成“center”,文本将居中显示。

setLines(int lines) 设置文本的行数,设置两行就显示两行,即使第二行没有数据。

setSingleLine()     true:单行显示 false:可以多行

        EditText示例:开发中常用的登录界面
        首先我们来看布局文件: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"
    android:orientation="vertical"
    android:background="@drawable/main_background"
    tools:context="com.teamosa.Teamosa.mvvm.activity.LoginActivity">

    <EditText
        android:id="@+id/login_accout"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="100dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:drawablePadding="8dp"
        android:drawableLeft="@mipmap/user_account"
        android:hint="请输入手机号码/邮箱地址"
        android:background="@drawable/btn_shape" />

    <EditText
        android:id="@+id/login_password"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="20dp"
        android:layout_marginLeft="20dp"
        android:layout_marginRight="20dp"
        android:drawablePadding="8dp"
        android:drawableLeft="@mipmap/user_password"
        android:hint="请输入密码"
        android:background="@drawable/btn_shape" />

    <Button
        android:id="@+id/login_login"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="70dp"
        android:layout_marginLeft="@dimen/dp20"
        android:layout_marginRight="@dimen/dp20"
        android:text=“登录”
        android:textColor="@color/white"
        android:background="@drawable/btn_shape"/>
</LinearLayout>

       以上这两个输入框的使用了EditText的常用属性,值得借鉴的是EditText组件drawableLeft属性设置的图片和hint设置的文字之间的距离。有的时候,需要在文本框里放置icon图片,并且设置默认提示文字的时候,需要设置两者之间的间距,就是之前的手机icon和”请输入手机号“之间的距离。下面再说一下上面没有用到的属性:
        1、android:background=”@null”     输入框无背景 
       2、android:drawableBottom=”@drawable/shape_bottom_line”   底部引入一个shape布局文件,该布局文件就是输入框的下划线。shape_bottom_line.xml内容如下:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >
    <solid android:color="#1E7EE3" />
    <size android:height="1dp" android:width="500dp"/>
</shape>

    三、EditText的其他功能
       1、监听用户输入的内容. 
       例如,一个搜索框,只要用户输入了内容就去请求服务器,于是就要在Activity里面监听EditeText文本改变事件,具体实现如下所示:
EditText etOne= (EditText) findViewById(R.id.phone);
etOne.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                Log.i("And","内容改变之前调用:"+s);
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                Log.i("And","内容改变,可以去告诉服务器:"+s);
            }

            @Override
            public void afterTextChanged(Editable s) {
                Log.i("And”,”内容改变之后调用:"+s);
            }


    2、EditText 在左边加入图片,代码如下:  
        Bitmap bitmap = mWebView.getFavicon();
        Drawable drawable = new BitmapDrawable(bitmap);
        drawable = this.getResources().getDrawable(R.drawable.history);
        edit.setCompoundDrawablesWithIntrinsicBounds(drawable, null, null, null);
        edit.setText(cur_url);
 
          setCompoundDrawablesWithIntrinsicBounds与setCompoundDrawables的区别:
          setCompoundDrawables画的drawable的宽高是按drawable.setBound()设置的宽高,所以才有The Drawables must already have had setBounds(Rect) called.
         而setCompoundDrawablesWithIntrinsicBounds画的drawable的宽高是按drawable固定的宽高,即通过getIntrinsicWidth()与getIntrinsicHeight()获得,所以才有The Drawables' bounds will be set to their intrinsic bounds.
          查看下面的方法:
public void setCompoundDrawablesWithIntrinsicBounds(Drawable left, Drawable top,
            Drawable right, Drawable bottom) {
        if (left != null) {
            left.setBounds(0, 0, left.getIntrinsicWidth(), left.getIntrinsicHeight());
        }
        if (right != null) {
            right.setBounds(0, 0, right.getIntrinsicWidth(), right.getIntrinsicHeight());
        }
        if (top != null) {
            top.setBounds(0, 0, top.getIntrinsicWidth(), top.getIntrinsicHeight());
        }
        if (bottom != null) {
            bottom.setBounds(0, 0, bottom.getIntrinsicWidth(), bottom.getIntrinsicHeight());
        }
        setCompoundDrawables(left, top, right, bottom);


    3、获取、设置EditText的文本
         在开发过程中,使用EditText的时候,需要获取到EditText的文本值,其实获得EditText控件的值方法也就是调用EditText的方法,获取文本值,具体步骤如下所示:
         首先,在xml中定义EditText控件editText1;
         然后在activity里面,声明生成一个EditText对象,然后获取文本值即可:
    String str = "";  
    EditText editText1 =(EditText)findViewById(R.id.editText1);  
    str = editText.getText().toString();   //str即为获取的String类型的文本值
    或者用方法  Integer.parseInt(editText.getText().toString());


         
        //将文本框1的文本赋给文本框2          
       

     EditText editText2 =(EditText)findViewById(R.id.editText2);       
       

     editText2.setText(str.toCharArray(), 0, str.length()); 


        以上就是本节全部内容, 欢迎关注三掌柜的微信公众号,欢迎关注!

猜你喜欢

转载自blog.csdn.net/CC1991_/article/details/81558672