版权声明
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/11069907.html
设置显示内容与隐藏内容
if (isChecked){ editPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());//设置密码明文显示 editPassword.setSelection(editPassword.getText().length()); }else { editPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());//设置密码隐藏显示 editPassword.setSelection(editPassword.getText().length()); }
自动选中焦点,并且弹出输入框
EditText.setFocusable(true);//设置焦点打开 EditText.setFocusableInTouchMode(true);//设置为输入焦点mode EditText.requestFocus();//申请焦点 InputMethodManager inputManager = (InputMethodManager)commentEditText.getContext().getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.showSoftInput(EditText, 0)
释放焦点,并且隐藏输入框
private void clearFocus(){ //设置其他View可以自动获取焦,注意如果不设置会有一个问题,在你的布局里只有一个EditText的情况下,可能清除焦点后又会自动将焦点定位回去 mBack.setFocusableInTouchMode(true); mAddReplyEdit.clearFocus(); //清除焦点 InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE); if (imm.isActive() && getActivity().getCurrentFocus() != null) { imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS); } }
开启or关闭输入框是否可以点击
private void openOrClose(boolean openOrClose){ //注意,不要使用mAddReplyEdit.setFocusable(true);来开关聚焦,因为关闭后在开启会出现依然无法点击的问题 mAddReplyEdit.setFocusableInTouchMode(openOrClose); }
不弹出输入盘
直接在xml布局里添加无法点击和无法获取焦点就可以了
android:clickable="false"
android:focusable="false"
自定义光标
shape_white_rectangle.xml
<?xml version="1.0" encoding="utf-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" > <size android:width="2dp"/> <solid android:color="@color/white"/> </shape>
android:textCursorDrawable="@drawable/shape_white_rectangle"
获取光标框选文本的位置
int start = mTextView.getSelectionStart();
int end = mTextView.getSelectionEnd();
设置光标位置
editText.setSelection(editPassword.getText().length());
editText.setSelection(0, editPassword.getText().length());
输入法位置改变,改变输入框位置
android:windowSoftInputMode的值adjustPan或者adjustResize即可,像这样:
<activity android:name=".MainActivity" android:windowSoftInputMode="adjustPan" > ... </activity>
这个方法在一些ListView里使用的时候,输入框依然会被遮盖.
悬浮输入框,始终悬浮在输入法的上方
首先是清单xml里添加activity的属性如下
<activity android:name=".work.HomeActivity"
android:windowSoftInputMode="adjustPan"/>
然后是在布局文件里的输入框,因为是点击按钮后显示输入框,所以下面的输入框在布局最下面为隐藏状态,
<EditText android:id="@+id/comment_edittext" android:layout_width="match_parent" android:layout_height="52dp" android:textSize="@dimen/font_size_14" android:hint="评论" android:textColorHint="@color/fontColorAuxiliaryLightGray" android:singleLine="true" android:visibility="gone" android:focusable="true" android:focusableInTouchMode="true" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent"/>
监听输入法是否弹出
private void initGlobalLayoutListener(){ mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() { int mScreenHeight = 0; int mKeyboardHeight = 0; @Override public void onGlobalLayout() { Rect rect = new Rect(); // 测量当前窗口的显示区域 ((Activity)getContext()).getWindow().getDecorView() .getWindowVisibleDisplayFrame(rect); if(mScreenHeight <= 0){ mScreenHeight = ((WindowManager) getContext() .getSystemService(Context.WINDOW_SERVICE)) .getDefaultDisplay().getHeight(); } //计算出软键盘的高度 int keyboardHeight = mScreenHeight - rect.bottom; //如果keyboardHeight大于屏幕的五分之一, // 此时keyboardHeight有效,反之就是软键盘已经关闭了。 if (Math.abs(keyboardHeight) > mScreenHeight / 5) { mKeyboardHeight = keyboardHeight; if (mEdittext.getVisibility() == View.GONE){//此处多添加一次显示,因为OnGlobalLayoutListener的连续性会导致之前未触发键盘的判断还执行,然后又隐藏了输入框 mEdittext.setVisibility(View.VISIBLE); mEdittext.setFocusable(true); mEdittext.setFocusableInTouchMode(true); mEdittext.requestFocus(); mEdittext.setTag(true); } L.e("已经触发键盘"); }else { //获取输入法是否要显示的状态,注意别以为可以使用mEdittext.getVisibility()来替代,实际上是不行的, //因为OnGlobalLayoutListener的监听是连续触发并且有延迟,很容易在mEdittext显示的一瞬间就隐藏了 if ((boolean)mEdittext.getTag()){ mEdittext.setVisibility(View.GONE); mEdittext.setTag(false); } L.e("没有触发键盘"); } } }; mRootLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);//给xml里的根布局设置监听 }
点击后显示输入框
mBtn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mEdittext.setFocusable(true); mEdittext.setFocusableInTouchMode(true); mEdittext.requestFocus(); InputMethodManager inputManager = (InputMethodManager) mCommentEdittext.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);//获取输入法管理 inputManager.showSoftInput(mEdittext, 0);//要显示输入法的view mEdittext.setTag(true);//给输入框设一个标记,标示输入框已经显示 } });
最后注意移除监听
@Override public void onPause() { super.onPause(); mRootLayout.getViewTreeObserver().removeOnGlobalLayoutListener(mGlobalLayoutListener); }
输入状态监听
EditText editText = (EditText)findViewById(R.id.monitor_edit_text0); editText.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence text, int start, int before, int count) { //text 输入框中改变后的字符串信息 //start 输入框中改变后的字符串的起始位置 //before 输入框中改变前的字符串的位置 默认为0 //count 输入框中改变后的一共输入字符串的数量 textView1.setText("输入后字符串 [ " + text.toString() + " ] 起始光标 [ " + start + " ] 输入数量 [ " + count+" ]"); } @Override public void beforeTextChanged(CharSequence text, int start, int count,int after) { //text 输入框中改变前的字符串信息 //start 输入框中改变前的字符串的起始位置 //count 输入框中改变前后的字符串改变数量一般为0 //after 输入框中改变后的字符串与起始位置的偏移量 System.out.println(text.toString()); textView0.setText("输入前字符串 [ " + text.toString() + " ]起始光标 [ " + start + " ]结束偏移量 [" + after + " ]"); } @Override public void afterTextChanged(Editable edit) { //edit 输入结束呈现在输入框中的信息 textView2.setText("输入结束后的内容为 [" + edit.toString()+" ] 即将显示在屏幕上"); } });
代码上设置EditView的内容长度
/** * 设置输入框文本长度 * * @param length */ private void setEditTextMaxLength(int length) { InputFilter[] inputFilters = {new InputFilter.LengthFilter(length)}; dialogInputEdt.setFilters(inputFilters); }
删除EditView最后一位内容
int index = mEditPasswordEt.getSelectionStart(); String password = mEditPasswordEt.getText().toString(); if (TextUtils.isEmpty(password)) { return; } mEditPasswordEt.getText().delete(index - 1, index);
输入法的键值监听
mBinding.editTextTextPersonName.setOnEditorActionListener(object :TextView.OnEditorActionListener{ override fun onEditorAction(p0: TextView?, p1: Int, p2: KeyEvent?): Boolean { return true } })
end
本文来自博客园,作者:观心静 ,转载请注明原文链接:https://www.cnblogs.com/guanxinjing/p/11069907.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具