我有一个按钮,当我点击它时,弹出窗口显示其中包含4个按钮。 根据用户在弹出窗口中单击的按钮,弹出式触发按钮将自行更新。但是我面临的问题是无法在下面的代码中使用getId()。无法getId()在嵌套setOnClickListener
public void pressCell(View view) {
final ImageButton popup = (ImageButton) findViewById(view.getId());
LayoutInflater layoutInflater = (LayoutInflater) getBaseContext()
.getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
popupWindow.setFocusable(true);
popupView.findViewById(popupView.getId()).setOnClickListener(
new OnClickListener() {
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
popup.setImageResource(R.drawable.num1);
case R.id.button2:
popup.setImageResource(R.drawable.num2);
case R.id.button3:
popup.setImageResource(R.drawable.num3);
case R.id.button4:
popup.setImageResource(R.drawable.num4);
}
popupWindow.dismiss();
}
});
popupWindow.showAsDropDown(findViewById(view.getId()), -120, -2
* findViewById(view.getId()).getHeight());
}
pressCell是触发按钮的onclick功能:
<ImageButton
android:id="@+id/cell1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="pressCell"
android:src="@drawable/num3" />
谁能告诉我什么是这里的问题?谢谢。
使用popup.setOnClickListener为多个视图设置onClickLinstener,并在切换案例中添加中断 –
使用setOnClickListener和使用android:onclick是否有区别?由于我有很多触发按钮,所以我真的不想在每个按钮上设置Listener。 – hakunami