需要从文本视图中选择文本到所需范围并进行复制。我尝试在文本视图上使用 onClick 侦听器,并且android:textIsSelectable="true"
在 xml 中添加
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textView = (TextView)findViewById(R.id.textview1);
textView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
textView.setKeyListener(null);
textView.setFocusable(true);
String stringYouExtracted = textView.getText().toString();
int startIndex = textView.getSelectionStart();
int endIndex = textView.getSelectionEnd();
stringYouExtracted = stringYouExtracted.substring(startIndex, endIndex);
ClipboardManager clipboard = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
clipboard.setText(stringYouExtracted);
}
});
Toast.makeText(this, "Text clipped!", Toast.LENGTH_SHORT)
.show();
}
}
但它没有选择文本,也没有得到要复制的文本