对于EditView软键盘的控制

package com.example.test_soft_input;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.os.IBinder;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

public class MainActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    /*
     * 屏幕点击判断是否隐藏软键盘。
     * 
     * @see android.app.Activity#dispatchTouchEvent(android.view.MotionEvent)
     */
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_DOWN) {
            View v = getCurrentFocus();
            if (isShouldHideInput(v, ev)) {
                hideSoftInput(v.getWindowToken());
            }
        }
        return super.dispatchTouchEvent(ev);
    }

    /**
     * 判断是否需要隐藏键盘,若点击EditText之外的区域,则表示需要隐藏键盘
     * 
     * @param v
     * @param event
     * @return
     */
    public boolean isShouldHideInput(View v, MotionEvent event) {
        if (v != null && v instanceof EditText) {
            int[] location = { 0, 0 };
            v.getLocationInWindow(location);
            int left = location[0];
            int top = location[1];
            int bottom = top + v.getHeight();
            int right = left + v.getWidth();
            if (event.getX() > left && event.getX() < right
                    && event.getY() > top && event.getY() < bottom) {
                return false;
            } else {
                return true;
            }
        }
        return false;
    }

    /**
     * 隐藏软键盘
     * 
     * @param token
     */
    public void hideSoftInput(IBinder token) {
        if (token != null) {
            InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
            imm.hideSoftInputFromWindow(token,
                    InputMethodManager.HIDE_NOT_ALWAYS);
        }

    }

}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <EditText
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="请输入密码..." />

</RelativeLayout>

 

转载于:https://www.cnblogs.com/zjc0514/p/4501029.html

你可以使用一个 ImageView控制 EditText 显示和隐藏密码。在点击 ImageView 时,切换 EditText 的输入类型即可。 首先,在布局文件中添加一个 ImageView 和一个 EditText,例如: ``` <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <EditText android:id="@+id/password_edittext" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:hint="Password" android:inputType="textPassword"/> <ImageView android:id="@+id/show_password_imageview" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/ic_show_password"/> </LinearLayout> ``` 然后,在代码中获取 ImageViewEditText,并在 ImageView 的点击事件中切换 EditText 的输入类型: ``` ImageView showPasswordImageView = findViewById(R.id.show_password_imageview); EditText passwordEditText = findViewById(R.id.password_edittext); showPasswordImageView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { int inputType = passwordEditText.getInputType(); if (inputType == InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD) { passwordEditText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD); showPasswordImageView.setImageResource(R.drawable.ic_show_password); } else { passwordEditText.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD); showPasswordImageView.setImageResource(R.drawable.ic_hide_password); } // move cursor to the end of the text passwordEditText.setSelection(passwordEditText.getText().length()); } }); ``` 这里需要注意的是,当切换输入类型时,EditText 中的光标位置会丢失,所以需要在切换后将光标移动到文本末尾。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值