Android问题动态创建多个AutoCompleteTextViews

发布于 2025-02-11 02:03:52 字数 8506 浏览 1 评论 0原文

我想动态创建输入视图,每个视图都包含一个自动填页。每个autoCompleteTextView显示一个下拉列表。

我已经定义了一个项目视图(item_new_product.xml),该视图被夸大并添加到viewContainer(activity_main.xml)。

每个autoCompleteTextView都定义了XML中的下拉clatdownancher,这是其父textInputlayout的视图ID。这是下拉列表应显示的地方。

对于第一个项目,下拉菜单出现在正确的位置(请参见 screenshot 1 )。但是,每个额外的下拉列表都与第一个AutoCompleteTextView的TextInputlayout相关联,因为它们都具有相同的下拉clatsdownander ID(请参阅)。

是否有一种干净的方法可以在正确的位置显示有多个动态创建的自动填充版视图的下拉列表?我没有发现与这个特定问题有关的任何内容。

activation_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:backgroundTint="@android:color/holo_blue_dark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <com.google.android.material.button.MaterialButton
            android:id="@+id/addButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_gravity="start"
            android:background="@color/holo_blue_bright"
            android:elevation="5dp"
            android:text="+" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/removeButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_gravity="end"
            android:background="@color/holo_blue_bright"
            android:elevation="5dp"
            android:text="-" />

    </com.google.android.material.card.MaterialCardView>

    <LinearLayout
        android:id="@+id/viewContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:animateLayoutChanges="true"
        android:background="@color/holo_blue_dark"
        android:orientation="vertical"
        android:padding="8dp"
        app:layout_constraintTop_toBottomOf="@id/cardView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

item_new_product.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".5" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/productText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="Product"
        app:hintTextColor="@color/holo_blue_dark"
        app:helperText=" "
        app:boxBackgroundColor="@color/holo_blue_bright"
        app:boxStrokeColor="@color/holo_blue_dark"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/productEditText"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:textSize="12sp"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:textCursorDrawable="@null" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/productTypeLayout"
        style="@style/DropdownLayoutStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="Type"
        app:helperText=" "
        android:inputType="none"
        app:boxBackgroundColor="@color/holo_blue_bright"
        app:boxStrokeColor="@color/holo_blue_dark"
        app:endIconTint="@color/holo_blue_dark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent">

        <AutoCompleteTextView
            android:id="@+id/productTypeAutoCompleteTv"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:dropDownAnchor="@id/productTypeLayout"
            android:dropDownHeight="120dp"
            android:inputType="none"
            android:maxLines="1"
            android:textSize="12sp"
            android:singleLine="true"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"/>

    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

mainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    
    private var IDs = mutableListOf<Int>()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        binding = ActivityMainBinding.inflate(layoutInflater)
    
        binding.addButton.setOnClickListener {
            Timber.d("add clicked")
            addView()
        }
        binding.removeButton.setOnClickListener {
            Timber.d("remove clicked")
            removeView()
        }
      
        setContentView(binding.root)
    }
    
    private fun addView() {
        Timber.d("addView")
        val rootView = binding.viewContainer
        val inflater = LayoutInflater.from(this)
        val child: View = inflater.inflate(R.layout.item_new_product, rootView, false)
        // generate new view id and remember it for later removal
        val childId = View.generateViewId()
        child.id = childId
        IDs.add(childId)
    
        val items: List<String> = listOf("Beverages", "Cocktails", "Pasta", "Pizza", "Salads")
        val adapter = ArrayAdapter(this, R.layout.item_product_dropdown, items)
    
        val productAutoComplete =
            child.findViewById(R.id.productTypeAutoCompleteTv) as AutoCompleteTextView
        productAutoComplete.setAdapter(adapter)
        rootView.addView(child)
    }

    private fun removeView() {
        if (IDs.size == 0) {
            return
        }
        val view: View? = findViewById<View>(IDs.last())
        IDs.removeLast()
        if (view != null) {
            (view.parent as ViewGroup).removeView(view)
        }
    }

styles.xml

<resources>
    <style name="DropdownLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
        <item name="endIconTint">@color/holo_blue_dark</item>
        <item name="hintTextColor">@color/holo_blue_dark</item>
    </style>
</resources>

item_product_dropdown.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:background="@color/holo_blue_bright"
    android:maxLines="1"
    android:padding="16dp"
    android:textColor="@color/white" />

I want to dynamically create input views, which each contain an AutoCompleteTextView. Each AutoCompleteTextView shows a Dropdown list.

I have defined an item view (item_new_product.xml), which gets inflated and added to the viewContainer (activity_main.xml).

Each AutoCompleteTextView has defined a dropDownAnchor in xml, which is the view ID of its parent TextInputLayout. This is where the dropdown list should show up.

For the first item the Dropdown appears in the right place (see Screenshot 1). But each additional Dropdown is associated to the first AutoCompleteTextView's TextInputLayout, because they all have the same dropDownAnchor id(see Screenshot 2).

Is there a clean way to show the Dropdown lists at the right place having multiple dynamically created AutoCompleteTextViews? I have not found anything related to this specific issue.

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout 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"
    tools:context=".MainActivity">

    <com.google.android.material.card.MaterialCardView
        android:id="@+id/cardView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:backgroundTint="@android:color/holo_blue_dark"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">


        <com.google.android.material.button.MaterialButton
            android:id="@+id/addButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_gravity="start"
            android:background="@color/holo_blue_bright"
            android:elevation="5dp"
            android:text="+" />

        <com.google.android.material.button.MaterialButton
            android:id="@+id/removeButton"
            android:layout_width="52dp"
            android:layout_height="52dp"
            android:layout_gravity="end"
            android:background="@color/holo_blue_bright"
            android:elevation="5dp"
            android:text="-" />

    </com.google.android.material.card.MaterialCardView>

    <LinearLayout
        android:id="@+id/viewContainer"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_margin="16dp"
        android:animateLayoutChanges="true"
        android:background="@color/holo_blue_dark"
        android:orientation="vertical"
        android:padding="8dp"
        app:layout_constraintTop_toBottomOf="@id/cardView"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        >
    </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

item_new_product.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <androidx.constraintlayout.widget.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical"
        app:layout_constraintGuide_percent=".5" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/productText"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="Product"
        app:hintTextColor="@color/holo_blue_dark"
        app:helperText=" "
        app:boxBackgroundColor="@color/holo_blue_bright"
        app:boxStrokeColor="@color/holo_blue_dark"
        app:layout_constraintEnd_toStartOf="@+id/guideline"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/productEditText"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:textSize="12sp"
            android:imeOptions="actionNext"
            android:inputType="textPersonName"
            android:textCursorDrawable="@null" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/productTypeLayout"
        style="@style/DropdownLayoutStyle"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_margin="16dp"
        android:hint="Type"
        app:helperText=" "
        android:inputType="none"
        app:boxBackgroundColor="@color/holo_blue_bright"
        app:boxStrokeColor="@color/holo_blue_dark"
        app:endIconTint="@color/holo_blue_dark"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintStart_toEndOf="@id/guideline"
        app:layout_constraintEnd_toEndOf="parent">

        <AutoCompleteTextView
            android:id="@+id/productTypeAutoCompleteTv"
            android:layout_width="match_parent"
            android:layout_height="52dp"
            android:dropDownAnchor="@id/productTypeLayout"
            android:dropDownHeight="120dp"
            android:inputType="none"
            android:maxLines="1"
            android:textSize="12sp"
            android:singleLine="true"
            android:paddingStart="16dp"
            android:paddingEnd="16dp"/>

    </com.google.android.material.textfield.TextInputLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private lateinit var binding: ActivityMainBinding
    
    private var IDs = mutableListOf<Int>()
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
    
        binding = ActivityMainBinding.inflate(layoutInflater)
    
        binding.addButton.setOnClickListener {
            Timber.d("add clicked")
            addView()
        }
        binding.removeButton.setOnClickListener {
            Timber.d("remove clicked")
            removeView()
        }
      
        setContentView(binding.root)
    }
    
    private fun addView() {
        Timber.d("addView")
        val rootView = binding.viewContainer
        val inflater = LayoutInflater.from(this)
        val child: View = inflater.inflate(R.layout.item_new_product, rootView, false)
        // generate new view id and remember it for later removal
        val childId = View.generateViewId()
        child.id = childId
        IDs.add(childId)
    
        val items: List<String> = listOf("Beverages", "Cocktails", "Pasta", "Pizza", "Salads")
        val adapter = ArrayAdapter(this, R.layout.item_product_dropdown, items)
    
        val productAutoComplete =
            child.findViewById(R.id.productTypeAutoCompleteTv) as AutoCompleteTextView
        productAutoComplete.setAdapter(adapter)
        rootView.addView(child)
    }

    private fun removeView() {
        if (IDs.size == 0) {
            return
        }
        val view: View? = findViewById<View>(IDs.last())
        IDs.removeLast()
        if (view != null) {
            (view.parent as ViewGroup).removeView(view)
        }
    }

styles.xml

<resources>
    <style name="DropdownLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox.ExposedDropdownMenu">
        <item name="endIconTint">@color/holo_blue_dark</item>
        <item name="hintTextColor">@color/holo_blue_dark</item>
    </style>
</resources>

item_product_dropdown.xml

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="52dp"
    android:background="@color/holo_blue_bright"
    android:maxLines="1"
    android:padding="16dp"
    android:textColor="@color/white" />

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文