kotlin Android Studio中的对话框(遵循课程)

nc1teljy  于 2023-03-19  发布在  Kotlin
关注(0)|答案(1)|浏览(185)

我目前正在学习Kotlin课程,在这里,Dialog使用了一个xml文件,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageButton
    android:id = "@+id/ib_small_brush"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/small"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/ib_medium_brush"
    />

<ImageButton
    android:id = "@+id/ib_medium_brush"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/medium"
    app:layout_constraintTop_toBottomOf="@id/ib_small_brush"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toTopOf="@+id/ib_large_brush"
    />

<ImageButton
    android:id = "@+id/ib_large_brush"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/medium"
    app:layout_constraintTop_toBottomOf="@id/ib_medium_brush"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"
    />
</androidx.constraintlayout.widget.ConstraintLayout>

我遇到了这样的问题:当我开始输入android:idandroid:src等属性时,它们不会自动弹出,这与其他属性不同。
这是我的MainActivity

class MainActivity : AppCompatActivity() {

private var drawingView: DrawingView? = null

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setContentView(R.layout.activity_main)
    drawingView = findViewById(R.id.drawing_view)
    drawingView?.setSizeForBrush(20.toFloat())
}

private fun showBrushSizeChooserDialog(){
    val brushDialog = Dialog(this)
    brushDialog.setContentView(R.layout.dialog_brush_size)
    brushDialog.setTitle("Brush size: ")
    val smallButton = brushDialog.ib
}}

在本课程中,ImageViews通过brushDialog.ib_small_brush访问,然后用于在其上设置onClickListeners等。然而,这对我来说是不可能的。这种方法是否过时(使用片段代替),或者我的代码或Android Studio是否存在根本性错误?
干杯。

lyr7nygr

lyr7nygr1#

这应该是brushDialog.ib_小刷子
你还没有展示你使用的课程示例,所以我来猜一猜。
brushDialog.ib_small_brush看起来像是你在尝试使用ViewBinding,但是brushDialogDialog,所以这当然是行不通的。
你需要为这个布局文件创建一个ViewBinding,类似于:

private fun showBrushSizeChooserDialog() {
    val brushDialog = Dialog(this)
    
    // Create binding from the dialog layout file
    val binding = NameOfLayoutFileBinding.inflate(layoutInflater)
    
    // Set the inflated view as the content
    brushDialog.setContentView(binding.root)
    
    brushDialog.setTitle("Brush size: ")
    
    // Access views via the binding
    val smallButton = brushDialog.ib_small_brush
}

有关详细信息,请查看the documentation on ViewBinding

相关问题