【问题标题】:How to expand a dialogue in android on demand (e.g. a button click)如何按需扩展android中的对话(例如单击按钮)
【发布时间】:2021-05-23 18:55:13
【问题描述】:

我是 android 和 kotlin 的新手,正在开发我的第一个应用程序,这将是一个库应用程序。在应用程序中,我想与一些适合一个屏幕的基本视图(例如创建数据库条目)进行对话,但我希望对话可以按需扩展,例如在单击按钮时应存储其他数据。

我需要的与我的安卓智能手机的内置对话框“添加新联系人”非常相似。首先,对话框不可滚动并在一个屏幕上显示最重要的字段,但通过按下“添加更多字段”按钮,对话框变为可滚动(纵向两个屏幕的高度)并显示其他字段以保存联系人数据。

我希望我能够描述我在寻找什么。

我的第一个想法是使用一个片段(简单的约束布局),当用户按下“添加更多字段”按钮并从第一个片段中检索已经输入的信息时,它将被另一个片段(ScrollView)替换,但我想知道这是否是最佳做法。

能否请你给我一些基本的建议,让我知道这个对话是如何被编程为现实的?

非常感谢!

【问题讨论】:

    标签: android kotlin dialog user-experience


    【解决方案1】:

    创建一个扩展 Dialog 类的类。

    class AddToPlaylist(
    context: Context
    ) : Dialog(context) {
    private var playlists: List<PlaylistEntity>? = null
    
    fun setDialogPlaylists(_playlists: List<PlaylistEntity>) {
        playlists = _playlists
    }
    
    
    var recyclerView: RecyclerView? = null
    var adapter: PlaylistDialogAdapter? = null
    
    var selectedPlaylistId = -1
    
    
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.add_to_playlist_dialog)
    
    
        recyclerView = findViewById(R.id.recyclerViewPlaylists)
    
        adapter = PlaylistDialogAdapter(context)
        recyclerView?.adapter = adapter
        recyclerView?.layoutManager = LinearLayoutManager(context)
    
        adapter?.playlistClickCallback = fun(id: Int) {
            selectedPlaylistId = id
            dismiss()
        }
    
        adapter?.setPlayLists(playlists!!)
    }
    

    在此对话框中,我使用了回收站视图,因为我将显示播放列表列表。 接下来,我的对话框有一个 xml 文件。

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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"
    android:padding="12dp"
    android:background="@color/backgroundColor"
    android:orientation="vertical">
    
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/choose_playlist"
        android:textColor="@color/textPrimary"
        android:textSize="@dimen/small_text" />
    
    
    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/recyclerViewPlaylists"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="5dp"
        android:scrollbarStyle="outsideOverlay"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
        tools:listitem="@layout/single_playlist_item" />
    

    现在我的对话框类已经准备好了,我只需要创建一个需要使用它的实例并调用 show 方法。

    addToPlaylistDialog = AddToPlaylist(activity as Context)
    addToPlaylistDialog.show()
    

    还可以使用解除监听器在对话框关闭后编写您想要的代码。

    addToPlaylistDialog.setOnDismissListener {
            val playlistId: Int = addToPlaylistDialog.selectedPlaylistId
            Log.i("PLAYLISTSONGS", "Dismiss Listener called with playlistId=$playlistId")
        }
    

    这应该足以满足您的需要。

    【讨论】:

    • 非常感谢您的回答,但不幸的是这不是我想要的。请参阅下面的文字了解更多信息
    • @LIronLeaves 我有一个自定义对话框,我想在其中使用 RecyclerView。你可以在那里有任何你想要的布局。例如,您可以简单地将您的 xml 代码从下面的文本复制到此自定义对话框 xml。并在对话框类中,在按钮上设置点击监听器以实现您的目标。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-13
    • 1970-01-01
    • 2012-07-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多