如何使用 android 中的上下文菜单将 textview 值从列表视图项复制到剪贴板?
How to copy textview value from listview item to clipboard using context menu in android?
我想使用列表视图项中的按钮单击将文本视图值从列表视图项复制到剪贴板。我可以从 listview onitemclick 上的 listview 项目获取 textview 值。但我想使用上下文菜单获取此值。那么,我怎样才能从列表视图项中获取值。我在 Holder 中有 textview class.Here 是我的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
LayoutInflater li = getActivity().getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
if (v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.single_row, parent, false);
holder.favourit_style = (ImageView) v.findViewById(R.id.favourit_style);
holder.share_style = (ImageView) v.findViewById(R.id.share_style);
holder.textView = (TextView) v.findViewById(R.id.text_Style);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
holder.textView.setText("" + s.toString() + "");
}
});
registerForContextMenu(holder.share_style);
return v;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case COPY:
ViewHolder h = new ViewHolder();
String s = h.textView.getText().toString();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(s);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", s);
clipboard.setPrimaryClip(clip);
}
break;
return super.onContextItemSelected(item);
}
首先你必须得到要复制的字符串内容。我认为你可以管理自己。现在您可以使用此代码将该内容复制到剪贴板:
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Clip", textToCopy);
clipboard.setPrimaryClip(clip);
希望能帮到你!
我想使用列表视图项中的按钮单击将文本视图值从列表视图项复制到剪贴板。我可以从 listview onitemclick 上的 listview 项目获取 textview 值。但我想使用上下文菜单获取此值。那么,我怎样才能从列表视图项中获取值。我在 Holder 中有 textview class.Here 是我的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
final ViewHolder holder;
LayoutInflater li = getActivity().getLayoutInflater();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(getActivity().LAYOUT_INFLATER_SERVICE);
if (v == null) {
holder = new ViewHolder();
v = inflater.inflate(R.layout.single_row, parent, false);
holder.favourit_style = (ImageView) v.findViewById(R.id.favourit_style);
holder.share_style = (ImageView) v.findViewById(R.id.share_style);
holder.textView = (TextView) v.findViewById(R.id.text_Style);
v.setTag(holder);
} else {
holder = (ViewHolder) v.getTag();
}
editText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
holder.textView.setText("" + s.toString() + "");
}
});
registerForContextMenu(holder.share_style);
return v;
}
public boolean onContextItemSelected(MenuItem item) {
switch (item.getItemId()) {
case COPY:
ViewHolder h = new ViewHolder();
String s = h.textView.getText().toString();
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.HONEYCOMB) {
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
clipboard.setText(s);
} else {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getActivity().getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Copied Text", s);
clipboard.setPrimaryClip(clip);
}
break;
return super.onContextItemSelected(item);
}
首先你必须得到要复制的字符串内容。我认为你可以管理自己。现在您可以使用此代码将该内容复制到剪贴板:
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) mActivity.getSystemService(Context.CLIPBOARD_SERVICE);
android.content.ClipData clip = android.content.ClipData.newPlainText("Clip", textToCopy);
clipboard.setPrimaryClip(clip);
希望能帮到你!