问题描述
我不希望用户在EditText框的中间插入任何内容.我只想允许用户单击EditText框以拉出键盘,然后在EditText框的末尾插入.
I don't want the user to insert anything in the middle of the EditText box. I only want to allow the user to click on the EditText box to bring out the keyboard, and insert at the end of the EditText box.
是否有一种方法可以禁止用户在单击EditText框时更改光标位置?
Is there a way to disable the ability for the user to change the cursor position when clicking on the EditText box?
我不在乎答案是使用XML还是Java.
I don't mind if the answer if done in XML or Java.
谢谢:)
如果必须使用setSelection,是否可以检测单击EditText框的时间?这样,当用户单击EditText时,我可以将光标重新设置到末尾.
if I have to use setSelection, is there a way to detect when the EditText box is clicked? so that I can set the cursor back to the end when the user click on the EditText.
推荐答案
您可以应用此技巧,这对我有用.只需将光标从edittext禁用,然后在其click侦听器上添加代码,即可将光标保持在最右边.
You can apply this trick, This worked for me.Just disable the cursor from edittext and on its click listener add your code that will keep the cursor on right most.
这是我尝试过的代码.
<EditText
android:id="@+id/edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="12"
android:background="@color/white"
android:padding="@dimen/activity_margin_10"
/>
Java代码
EditText mEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
mEditText = (EditText)findViewById(R.id.edit); // Initialzation of Edittext
mEditText.setSelection(mEditText.getText().length()); // After initialization keep cursor on right side
mEditText.setCursorVisible(false); // Disable the cursor.
/*
Add Click listener and on put your code that will keep cursor on right side
*/
mEditText.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mEditText.setSelection(mEditText.getText().length());
}
});
}
希望它会对您有所帮助.
Hope it will help you.
这篇关于如何为EditText固定光标位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!