我正在Android Studio中创建一个非常基本的待办应用程序。当用户添加新任务时,我希望他从房屋/工作/活动中选择一种类别。为此,我在主布局中添加了PopupWindow
,并在表示类别的3 imageButtons
中添加了内容。房屋默认为选中状态,当用户要选择其他房屋时,houseButton将其图像更改为“空”,其他按钮将其图像更改为“选定”
根据我在互联网上发现的内容,我尝试在view.invalidate()
上使用view.postInvalidate()
和imageButtons
,但没有任何反应。我也尝试过覆盖runOnUiThread {}
中的整个块,但它也无济于事。
不起作用的监听器:
fun swapActivity(view: View) {
houseButton.setImageResource(R.drawable.houseempty)
workButton.setImageResource(R.drawable.workempty)
activityButton.setImageResource(R.drawable.activityempty)
when (view) {
houseButton -> {
houseButton.setImageResource(R.drawable.house)
selectedActivity = "house"
}
workButton -> {
workButton.setImageResource(R.drawable.work)
selectedActivity = "work"
}
aActivity -> {
activityButton.setImageResource(R.drawable.activity)
selectedActivity = "activ"
}
}
}
主布局中显示PopupWindow的按钮:
var shown = false
button.setOnClickListener{
if(!shown) {
shown = true
val inflater: LayoutInflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.new_todo, null)
val popupWindow = PopupWindow(
view,
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
)
//imageView inside popup that hides it
val buttonPopup = view.findViewById<ImageView>(R.id.imageCancel)
buttonPopup.setOnClickListener {
popupWindow.dismiss()
shown = false
}
TransitionManager.beginDelayedTransition(mainLayout)
popupWindow.showAtLocation(
mainLayout, // Location to display popup window
Gravity.BOTTOM,
0,
0
)
}
}
XML弹出文件(new_todo
):
<?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:background="#ffffff">">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/cancel"
android:id="@+id/imageCancel"/>
<ImageButton
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/activity"
android:id="@+id/activityButton"
android:onClick="swapActivity"/>
<ImageButton
android:background="@null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="@drawable/workempty"
android:id="@+id/workButton"
android:onClick="swapActivity"/>
<ImageButton
android:layout_width="wrap_content" android:background="@null"
android:layout_height="wrap_content"
app:srcCompat="@drawable/house"
android:id="@+id/houseButton"
android:onClick="swapActivity"/>
</LinearLayout>
编辑:已解决。我删除了整个swapActivity
函数,并在主按钮侦听器中添加了以下几行:
val houseButton1 = view.findViewById<ImageButton>(R.id.houseButton)
val workButton1 = view.findViewById<ImageButton>(R.id.workButton)
val activityButton1 = view.findViewById<ImageButton>(R.id.activityButton)
houseButton1.setOnClickListener {
houseButton1.setImageResource(R.drawable.house)
workButton1.setImageResource(R.drawable.workempty)
activityButton1.setImageResource(R.drawable.activityempty)
selectedActivity = "house"
}
//repeat for all buttons