android 点击Edittext时hint值不自动隐藏问题

android 点击Edittext时hint值不自动隐藏问题

当我们做注册界面或登录等界面时,需要在EditText里设置hint的值,来提醒用户该输入什么东西。但Android的EditText有一个不好的地方在于,当我们点击EditText时,它不会自动隐藏hint值,本文主要就是解决这个问题。

1.在这里,需要用到几个属性值:

 android:focusable="true"//是否可聚焦
 android:focusableInTouchMode="true"//是否是触摸方式获取焦点

要想让一个EditText默认状态为没有获得焦点,则必须在它的父布局里写入上述两个属性。

直接贴代码:
首先建立一个register.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="@color/back_gray"
    android:id="@+id/main_ll"
    android:orientation="vertical" >
    <LinearLayout android:orientation="horizontal"
        android:background="@color/white" 
        android:layout_width="fill_parent" 
        android:layout_height="@dimen/height_50">
        <ImageView android:id="@+id/image_back"
            android:layout_width="30dp" 
            android:layout_height="wrap_content" 
            android:layout_marginLeft="15.0dip" 
            android:background="@color/white"
            android:src="@drawable/row2" />
        <TextView android:textSize="@dimen/text_size_20" 
            android:textColor="#000000"
            android:background="@color/white"
            android:gravity="center" 
            android:layout_width="fill_parent" 
            android:layout_height="fill_parent" 
            android:text="注册" />
    </LinearLayout>

    <LinearLayout 
        android:layout_marginTop="20dp"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        android:focusable="true"
        android:focusableInTouchMode="true"
        >
        <ImageView android:layout_width="25dp"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_height="25dp"
            android:src="@drawable/phonenum"/>
        <EditText 

            android:id="@+id/phonenum_et"
            android:layout_width="match_parent"
            android:background="@null"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#000000"
            android:hint="请输入您的手机号码"/>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent"
        android:background="@color/white"
        android:layout_height="1dp"
        android:orientation="horizontal">
        <TextView android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/back_gray"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"/>
    </LinearLayout>
     <LinearLayout 
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"
        >
        <ImageView android:layout_width="25dp"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_height="25dp"
            android:src="@drawable/yanzheng"/>
        <EditText 
            android:id="@+id/putems_et"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:background="@null"
            android:editable="true"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#000000"
            android:hint="请输入短信验证码"/>
        <Button android:id="@+id/btn"
            android:layout_marginTop="5dp"
            android:layout_marginBottom="5dp"
            android:layout_marginRight="10dp"
            android:layout_width="wrap_content"
            android:textSize="10dp"
            android:textColor="#7cfc00"
            android:background="@drawable/btn_shape"
            android:layout_gravity="center_vertical"
            android:layout_height="wrap_content"
            android:text="获取验证码"/>
    </LinearLayout>

     <LinearLayout android:layout_width="match_parent"
        android:background="@color/white"
        android:layout_height="1dp"
        android:orientation="horizontal">
        <TextView android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/back_gray"
            android:layout_marginLeft="8dp"
            android:layout_marginRight="8dp"/>
    </LinearLayout>
    <LinearLayout 
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:orientation="horizontal"

        >
        <ImageView android:layout_width="25dp"
            android:layout_gravity="center"
            android:layout_margin="10dp"
            android:layout_height="25dp"
            android:src="@drawable/pwd"/>
        <EditText 
            android:id="@+id/pwd_et"
            android:layout_width="match_parent"
            android:background="@null"
            android:editable="true"
            android:layout_height="wrap_content"
            android:layout_gravity="center_vertical"
            android:textColor="#000000"
            android:hint="请输入密码"/>
    </LinearLayout>

         <Button
             android:id="@+id/button1"
             android:layout_marginTop="20dp"
             android:layout_marginLeft="10dp"
             android:layout_marginRight="10dp"
             android:layout_width="match_parent"
             android:layout_height="wrap_content"
             android:background="@drawable/btn_shape_register"
             android:text="立即注册" />

</LinearLayout>

在RegisterAct中:

package com.yds.newpowerbike.activity;

import com.yds.newpowerbike.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.LinearLayout;

public class RegisterAct extends Activity{
    private EditText phonenum_et,putems_et,pwd_et;
    private LinearLayout ll;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);

        init();


    }

    private void init(){
        phonenum_et = (EditText) findViewById(R.id.phonenum_et);
        putems_et = (EditText) findViewById(R.id.putems_et);
        pwd_et = (EditText) findViewById(R.id.pwd_et);
        ll = (LinearLayout) findViewById(R.id.main_ll);


        phonenum_et.setOnFocusChangeListener(onFocusChangeListener);
        putems_et.setOnFocusChangeListener(onFocusChangeListener);
        pwd_et.setOnFocusChangeListener(onFocusChangeListener);
        ll.setOnTouchListener(onTouchListener);
    }
    /**
     * 给布局加触摸监听,当点击EditText之外的地方时
     * EditText失去焦点
     */
    private OnTouchListener onTouchListener = new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub
            ll.setFocusable(true);
            ll.setFocusableInTouchMode(true);
            ll.requestFocus();//布局获得焦点
            return false;
        }
    };

    /**
     * 下面的OnFocusChangeListener的作用主要是
     * 点击EditText时获取焦点并隐藏hint值
     * */
    private OnFocusChangeListener onFocusChangeListener = new OnFocusChangeListener() {

        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            // TODO Auto-generated method stub

            switch (v.getId()) {
            case R.id.phonenum_et:
                setHintEt(phonenum_et,hasFocus);

                break;
            case R.id.putems_et:
                setHintEt(putems_et,hasFocus);
                break;
            case R.id.pwd_et:
                setHintEt(pwd_et, hasFocus);
                break;
            default:
                break;
            }
        }
    };
    private void setHintEt(EditText et,boolean hasFocus){
        String hint;
        if(hasFocus){
            hint = et.getHint().toString();
            et.setTag(hint);
            et.setHint("");
        }else{
            hint = et.getTag().toString();
            et.setHint(hint);
        }
    }
}

运行截图:
这里写图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值