Skip to content

Commit

Permalink
帖子内容支持复制
Browse files Browse the repository at this point in the history
  • Loading branch information
freedom10086 committed Aug 1, 2018
1 parent f75adde commit d77f9c8
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 2 deletions.
3 changes: 3 additions & 0 deletions app/src/main/java/me/yluo/ruisiapp/activity/PostActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,9 @@ public void onListItemClick(View v, final int position) {
public boolean onMenuItemClick(MenuItem menuItem) {
// 更多按钮里面的选项被点击
switch (menuItem.getItemId()) {
case R.id.tv_copy:
adapter.copyItem(clickPosition);
break;
case R.id.tv_edit:
Intent i = new Intent(this, EditActivity.class);
i.putExtra("PID", datas.get(clickPosition).pid);
Expand Down
14 changes: 14 additions & 0 deletions app/src/main/java/me/yluo/ruisiapp/adapter/PostAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

import com.squareup.picasso.Picasso;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import java.util.List;

import me.yluo.ruisiapp.R;
Expand Down Expand Up @@ -181,6 +184,17 @@ void setData(int position) {
}
}

public void copyItem(int position) {
String user = datalist.get(position).username;
Document document = Jsoup.parse(datalist.get(position).content);
String content = document.text();
ClipboardManager cm = (ClipboardManager) activity.getSystemService(Context.CLIPBOARD_SERVICE);
if (cm != null) {
cm.setPrimaryClip(ClipData.newPlainText(null, content));
Toast.makeText(activity, "已复制" + user + "的评论", Toast.LENGTH_SHORT).show();
}
}

//header
private class HeaderViewHolder extends BaseViewHolder {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package me.yluo.ruisiapp.widget.htmlview;

import android.content.Context;
import android.support.annotation.Nullable;
import android.text.Selection;
import android.text.Spannable;
import android.util.AttributeSet;
import android.widget.TextView;


/**
* 修复 选择报错
* https://stackoverflow.com/questions/28689871/android-default-textselector-to-copy-text-not-working
*/
public class SelectFixTextView extends TextView {
public SelectFixTextView(Context context) {
super(context);
}

public SelectFixTextView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}

public SelectFixTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}

public SelectFixTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
}

@Override
protected void onSelectionChanged(int selStart, int selEnd) {
if (selStart == -1 || selEnd == -1) {
// @hack : https://code.google.com/p/android/issues/detail?id=137509
CharSequence text = getText();
if (text instanceof Spannable) {
Selection.setSelection((Spannable) text, 0, 0);
}
} else {
super.onSelectionChanged(selStart, selEnd);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private void setSpan(int start, Object span) {

private void setSpan(int start, int end, Object span) {
if (end <= start || end > spannedBuilder.length()) return;
spannedBuilder.setSpan(span, start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
spannedBuilder.setSpan(span , start, end, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}


Expand Down
3 changes: 2 additions & 1 deletion app/src/main/res/layout/item_content.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@
android:layout_marginTop="8dp"
android:background="@color/colorDivider" />

<TextView
<me.yluo.ruisiapp.widget.htmlview.SelectFixTextView
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
Expand All @@ -109,6 +109,7 @@
android:paddingStart="12dp"
android:paddingTop="8dp"
android:textColor="?android:textColorPrimary"
android:textIsSelectable="true"
android:textSize="16sp"
tools:text="在点(110,10)处绘制一个宽和高均为50像素的正方形边框 context.strokeRect(110,10,50,50); //在点(30,10)处绘制一个宽和高均为50像素的正方形边框 " />

Expand Down

0 comments on commit d77f9c8

Please sign in to comment.