有時,在EditText中粘貼文本時,會自動插入空格/空格。例如,如果將文本粘貼到文本字段中已包含的文本的中間或末尾,就會發生這種情況。有沒有辦法告訴EditText對象或ClipboardManager不應該自動插入前導空白和尾隨空白?Android EditText如何在粘貼文本時禁用自動插入空格?
回答
使用trim();它會從字符串前後刪除空格
str = str.trim();
我不知道在哪裏添加trim()的調用。當我通過ClipboardManager檢索剪貼板的內容時,不能找到空格。我也嘗試了一個InputFilter,但是這裏的過濾器方法只針對一個前導空白,一個針對尾隨空白,一次針對實際剪貼板內容。在InputFilter中,我無法決定一個空白是來自用戶還是這個studip粘貼方法。 – EricJobs 2013-04-29 14:21:46
好像我不是唯一一個被這種自動主義困擾的人:http://code.google.com/p/android/issues/detail?id=41037 – EricJobs 2013-04-29 14:32:38
是啊我從來沒有嘗試過這個,我讀過它,並認爲它將適用於你的問題 – JRowan 2013-04-29 16:49:59
這是一個Android bug。 Check this issue report。
它固定在5
我需要防止的「空間」之前確實與輸入過濾工作,所以我用的就是:
mSearchText.setOnKeyListener(new View.OnKeyListener() {
@Override
public boolean onKey(View view, int i, KeyEvent keyEvent) {
MyLog.d(this, keyEvent);
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_SPACE) {
switch (keyEvent.getAction()) {
case KeyEvent.ACTION_DOWN:
mSpacePressed = true;
break;
case KeyEvent.ACTION_UP:
mSpacePressed = false;
}
}
return false;
}
});
InputFilter mSearchFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence arg0, int arg1, int arg2, Spanned arg3, int arg4, int arg5)
{
//Prevent adding spaces on Paste
//Remember: if you want paste " " it will be prevented
if (arg0.toString().equals(" ")){
if (!mSpacePressed){
return "";
}
}
//do work with filter
return null;
}
};
mSearchText.setFilters(new InputFilter[]{mSearchFilter});
我來晚了,但在我的老片,我的EditText也像你說的那樣工作。
所以,我就像下面那樣修復它。
1.創建一個類來存儲關於起始索引和插入字符串長度的信息。
public class PasteUnit {
int start = 0;
int length = 0;
public PasteUnit(int start, int length) {
this.start = start;
this.length = length;
}
}
2.創建一個擴展EditText的類。
public class MyEditText extends EditText
{
boolean pasteStarted = false;
ArrayList<PasteUnit> pasteUnits = new ArrayList<>();
public MyEditText(Context context)
{
super(context);
}
public MyEditText(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public MyEditText(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
@Override
public boolean onTextContextMenuItem(int id) {
if(id==android.R.id.paste) {//This is called when the paste is triggered
pasteStarted = true;
}
boolean consumed = super.onTextContextMenuItem(id);
//the super.onTextContextMenuItem(id) processes the pasted string.
//This is where EditText acts weird.
//And we can watch what is happening in our TextWatcher to be added below
switch (id){
case android.R.id.paste://This is called after we collected all the information
if(pasteUnits.size()>1) {//When a space or spaces are inserted
int startIndex = pasteUnits.get(0).start;
int totalLength = 0;
for(int i=0;i<pasteUnits.size();i++) {
PasteUnit pasteUnit = pasteUnits.get(i);
totalLength = totalLength + pasteUnit.length;
}
int endIndex = startIndex + totalLength;
String string = this.getText().toString();
String before = string.substring(0, startIndex);
String after = string.substring(endIndex);
PasteUnit lastPasteUnit = pasteUnits.get(pasteUnits.size()-1);
String lastString = string.substring(lastPasteUnit.start, lastPasteUnit.start + lastPasteUnit.length);
String result = before + lastString + after;
this.setText(result);
this.setSelection(startIndex + lastString.length());
}
pasteUnits.clear();
pasteStarted = false;
break;
case android.R.id.copy:
break;
case android.R.id.cut:
break;
}
return consumed;
}
}
3.將TextWatcher添加到您的EditText中。
看到TextWatcher時,舊的EditText顯得非常奇怪。將字符串A粘貼到字符串B中時,它首先在字符串B中插入一個空格,然後在字符串B中插入另一個空格,並最後在兩個空格之間插入字符串A.
而且在像字符串B之後粘貼字符串中的其他情況下,首先附加字符串B之後一個空間和追加字符串A.
後,因此,無論如何,似乎插入在最後一步原始字符串。
MyEditText edit = (MyEditText) findViewById(R.id.mainEditText1);
edit.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if(edit.pasteStarted) {//when it is processing what we pasted
edit.pasteUnits.add(new PasteUnit(start, count));
//store the information about the inserted spaces and the original pasted string
}
}
});
- 1. 如何在粘貼時禁用Eclipse PDT自動格式代碼
- 2. 如何禁止在粘貼上插入空行?
- 3. Edittext禁用系統粘貼圖標
- 4. 如何防止在將文本粘貼到輸入字段時出現空格?
- 5. Android複製/粘貼禁用
- 6. Excel VBA插入InputBox粘貼大文本
- 7. 如何從/向EditText禁用複製/粘貼
- 8. UITextField如何禁用粘貼?
- 9. Handsontable - 在粘貼到單元格時禁用自動向上滾動
- 10. Android:如何在Edittext中完全禁用複製和粘貼功能
- 11. 如何讓android edittext在插入文本時顯示圖案?
- 12. 如何禁用VIM「插入(粘貼)」模式中的箭頭鍵?
- 13. WebStorm自動導入粘貼
- 14. 如何從Android上的剪貼板粘貼時刪除文本格式
- 15. Vim:在粘貼代碼時自動禁用iab替換
- 16. 如何禁用從EditText複製和粘貼
- 17. texarea在chrome中粘貼文本時插入換行符
- 18. Android攔截在editText上粘貼\ copy \ cut
- 19. 如何自動格式化粘貼到富文本框中的文本?
- 20. 如何在SWT文本中禁用粘貼操作
- 21. 如何禁用在搜索行中包裝粘貼文本
- 22. 如何禁用在文本框中粘貼特殊字符
- 23. 如何在PowerShell中粘貼輸入時不自動執行
- 24. 如何在android中啓用editText剪切/複製/粘貼功能?
- 25. 當粘貼文本時輸入文本移動 - Webkit
- 26. 如何在swift中自動粘貼文本?
- 27. 如何在emacs中禁用x粘貼
- 28. Vi:如何自動插入空格
- 29. 如何在Android Studio中不使用格式化粘貼格式化文本?
- 30. 粘貼後如何跳轉到插入文本的另一端
我無法確認此行爲。也許這是你的鍵盤?你有沒有嘗試過Android鍵盤? – 2013-04-25 17:13:54
你可以只刪除它們的代碼喜歡這裏: http://stackoverflow.com/questions/3349121/how-do-i-use-inputfilter-to-limit-characters-in-an-edittext- in-android – user1875797 2013-04-25 17:20:30