簡體   English   中英

帶芯片的 Android AutoCompleteTextView

[英]Android AutoCompleteTextView with chips

我不確定我對這個 UI 功能使用了正確的詞,但我附上了我希望在我的應用程序中實現的目標的快照。

它被 Go SMS 使用,其中用戶在編輯文本中鍵入聯系人,在用戶從完成下拉列表中選擇聯系人后,聯系人將插入到編輯文本中,如附加圖像所示。編輯文本仍然打開接受進一步的輸入。

對於我的應用程序,我想在用戶輸入逗號后立即進行分組和插入,就像 StackOverflow 的標簽輸入工作一樣(但我相信我可以單獨處理。)我的問題是這是什么類型的視圖或者如何修改 EditText 使其表現得像這樣?

帶有分組值的 EditText

謝謝。

Google 的官方芯片庫(在 Gmail、電子郵件、日歷、消息中使用)位於https://android.googlesource.com/platform/frameworks/opt/chips/

可以在https://code.google.com/p/platform-features-talk-io-2013/source/browse/src/com/example/iotalk/ChipsActivity.java找到如何使用它的簡單示例

還有兩個芯片庫。

  • 安卓芯片 與其他一些不同的是,這個更新后的視覺效果反映了新發布的“材料設計”標准

    在此處輸入圖片說明

  • 令牌自動完成 它是一個 Android Gmail 風格的令牌自動完成文本字段和過濾器。

    在此處輸入圖片說明
    在此處輸入圖片說明

我認為我們可以使用 Recycler 視圖和 Edit text 或 Auto complete text view 構建我們自己的芯片視圖。 所以我們可以很容易地定制它。

1.在Drawable中創建了一個標簽形狀,比如tags_layout.xml

<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#cfcfcf">
</solid>
<corners android:radius="20dp">
</corners>

2. 為回收者視圖創建布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_margin="4dp"
android:gravity="center"
android:background="@drawable/tags_layout">
<TextView
    android:id="@+id/tag_textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:maxLines="1"
    android:maxLength="25"
    android:ellipsize="end"
    android:padding="2dp"
    android:text="Hello"/>
<ImageView
    android:id="@+id/tag_closeBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_close"/>

3. 在我們的活動布局中,我們在編輯文本上方實施小部件回收器視圖以保留標簽並編輯文本或自動完成文本視圖以輸入標簽。

   <android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">
<android.support.v7.widget.RecyclerView
    android:id="@+id/tagsRecyclerView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
        <EditText
            android:id="@+id/tagsEditText"
            android:inputType="text"
            android:imeOptions="actionDone"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v4.widget.NestedScrollView>

4.為回收者視圖創建了一個模型java類

        public class RecyclerModel {
    private String tagText;

    public RecyclerModel(String tagText){
        this.tagText = tagText;
    }
    public String getTagText() {
        return tagText;
    }

    public void setTagText(String tagText) {
        this.tagText = tagText;
    }
}

5. 用於回收者視圖的適配器類

    public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.RecyclerAdapterHolder> {
    Context context;
    ArrayList<RecyclerModel> model = new ArrayList<>(  );

    public RecyclerAdapter(Context context,ArrayList<RecyclerModel> model){
        this.context = context;
        this.model = model;
    }

    @NonNull
    @Override
    public RecyclerAdapterHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.recycler_layout, parent, false);

        return new RecyclerAdapterHolder(itemView);
    }

    @Override
    public void onBindViewHolder(final RecyclerAdapterHolder holder, final int position) {
        final RecyclerModel mod = model.get( position );
        holder.tagTextView.setText( mod.getTagText() );
        //remove tag on click x button
        holder.tagImageView.setOnClickListener( new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                model.remove( position );
                notifyDataSetChanged();
            }
        } );
    }

    @Override
    public int getItemCount() {
        return model.size();
    }

    public static class RecyclerAdapterHolder extends RecyclerView.ViewHolder {
        public TextView tagTextView;
        public ImageView tagImageView;
        public RecyclerAdapterHolder(View itemView) {
            super( itemView );
            tagTextView = itemView.findViewById( R.id.tag_textView );
            tagImageView = itemView.findViewById( R.id.tag_closeBtn );
        }
    }
}

6. 最后,在我們的活動中,在編輯文本中輸入數據時將數據添加到回收器

public class MainActivity extends AppCompatActivity {
    RecyclerView tagsRecyclerView;
    EditText tagsEditText;
    ArrayList<RecyclerModel> recyclerModels = new ArrayList<>(  );

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate( savedInstanceState );
        setContentView( R.layout.activity_main );
        tagsRecyclerView = findViewById( R.id.tagsRecyclerView );
        tagsEditText = findViewById( R.id.tagsEditText );
        tagsEditText.setOnEditorActionListener( new TextView.OnEditorActionListener() {
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    Toast.makeText( MainActivity.this,"hello",Toast.LENGTH_SHORT );
                    String str = tagsEditText.getText().toString();
                    if(str != null && !str.equals( "" )) {
                        getUpdateData( str );
                        tagsEditText.setText( null );
                        RecyclerAdapter adapter = new RecyclerAdapter( MainActivity.this, recyclerModels );
                        FlexboxLayoutManager gridLayout = new FlexboxLayoutManager( MainActivity.this );
                        tagsRecyclerView.setLayoutManager( gridLayout );
                        tagsRecyclerView.setAdapter( adapter );
                    }
                }
                return false;
            }
        } );
    }

    private void getUpdateData(String str) {
        RecyclerModel model = new RecyclerModel( str );
        recyclerModels.add( model );
    }
}

7. 清單文件應包含 gradles

implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.google.android:flexbox:1.0.0'

有一個新的 Android Material Chips

從 android 支持庫版本 28.0.0 開始,谷歌添加了Chip視圖,允許我們在布局中顯示芯片視圖。 關於芯片的設計和文檔

和簡單的例子:

<android.support.design.chip.ChipGroup
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    app:chipSpacing="8dp">

    <android.support.design.chip.Chip
        android:id="@+id/some_chip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android Chip Group"
        app:chipIcon="@drawable/ic_android"
        app:closeIconVisible="true" />

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Android"
        app:chipIcon="@drawable/ic_android" />

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Chip"
        app:chipIcon="@drawable/ic_android" />

    <android.support.design.chip.Chip
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Group"
        app:chipIcon="@drawable/ic_android" />
</android.support.design.chip.ChipGroup>

在此處輸入圖片說明

改變了很多。 我們有新的圖書館。 我會推薦這個圖書館 這是非常簡單和強大的。

只需添加此依賴項

implementation "com.hootsuite.android:nachos:1.1.1"

而這個觀點

<com.hootsuite.nachos.NachoTextView
    android:id="@+id/nacho_text_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:chipHorizontalSpacing="2dp"
    app:chipBackground="@color/chip_background"
    app:chipTextColor="@color/cheddar"
    app:chipTextSize="16dp"
    app:chipHeight="30dp"
    app:chipVerticalSpacing="3dp"/>

和這個適配器:

val suggestions = arrayOf("Tortilla Chips", "Melted Cheese", "Salsa", "Guacamole", "Mexico", "Jalapeno")
val adapter = ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, suggestions)
nachoTextView.setAdapter(adapter)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM