微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

RecyclerView中的EditText与Kotlin不起作用

如何解决RecyclerView中的EditText与Kotlin不起作用

我是应用开发的新手,并且recyclerview的项目中有EditText问题。

用户单击editText时,没有任何显示。我在EditText上尝试使用itemClickListener,但单击确实有效,但没有编辑文本。

这是我的代码

活动:

class MyIngredientListActivity : AppCompatActivity() {

val ingerdientOfListArray = database.getAllIngredientOfList()
val adapter = IngredientOflistadapter(ingerdientOfListArray)

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.ingredient_list_recyclerview)

    findViewById<Button>
    (R.id.ingredient_list_activity_search_button).setonClickListener {

        val intent = Intent(this,IngredientSearchableActivity::class.java)
        startActivity(intent)
    }

    val recyclerView = findViewById<RecyclerView> 
    (R.id.ingredientoflist_recyclerview)
    recyclerView.layoutManager = linearlayoutmanager(this)
    recyclerView.adapter = adapter

}


override fun onResume() {
    super.onResume()

    adapter.updateIngredientList()
}

适配器:

class IngredientOflistadapter(val ingredientOfListdisplay: 
ArrayList<IngredientOfList>
): RecyclerView.Adapter<IngredientOflistadapter.ViewHolder>() {



class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {


    val cardView = itemView.findViewById<CardView> 
(R.id.ingredientoflist_cardview_name)
    val name = itemView.findViewById<TextView>(R.id.ingredientoflist_name)
    val checkBox = itemView.findViewById<CheckBox> 
(R.id.ingredientoflist_checkBox)
    val editText = itemView.findViewById<EditText> 
(R.id.ingredientoflist_quantity)
    val unitmeasure = itemView.findViewById<TextView> 
(R.id.ingredientoflist_unitmeasure)

    }



override fun onCreateViewHolder(parent: ViewGroup,viewType: Int): ViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val viewItem = inflater.inflate(R.layout.ingredient_list_item,parent,false)

    return ViewHolder(viewItem)
}



override fun onBindViewHolder(holder: ViewHolder,position: Int) {
    val ingredientOfList = ingredientOfListdisplay[position]        

    val ingredientFromId = database.readIngredientById(ingredientOfList).first()
    val unitMeasureId = database.readUnitMeasureById(ingredientFromId).first()     


    holder.cardView?.tag = position
    holder.name.text = ingredientFromId.name
    holder.unitmeasure.text = unitMeasureId.symbol

    holder.editText?.tag = position
    holder.editText.setText(0)

}

override fun getItemCount(): Int {
    return ingredientOfListdisplay.size
}


fun updateIngredientList(ingredientOfListArray: ArrayList<IngredientOfList> = database.getAllIngredientOfList()) {

    ingredientOfListdisplay.clear()
    ingredientOfListdisplay.addAll(ingredientOfListArray)
    notifyDataSetChanged()
}

}

RelativeLayout在这里

在这里查看卡片

    android:id="@+id/ingredientoflist_cardview_name"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:cardUseCompatPadding="true"
    android:focusable="false"
    android:clickable="false"
    android:foreground="?android:attr/selectableItemBackground">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#C3FFFFFF">

        <TextView
            android:id="@+id/ingredientoflist_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="20dp"
            android:layout_marginLeft="20dp"
            android:textColor="#403E3E"
            android:textSize="17sp"
            android:textStyle="bold"
            tools:text="ingredient"/>

        <EditText
            android:id="@+id/ingredientoflist_quantity"
            android:layout_width="50sp"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="250dp"
            android:layout_marginLeft="250dp"
            android:layout_marginBottom="8dp"
            android:clickable="true"
            android:focusable="true"
            android:inputType="number"
            android:imeOptions="actionDone"
            android:saveEnabled="true"
            android:hint="00"
            android:textColor="#403E3E"
            android:textSize="17sp"
            android:textStyle="bold"
            android:gravity="center_horizontal"
            android:background="@drawable/abc_vector_test" />

        <TextView
            android:id="@+id/ingredientoflist_unitmeasure"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:gravity="center_vertical"
            android:layout_marginStart="305dp"
            android:layout_marginLeft="305dp"
            android:hint="cl"
            android:textColor="#403E3E"
            android:textSize="17sp"
            android:textStyle="bold"/>

        <CheckBox
            android:id="@+id/ingredientoflist_checkBox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerVertical="true"
            android:layout_gravity="center_vertical"
            android:layout_marginStart="350dp"
            android:layout_marginLeft="350dp"
            android:gravity="center_vertical"/>

    </RelativeLayout>

</CardView>

RecyclerView布局:

<RelativeLayout

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"
style="@style/NoActionBar"
android:background="#000000">


<ImageView
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_below="@+id/ingredient_search_activity_search_button"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:paddingBottom="60dp"
    app:srcCompat="@drawable/baseline_no_food_white_48"
    app:tint="#40FFFFFF" />

<Button
    android:id="@+id/ingredient_search_activity_search_button"
    android:layout_width="match_parent"
    android:layout_height="48dp"
    android:background="@android:color/white"
    android:drawableStart="@android:drawable/ic_search_category_default"
    android:drawableLeft="@android:drawable/ic_search_category_default"
    android:gravity="start|center_vertical"
    android:text="@string/my_searchables_ingredients"
    android:textAlignment="textStart"
    android:focusable="false"
    android:clickable="true"/>


<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/ingredientoflist_recyclerview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#C3FFFFFF"
    app:queryHint="your ingredients"
    android:layout_below="@id/ingredient_search_activity_search_button"
    android:clickable="false"
    android:focusable="false"/>

</RelativeLayout>

谢谢,祝你有美好的一天!

解决方法

我想添加

holder.editText.setOnClickListener(Object : View.OnClickListener {
    override fun OnClick(v: View?) {
        v.requestFocus()
    }
}

应该做到这一点。我不太擅长Kotlin,所以那里可能存在语法错误。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。