在Android中,是否可以在editext中进行这种分隔?

问题描述 投票:-1回答:1

我想在逗号(,)之间创建这种分隔?就像StackOverflow标签editext field

android android-edittext tags
1个回答
0
投票

尝试一下:

在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);
    }
}

在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>

在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"/>

在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" />

请参见此处以获取更多详细信息:https://github.com/qiugang/EditTag/blob/master/sample/src/main/res/layout/view_sample_tag.xml

© www.soinside.com 2019 - 2024. All rights reserved.