mirror of
https://github.com/leanote/leanote-android.git
synced 2025-10-17 16:07:48 +00:00
add auto complete feature for entering tag
This commit is contained in:
@@ -210,4 +210,11 @@ public class AppDataBase {
|
||||
.async()
|
||||
.execute();
|
||||
}
|
||||
|
||||
public static List<Tag> getAllTags(String userId) {
|
||||
return SQLite.select()
|
||||
.from(Tag.class)
|
||||
.where(Tag_Table.userId.eq(userId))
|
||||
.queryList();
|
||||
}
|
||||
}
|
||||
|
@@ -4,26 +4,35 @@ package org.houxg.leamonax.ui.edit;
|
||||
import android.app.AlertDialog;
|
||||
import android.app.Fragment;
|
||||
import android.content.DialogInterface;
|
||||
import android.graphics.Color;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.text.Editable;
|
||||
import android.text.Spanned;
|
||||
import android.text.TextUtils;
|
||||
import android.text.TextWatcher;
|
||||
import android.util.Log;
|
||||
import android.view.LayoutInflater;
|
||||
import android.view.View;
|
||||
import android.view.ViewGroup;
|
||||
import android.widget.EditText;
|
||||
import android.widget.ArrayAdapter;
|
||||
import android.widget.MultiAutoCompleteTextView;
|
||||
import android.widget.Switch;
|
||||
import android.widget.TextView;
|
||||
|
||||
import org.houxg.leamonax.R;
|
||||
import org.houxg.leamonax.database.AppDataBase;
|
||||
import org.houxg.leamonax.model.Notebook;
|
||||
import org.houxg.leamonax.model.Tag;
|
||||
import org.houxg.leamonax.service.AccountService;
|
||||
import org.houxg.leamonax.utils.CollectionUtils;
|
||||
import org.houxg.leamonax.utils.DisplayUtils;
|
||||
import org.houxg.leamonax.widget.RoundedRectBackgroundSpan;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import butterknife.BindView;
|
||||
import butterknife.ButterKnife;
|
||||
@@ -32,11 +41,12 @@ import butterknife.OnClick;
|
||||
public class SettingFragment extends Fragment {
|
||||
|
||||
private static final String TAG = "SettingFragment";
|
||||
private static final Pattern TAG_PATTERN = Pattern.compile("[^,\\s]+\\s*[^,]*");
|
||||
|
||||
@BindView(R.id.sw_public)
|
||||
Switch mPublicSw;
|
||||
@BindView(R.id.et_tags)
|
||||
EditText mTagEt;
|
||||
MultiAutoCompleteTextView mTagEt;
|
||||
@BindView(R.id.tv_notebook)
|
||||
TextView mNotebookTv;
|
||||
|
||||
@@ -55,6 +65,42 @@ public class SettingFragment extends Fragment {
|
||||
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
|
||||
View view = inflater.inflate(R.layout.fragment_settings, container, false);
|
||||
ButterKnife.bind(this, view);
|
||||
List<Tag> tags = AppDataBase.getAllTags(AccountService.getCurrent().getUserId());
|
||||
String[] tagTexts = new String[tags.size()];
|
||||
int i = 0;
|
||||
for (Tag tag : tags) {
|
||||
tagTexts[i] = tag.getText();
|
||||
i++;
|
||||
}
|
||||
|
||||
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(container.getContext(), android.R.layout.simple_dropdown_item_1line, tagTexts);
|
||||
mTagEt.setAdapter(arrayAdapter);
|
||||
mTagEt.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());
|
||||
|
||||
final int tagPadding = DisplayUtils.dp2px(container.getContext(), 2);
|
||||
mTagEt.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) {
|
||||
RoundedRectBackgroundSpan[] spans = s.getSpans(0, s.length(), RoundedRectBackgroundSpan.class);
|
||||
for (RoundedRectBackgroundSpan span : spans) {
|
||||
s.removeSpan(span);
|
||||
}
|
||||
Matcher matcher = TAG_PATTERN.matcher(s.toString());
|
||||
while (matcher.find()) {
|
||||
s.setSpan(new RoundedRectBackgroundSpan(Color.LTGRAY, 10, tagPadding), matcher.start(), matcher.end(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
|
||||
}
|
||||
}
|
||||
});
|
||||
return view;
|
||||
}
|
||||
|
||||
@@ -68,7 +114,6 @@ public class SettingFragment extends Fragment {
|
||||
}
|
||||
}
|
||||
|
||||
//TODO:input List<Tag>
|
||||
public void setTags(List<String> tagData) {
|
||||
String tags = "";
|
||||
if (CollectionUtils.isNotEmpty(tagData)) {
|
||||
@@ -108,13 +153,20 @@ public class SettingFragment extends Fragment {
|
||||
return mPublicSw.isChecked();
|
||||
}
|
||||
|
||||
//TODO:output List<Tag>
|
||||
public List<String> getTags() {
|
||||
String text = mTagEt.getText().toString();
|
||||
if (TextUtils.isEmpty(text)) {
|
||||
return new ArrayList<>();
|
||||
} else {
|
||||
return Arrays.asList(text.split(","));
|
||||
String[] tagTexts = text.split(",");
|
||||
List<String> tags = new ArrayList<>();
|
||||
for (String tagText : tagTexts) {
|
||||
tagText = tagText.trim();
|
||||
if (!TextUtils.isEmpty(tagText)) {
|
||||
tags.add(tagText);
|
||||
}
|
||||
}
|
||||
return tags;
|
||||
}
|
||||
}
|
||||
|
||||
|
@@ -0,0 +1,45 @@
|
||||
package org.houxg.leamonax.widget;
|
||||
|
||||
|
||||
import android.graphics.Canvas;
|
||||
import android.graphics.Paint;
|
||||
import android.graphics.RectF;
|
||||
import android.text.style.ReplacementSpan;
|
||||
|
||||
import org.houxg.leamonax.Leamonax;
|
||||
import org.houxg.leamonax.utils.DisplayUtils;
|
||||
|
||||
public class RoundedRectBackgroundSpan extends ReplacementSpan {
|
||||
|
||||
private int mColor;
|
||||
private float mRadius;
|
||||
private int padding = 10;
|
||||
private RectF mTempRect = new RectF();
|
||||
|
||||
public RoundedRectBackgroundSpan(int color, float radius) {
|
||||
this(color, radius, 0);
|
||||
}
|
||||
|
||||
public RoundedRectBackgroundSpan(int mColor, float mRadius, int padding) {
|
||||
this.mColor = mColor;
|
||||
this.mRadius = mRadius;
|
||||
this.padding = padding;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getSize(Paint paint, CharSequence text, int start, int end, Paint.FontMetricsInt fm) {
|
||||
return Math.round(paint.measureText(text, start, end) + padding * 2);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void draw(Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, Paint paint) {
|
||||
mTempRect.set(x, top, x + paint.measureText(text, start, end) + padding * 2, bottom);
|
||||
float offsetY = DisplayUtils.dp2px(Leamonax.getContext(), 2);
|
||||
mTempRect.offset(0, -offsetY);
|
||||
int textColor = paint.getColor();
|
||||
paint.setColor(mColor);
|
||||
canvas.drawRoundRect(mTempRect, mRadius, mRadius, paint);
|
||||
paint.setColor(textColor);
|
||||
canvas.drawText(text, start, end, x + padding, y - offsetY, paint);
|
||||
}
|
||||
}
|
@@ -71,7 +71,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:text="@string/tags" />
|
||||
|
||||
<EditText
|
||||
<android.support.v7.widget.AppCompatMultiAutoCompleteTextView
|
||||
android:id="@+id/et_tags"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
|
Reference in New Issue
Block a user