您的位置:首页 > 移动开发 > Android开发

Android EditText 获得焦点不显示光标

2017-07-11 18:42 561 查看
在项目中使用EditText的时候,进去设置获取焦点,是有光标显示。

可有时候我们的项目并不只有一个EditText所以会有第二第三个EditText获取焦点。

然而在项目实际开发的时候发现了,除了第一次进入后有焦点显示光标外,再点击别的EditText监听到有焦点了,但是光标没显示了。

网上找了一圈,也就三种设置方法,试了一圈没用。下面也贴一下吧,都是网上复制过来的。也许下次项目前三个设置下有显示光标,就不用折腾后面的方法了。

可以说,下面这几个方法设置了都没用

1.在Edittext中加入以下属性

android:cursorVisible=”true”

android:textCursorDrawable=”@null”

2.在Edittext中加入以下属性

android:cursorVisible=”true”

android:textCursorDrawable=”@drawable/test_cursor”

对应的drawable文件

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">

<size android:width="1dp" />

<span style="font-family: Arial, Helvetica, sans-serif;"><!-- 光标宽度可以自己定义 --></span>

<solid android:color="#008000" /><!-- 光标颜色可以自己定义 -->
</shape>


3.如果以上没有效果就请用这个

明确指定EditText的inputType属性值inputType属性中的textCapSentences

不要用这个,国内手机好像没有用到这个,个人证实而已,用text或者textMultiLine

android:inputType=”text|textMultiLine”

4.代码里设置显示光标

在请求出现光标是,也就是在获取焦点时:

editText.requestFocus();

清除光标,也就是失去焦点:

editText.clearFocus();

另外还有:

使光标移动到指定的位置:

editText.setSelection(2);

输入的参数是个整数

让EditText不出现光标:

editText.setCursorVisible(false);

获取焦点

editText.requestFocus();

虽然上面几个没用,但也贴出来吧。以后说不定用得着呢。

下面就是解决方法了,有时候还是谷歌靠谱。直接英文搜索,就有了。不过本人英文太差,也看下一懂半懂,只要关注一些关键英文就行了。

// 焦点监听
editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {

if (hasFocus) {
// 这里是关键代码了
editText.setTextIsSelectable(true);
PhoneUtils.showKeyBoard(SendAlbumActivity.this,editText);
}
}
});

还有一个显示输入法的,也一起贴出来吧
/**
* 显示软键盘
*/
public static void showKeyBoard(Context context,View view){
if (context == null) return;
InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(view,InputMethodManager.SHOW_FORCED);
}


上面那个设置是从谷歌上找到的,抱着试下的心态添加了代码,

方法的意思是设置文本是可选的(谷歌翻译过来的)。点击进去看了

TextView 源码中的方法
/**
* Sets whether the content of this view is selectable by the user. The default is
* {@code false}, meaning that the content is not selectable.
* <p>
* When you use a TextView to display a useful piece of information to the user (such as a
* contact's address), make it selectable, so that the user can select and copy its
* content. You can also use set the XML attribute
* {@link android.R.styleable#TextView_textIsSelectable} to "true".
* <p>
* When you call this method to set the value of {@code textIsSelectable}, it sets
* the flags {@code focusable}, {@code focusableInTouchMode}, {@code clickable},
* and {@code longClickable} to the same value. These flags correspond to the attributes
* {@link android.R.styleable#View_focusable android:focusable},
* {@link android.R.styleable#View_focusableInTouchMode android:focusableInTouchMode},
* {@link android.R.styleable#View_clickable android:clickable}, and
* {@link android.R.styleable#View_longClickable android:longClickable}. To restore any of these
* flags to a state you had set previously, call one or more of the following methods:
* {@link #setFocusable(boolean) setFocusable()},
* {@link #setFocusableInTouchMode(boolean) setFocusableInTouchMode()},
* {@link #setClickable(boolean) setClickable()} or
* {@link #setLongClickable(boolean) setLongClickable()}.
*
* @param selectable Whether the content of this TextView should be selectable.
*/
public void setTextIsSelectable(boolean selectable) {
if (!selectable && mEditor == null) return; // false is default value with no edit data

createEditorIfNeeded();
if (mEditor.mTextIsSelectable == selectable) return;

mEditor.mTextIsSelectable = selectable;
setFocusableInTouchMode(selectable);
setFocusable(selectable);
setClickable(selectable);
setLongClickable(selectable);

// mInputType should already be EditorInfo.TYPE_NULL and mInput should be null

setMovementMethod(selectable ? ArrowKeyMovementMethod.getInstance() : null);
setText(mText, selectable ? BufferType.SPANNABLE : BufferType.NORMAL);

// Called by setText above, but safer in case of future code changes
mEditor.prepareCursorControllers();
}


大概意思是该View的内容是否可由用户选择,默认为是

如果为false 表示内容不可选。

其实上面说啥我也不懂,英文好的同学可以看下,关键的是方法里面调用的那几个方法。看到名称大概就懂了。

上面设置之后,发现点击光标是有了。然而输入法没有显示。这里也偷懒了,既然没显示那我就调用下显示输入法就行了。

上面只当是个记录吧。希望下次再遇到这种问题可以有个找的地方。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  android EditText 光标