You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

实现AlertDialog内添加按钮上方的MultiAutoCompleteTextView可滚动及自定义弹窗

Custom AlertDialog with Scrollable MultiAutoCompleteTextView Above Add Button

Got it, let's build this custom AlertDialog exactly how you want it. We'll break this down into easy-to-follow steps—no fancy tricks, just solid implementation.

Step 1: Create the Custom Dialog Layout

First, make an XML layout file for your dialog (let's name it dialog_multi_auto_complete.xml). We'll use a vertical LinearLayout to stack the scrollable MultiAutoCompleteTextView on top of the add button. The ScrollView ensures the text view can scroll if there are too many entries, and we'll set weights to make sure the text view takes most of the dialog space while the button stays fixed at the bottom.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

    <!-- Scrollable MultiAutoCompleteTextView -->
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:fillViewport="true">

        <MultiAutoCompleteTextView
            android:id="@+id/multi_auto_complete_tv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Enter items (separate with commas)"
            android:inputType="textCapWords"
            android:minLines="3" />
    </ScrollView>

    <!-- Add Button -->
    <Button
        android:id="@+id/add_btn"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:text="Add Items" />

</LinearLayout>

Step 2: Set Up the MultiAutoCompleteTextView in Your Code

Next, in your Activity or Fragment, you'll inflate this layout, configure the MultiAutoCompleteTextView with an adapter and tokenizer (to handle multiple entries), then build the AlertDialog.

Kotlin Implementation:

// Inside your Activity/Fragment
fun showCustomDialog() {
    // Inflate the custom layout
    val dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_multi_auto_complete, null)
    
    // Get references to the views
    val multiAutoCompleteTv = dialogView.findViewById<MultiAutoCompleteTextView>(R.id.multi_auto_complete_tv)
    val addBtn = dialogView.findViewById<Button>(R.id.add_btn)

    // Sample data for the auto-complete suggestions
    val items = arrayOf("Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape", "Honeydew")
    val adapter = ArrayAdapter(this, android.R.layout.simple_dropdown_item_1line, items)
    
    // Set up the adapter and tokenizer (comma-separated entries)
    multiAutoCompleteTv.setAdapter(adapter)
    multiAutoCompleteTv.setTokenizer(MultiAutoCompleteTextView.CommaTokenizer())

    // Build the AlertDialog
    val alertDialog = AlertDialog.Builder(this)
        .setTitle("Add Items")
        .setView(dialogView)
        .setNegativeButton("Cancel") { dialog, _ ->
            dialog.dismiss()
        }
        .create()

    // Handle add button click
    addBtn.setOnClickListener {
        val enteredText = multiAutoCompleteTv.text.toString()
        // Process the entered items (split, trim, filter empty entries)
        val itemsList = enteredText.split(",").map { it.trim() }.filter { it.isNotEmpty() }
        Toast.makeText(this, "Added: ${itemsList.joinToString(", ")}", Toast.LENGTH_SHORT).show()
        alertDialog.dismiss()
    }

    alertDialog.show()
}

Java Implementation:

// Inside your Activity/Fragment
public void showCustomDialog() {
    // Inflate the custom layout
    View dialogView = LayoutInflater.from(this).inflate(R.layout.dialog_multi_auto_complete, null);
    
    // Get references to the views
    MultiAutoCompleteTextView multiAutoCompleteTv = dialogView.findViewById(R.id.multi_auto_complete_tv);
    Button addBtn = dialogView.findViewById(R.id.add_btn);

    // Sample data for the auto-complete suggestions
    String[] items = {"Apple", "Banana", "Cherry", "Date", "Elderberry", "Fig", "Grape", "Honeydew"};
    ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_dropdown_item_1line, items);
    
    // Set up the adapter and tokenizer (comma-separated entries)
    multiAutoCompleteTv.setAdapter(adapter);
    multiAutoCompleteTv.setTokenizer(new MultiAutoCompleteTextView.CommaTokenizer());

    // Build the AlertDialog
    AlertDialog alertDialog = new AlertDialog.Builder(this)
        .setTitle("Add Items")
        .setView(dialogView)
        .setNegativeButton("Cancel", (dialog, which) -> dialog.dismiss())
        .create();

    // Handle add button click
    addBtn.setOnClickListener(v -> {
        String enteredText = multiAutoCompleteTv.getText().toString();
        // Process the entered items
        List<String> itemsList = Arrays.stream(enteredText.split(","))
                .map(String::trim)
                .filter(s -> !s.isEmpty())
                .collect(Collectors.toList());
        Toast.makeText(this, "Added: " + String.join(", ", itemsList), Toast.LENGTH_SHORT).show();
        alertDialog.dismiss();
    });

    alertDialog.show();
}

Key Notes to Remember

  • Scroll Behavior: The ScrollView wraps the MultiAutoCompleteTextView so if the user enters more items than fit in the dialog, they can scroll to see all of them. The layout_weight="1" ensures the text view takes up all available space above the button.
  • Tokenizer: Using CommaTokenizer() lets users separate entries with commas—you can swap this out for a custom tokenizer if you want a different separator (like spaces or semicolons).
  • Dialog Resizing: The LinearLayout uses wrap_content for height, so the dialog will shrink/grow based on content. If you want a fixed max height, add android:maxHeight="300dp" to the ScrollView.

内容的提问来源于stack exchange,提问作者Arnab Sarkar

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.15 06:56:41