<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                資源文件都可以在百度云盤中找到: 鏈接:https://pan.baidu.com/s/1EegFNEv6X2yqi6cJ9iStJQ 提取碼:f6zp ___ 為了實現下拉框,這里首先定義主布局文件: ~~~ <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <EditText android:hint="請輸入內容" android:id="@+id/editText" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingRight="42dp" /> <ImageView android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/down_arrow" android:layout_alignRight="@id/editText" android:layout_alignTop="@id/editText" > </ImageView> </RelativeLayout> ~~~ 對應效果: ![](https://img.kancloud.cn/f2/0f/f20f61bda149e1aea220cd98052a715b_196x60.png) 然后就需要為其添加動態點擊事件,以及彈出窗口,對于彈出窗口這里使用popupwindow。也就是在下拉圖片點擊的時候,就彈出菜單: ~~~ /** * 下拉框 */ class MainActivity : AppCompatActivity() { val editText by lazy {findViewById<EditText>(R.id.editText)} val imageview by lazy {findViewById<ImageView>(R.id.imageview)} var listView: ListView? = null var popupWindow: PopupWindow? = null val datas = listOf("123", "測試", "123", "測試", "123", "測試") override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) listView = ListView(this) listView?.adapter = MyListViewAdapter(this, R.layout.item, datas.toMutableList()) imageview.setOnClickListener(object : View.OnClickListener{ override fun onClick(v: View?) { // 創建PopupWindow if(popupWindow == null){ popupWindow = PopupWindow() popupWindow?.width = editText.width popupWindow?.height = dp2pix(200f) // 指定了布局為動態創建的ListView popupWindow?.contentView = listView popupWindow?.isFocusable = true // 設置焦點,系統就會把各種事件交給popup來處理 } popupWindow?.showAsDropDown(editText, 0, 0) } }) } fun dp2pix(value: Float): Int{ return (resources.displayMetrics.density * value).toInt() } } ~~~ 由于我們在上面代碼中指定了popupWindow的布局為一個ListView,這里我們就需要定義一個適配器對象: ~~~ class MyListViewAdapter(context: Context, resource: Int, datas: MutableList<String>): BaseAdapter(){ var mCtx: Context = context var mDatas: MutableList<String> = datas var mResId: Int = resource override fun getCount(): Int { return mDatas.size } override fun getItem(position: Int): Any { return position } override fun getItemId(position: Int): Long { return position.toLong() } override fun getView(position: Int, convertView: View?, parent: ViewGroup?): View { var view: MyViewHolder? = null var tempView: View? = null if(convertView == null){ tempView = View.inflate(mCtx, mResId, null) as View view = MyViewHolder() view.title = tempView.findViewById<TextView>(R.id.textView) view.delete = tempView.findViewById<ImageView>(R.id.delete) } else { tempView = convertView view = tempView.getTag() as MyViewHolder } // 設置標題 val msg = mDatas.get(position) view.title?.text = msg // 設置刪除按鈕監聽 view.delete?.setOnClickListener(object : View.OnClickListener{ override fun onClick(v: View?) { mDatas.remove(msg) // 通知數據發生改變 notifyDataSetChanged() } }) // 緩存實例化的view tempView.setTag(view) return tempView } inner class MyViewHolder{ var delete: ImageView? = null var title: TextView? = null } } ~~~ 當然這里按道理應該使用RecyclerView,因為是鞏固和復習一下kotlin這個語言,所以選擇了使用ListView。對應的布局item.xml: ~~~ <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:orientation="horizontal" android:layout_height="match_parent"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="請輸入內容" android:layout_weight="4" android:layout_gravity="center" android:textColor="@color/black" android:textSize="20sp"/> <ImageView android:id="@+id/delete" android:layout_weight="1" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content" app:srcCompat="@drawable/delete" /> </LinearLayout> ~~~ 最終效果就是點擊下拉框可展開,然后彈出一個下拉框: ![](https://img.kancloud.cn/7c/bf/7cbf0459216c07531cca603e0bb75e91_210x236.png) 點擊X可以進行該項的移除,當然也可以為每項添加一個事件,然后為EditText賦值: ~~~ listView?.setOnItemClickListener(object :AdapterView.OnItemClickListener{ override fun onItemClick( parent: AdapterView<*>?, view: View?, position: Int, id: Long ) { editText.setText(datas[position]) // 關閉窗口 popupWindow?.apply { if(isShowing){ popupWindow?.dismiss() popupWindow = null } } } }) ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看