将"编辑文本背景"设置为透明后,闪烁的光标消失了



我已经动态创建了 EditTexts,我希望在创建后有一个闪烁的光标,但我不希望它们有任何底线。根据我发现的建议,我通过将EditTexts的背景设置为透明(editText.setBackgroundResource(android.R.color.transparent(,如文档所建议的那样(。但是现在当创建EditText时,它没有闪烁的光标,尽管我为它请求焦点(editText.requestFocus(((。检查后,我发现它真的很集中。

到目前为止,我已经尝试了我能找到的所有方法(例如,this:http://techfeeding.blogspot.com/2015/10/set-cursor-drawable-programmatically-in.html(,但它对显示光标没有帮助 - 它仅在您开始键入时才出现,但这不是所需的行为。有人建议设置输入类型,但它也没有帮助:setInputType(InputType.TYPE_CLASS_TEXT(。将 isFocusable(( 和 isFocusableInTouchMode(( 设置为 true 也没有给出任何结果。

EditTexts必须是透明的,它们不能有彩色背景,因此将BG颜色更改为另一种颜色不是解决方案。

当通过XML(在另一个EditText上(将背景设置为透明时,它工作正常 - 调用requestFocus((后光标在那里。但是,当以编程方式执行相同的操作时,结果会有所不同。如何通过代码实现此目的?

自定义视图中的代码:

EditText edText = new EditText(getContext());
RelativeLayout.LayoutParams params = new 
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
RelativeLayout.LayoutParams.WRAP_CONTENT);
rootLayout.addView(edText, params);
params.leftMargin = Math.round(pointX);
params.topMargin = Math.round(pointY);
edText.setLayoutParams(params);
edText.setBackgroundResource(android.R.color.transparent);
edText.requestFocus();

要设置自定义编辑文本背景宽度闪烁自定义光标,您必须android:textCursorDrawable用于设置自定义光标,android:background用于自定义编辑文本。

请参阅下面的XML代码,我创建了两个自定义XML文件,一个用于EditText的背景,另一个用于闪烁光标。

编辑文本 XML 代码

<EditText
android:id="@+id/edt1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:hint="Enter Number 1"
android:textAlignment="center"
android:inputType="numberDecimal"
android:layout_margin="20dp"
android:background="@drawable/edittext_shape"
android:textColor="#fff"
android:focusable="true"
android:textCursorDrawable="@drawable/cursor_widht"
/>

编辑文本背景 XML 代码

<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:endColor="@color/colorAccent"
android:centerColor="@color/colorAccent"
android:startColor="@color/colorAccent"
android:angle="270" />
<corners
android:radius="30dp" />
<padding
android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp" />

编辑文本光标 XML 代码:

<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle" >
<size android:width="4dp" />
<solid android:color="#FF0910DD"/>

您可以使用字段在编辑文本中设置光标。

LinearLayout rootLayout = (LinearLayout) findViewById(R.id.rootlayout);
rootLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout temp = new LinearLayout(this);
temp.setOrientation(LinearLayout.HORIZONTAL);
temp.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));

EditText editText = new EditText(this);
editText.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,1));
editText.setBackground(ContextCompat.getDrawable(context,R.drawable.edittext_shape));
temp.addView (editText);

rootLayout.addView(temp);

上面的代码用于编辑文本,我在哪里使用我的CUstom形状而不是oF Transprent您可以根据需要进行选择。

try {
Field f = MainActivity.class.getDeclaredField("mCursorDrawableRes");
f.setAccessible(true);
f.set(editText, R.drawable.cursor_widht);
}catch (Exception e){
e.printStackTrace();
}

借助字段,您可以设置光标。希望此代码可根据您的要求工作。

最新更新