Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để tạo Focusable EditText bên trong ListView trên Android bằng Kotlin?

Ví dụ này trình bày cách tạo Focusable EditText bên trong ListView trên Android bằng Kotlin.

Bước 1 - Tạo một dự án mới trong Android Studio, vào Tệp? Dự án mới và điền tất cả các chi tiết bắt buộc để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

Ví dụ

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:padding="4dp"
   tools:context=".MainActivity">
<ListView
   android:id="@+id/listView"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:descendantFocusability="beforeDescendants" />
</LinearLayout>

Bước 3 - Thêm mã sau vào src / MainActivity.kt

import androidx.appcompat.app.AppCompatActivity
import android.annotation.SuppressLint
import android.app.LauncherActivity
import android.content.Context
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import android.widget.EditText
import android.widget.ListView
import java.util.ArrayList
class MainActivity : AppCompatActivity() {
   private lateinit var listView: ListView
   private lateinit var myAdapter: MyAdapter
   override fun onCreate(savedInstanceState: Bundle?) {
      super.onCreate(savedInstanceState)
      setContentView(R.layout.activity_main)
      title = "KotlinApp"
      listView = findViewById(R.id.listView)
      listView.itemsCanFocus = true
      myAdapter = MyAdapter()
      listView.adapter = myAdapter
   }
   private inner class MyAdapter internal constructor() : BaseAdapter() {
      private var layoutInflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as
      LayoutInflater
      internal var myItems = ArrayList<LauncherActivity.ListItem>()
      lateinit var context: Context
      init {
         for (i in 0..6) {
            val listItem = LauncherActivity.ListItem()
            listItem.className = "Caption$i"
            myItems.add(listItem)
         }
         notifyDataSetChanged()
      }
      override fun getItem(position: Int): Any {
         return position
      }
      override fun getItemId(position: Int): Long {
         return position.toLong()
      }
      override fun getCount(): Int {
         return myItems.size
      }
      @SuppressLint("InflateParams")
      override fun getView(position: Int, convertView: View?, parent: ViewGroup): View? {
         var convertView: View? = convertView
         val holder: ViewHolder
         if (convertView == null) {
            holder = ViewHolder()
            convertView = layoutInflater.inflate(R.layout.list, null)
            holder.caption = convertView.findViewById(R.id.ItemCaption)
            convertView.setTag(holder)
         } else {
            holder = convertView.tag as ViewHolder
         }
         holder.caption.setText(myItems.get(position).className)
         holder.caption.id = position
         holder.caption.onFocusChangeListener = View.OnFocusChangeListener { v, hasFocus ->
            if (!hasFocus) {
               val position = v.id
               val caption = v as EditText
               myItems[position].className = caption.text.toString()
            }
         }
         return convertView
      }
   }
   internal inner class ViewHolder {
      lateinit var caption: EditText
   }
}

Bước 4 - Tạo tệp tài nguyên bố cục (list.xml) và thêm mã sau

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:paddingStart="4dp">
<EditText
   android:id="@+id/ItemCaption"
   android:layout_width="fill_parent"
   android:layout_height="wrap_content"
   android:layout_marginStart="2dip"
   android:singleLine="true" />
</LinearLayout>

Bước 5 - Thêm mã sau vào androidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="com.example.q11">
   <application
      android:allowBackup="true"
      android:icon="@mipmap/ic_launcher"
      android:label="@string/app_name"
      android:roundIcon="@mipmap/ic_launcher_round"
      android:supportsRtl="true"
      android:theme="@style/AppTheme">
      <activity android:name=".MainActivity">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Run Làm thế nào để tạo Focusable EditText bên trong ListView trên Android bằng Kotlin? từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn

Làm thế nào để tạo Focusable EditText bên trong ListView trên Android bằng Kotlin?