問題描述
我想在我的 EditText 中實(shí)現(xiàn)剪切、復(fù)制、粘貼功能,我嘗試使用以下代碼:
I want to implement the cut,copy, paste functionality in my EditText,i tried with the following code :
ClipMan = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
editbox1.setText(ClipMan.getText());
用于粘貼文本,但它會(huì)將整個(gè)文本粘貼到另一個(gè)編輯框中..我想復(fù)制選定的文本并將該文本粘貼到同一個(gè)編輯框中,就像普通記事本一樣..
for paste the text,but it paste the whole text in another Editbox..I want to copy the selected text and paste that text in the same Editbox just like the normal notepad works..
非常感謝任何建議...謝謝!!
Any suggestions are greatly appreciated... Thanks !!
推薦答案
我終于可以復(fù)制粘貼到我的應(yīng)用程序中了..現(xiàn)在我可以使用此代碼僅粘貼選定的文本:
Finally i am able to copy ,paste in my application..now i can paste only selected text by using this code :
Editable s1;
EditText editbox2;
復(fù)制選定的文本:
if(editbox2.getSelectionEnd() > editbox2.getSelectionStart())
{
s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionStart(), editbox2.getSelectionEnd());
}else
{
s1 = (Editable) editbox2.getText().subSequence(editbox2.getSelectionEnd(), editbox2.getSelectionStart());
}
粘貼選定的文本:
editbox2.getText().replace(Math.min(editbox2.getSelectionStart(),editbox2.getSelectionEnd()), Math.max(editbox2.getSelectionStart(), editbox2.getSelectionEnd()),s1, 0, s1.length());
這篇關(guān)于在android中剪切、復(fù)制、粘貼的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!