问题
I want to create this kind of separation between commas(,)? like StackOverflow Tags
回答1:
Try this:
In MainActivity.java
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SwitchCompat;
import android.widget.CompoundButton;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
import me.originqiu.library.EditTag;
public class MainActivity extends AppCompatActivity
implements SwitchCompat.OnCheckedChangeListener {
private EditTag editTagView;
private SwitchCompat statusSwitchView;
private List<String> tagStrings = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTagView = (EditTag) findViewById(R.id.edit_tag_view);
statusSwitchView = (SwitchCompat) findViewById(R.id.status_switch);
statusSwitchView.setOnCheckedChangeListener(this);
for (int i = 0; i < 10; i++) {
tagStrings.add("test" + i);
}
//Set tag add callback before set tag list
editTagView.setTagAddCallBack(new EditTag.TagAddCallback() {
@Override
public boolean onTagAdd(String tagValue) {
if ("test1".equals(tagValue)) {
return false;
} else {
return true;
}
}
});
editTagView.setTagDeletedCallback(new EditTag.TagDeletedCallback() {
@Override
public void onTagDelete(String deletedTagValue) {
Toast.makeText(MainActivity.this, deletedTagValue, Toast.LENGTH_SHORT).show();
}
});
editTagView.setTagList(tagStrings);
editTagView.addTag("hello world!");
editTagView.removeTag("test3");
}
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
editTagView.setEditable(isChecked);
}
}
In activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="me.originqiu.edittag.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<me.originqiu.library.EditTag
android:id="@+id/edit_tag_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:input_layout="@layout/view_sample_input_tag"
app:tag_layout="@layout/view_sample_tag" />
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginTop="4dp"
android:background="@color/colorAccent" />
</LinearLayout>
<android.support.v7.widget.SwitchCompat
android:id="@+id/status_switch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="true"
android:padding="16dp"
android:text="open editable status" />
</LinearLayout>
In view_sample_input_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<me.originqiu.library.MEditText
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@null"
android:minWidth="56dp"
android:drawablePadding="2dp"
android:paddingBottom="4dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="4dp"
android:singleLine="true"
android:maxEms="30"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:hint="Add Tag "
android:imeOptions="actionDone"
android:textColor="@color/dark_clr"
android:textSize="14sp"/>
In view_sample_tag.xml
<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:background="@drawable/bg_sample_tag"
android:gravity="center"
android:paddingBottom="2dp"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="2dp"
android:textColor="#fff"
android:textSize="14sp"
android:textStyle="bold" />
See here for more details: https://github.com/qiugang/EditTag/blob/master/sample/src/main/res/layout/view_sample_tag.xml
来源:https://stackoverflow.com/questions/61094990/in-android-is-it-possible-to-make-text-tag-separation-in-editext-like-stack-ove