java Android onClickListener没有注册点击(我想)
我想写一个非常基本的纸牌游戏。我已经设置了按钮,允许玩家与卡片交互。我在每个按钮上都设置了侦听器,但目前我只使用其中一个按钮来解决这个问题。我已经设置了onClick()方法,并在按钮上设置了侦听器,但是当我单击按钮时,什么都没有发生。我将尝试提取相关代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.game);
playableImage = (ImageView) this.findViewById(R.id.playableStack);
deckButton = (Button) this.findViewById(R.id.deckButton);
deckButton.setOnClickListener(this);
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.deckButton:
System.out.println(playableCards);
dealCardsToPlayableStack();
System.out.println(playableCards);
playableImage.setImageResource(getCardImageId(playableCards.peekTopCard()));
playableImage.invalidate();
break;
default:
break;
}
}
根据要求,下面是活动的xml布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
安卓:layout_height="match_parent" 安卓:layout_width="match_parent"
安卓:background="@drawable/background" 安卓:baselineAligned="true"
安卓:orientation="vertical" 安卓:weightSum="1">
<RelativeLayout 安卓:id="@+id/relativeLayout1"
安卓:layout_width="match_parent" 安卓:layout_height="match_parent">
<LinearLayout 安卓:id="@+id/linearLayout1"
安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content"
安卓:layout_alignParentTop="true" 安卓:layout_alignParentLeft="true">
<ImageView 安卓:src="@drawable/cardback"
安卓:layout_width="50dp" 安卓:layout_height="62.5dp"
安卓:padding="2.5dp" 安卓:id="@+id/deckImage" />
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_width="50dp" 安卓:layout_height="62.5dp"
安卓:padding="2.5dp" 安卓:id="@+id/playableStack" />
<ImageView 安卓:src="@drawable/cardback"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/spadesStack"></ImageView>
<ImageView 安卓:src="@drawable/cardback"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/diamondsStack"></ImageView>
<ImageView 安卓:src="@drawable/cardback"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/clubsStack"></ImageView>
<ImageView 安卓:src="@drawable/cardback"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/heartsStack"></ImageView>
</LinearLayout>
<LinearLayout 安卓:id="@+id/linearLayout2"
安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content"
安卓:layout_below="@+id/linearLayout1" 安卓:layout_alignLeft="@+id/linearLayout1">
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack1"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack2"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack3"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack4"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack5"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack6"
安卓:layout_weight="1"></ImageView>
<ImageView 安卓:src="@drawable/cardtemplate"
安卓:layout_height="62.5dp" 安卓:layout_width="50dp"
安卓:padding="2.5dp" 安卓:id="@+id/stack7"
安卓:layout_weight="1"></ImageView>
</LinearLayout>
<LinearLayout 安卓:id="@+id/linearLayout3"
安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content"
安卓:layout_below="@+id/linearLayout2" 安卓:layout_alignLeft="@+id/linearLayout2"
安卓:orientation="horizontal">
<Button 安卓:layout_height="wrap_content"
安卓:layout_width="wrap_content" 安卓:text="@string/deck"
安卓:id="@+id/deckButton"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/playable"
安卓:layout_width="wrap_content" 安卓:id="@+id/playableButton"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/spades"
安卓:layout_width="wrap_content" 安卓:id="@+id/spadesButton"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/diamonds"
安卓:layout_width="wrap_content" 安卓:id="@+id/diamondsButton"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/clubs"
安卓:layout_width="wrap_content" 安卓:id="@+id/clubsButton"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/hearts"
安卓:layout_width="wrap_content" 安卓:id="@+id/heartsButton"></Button>
</LinearLayout>
<LinearLayout 安卓:id="@+id/linearLayout4"
安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content"
安卓:layout_below="@+id/linearLayout3" 安卓:layout_alignLeft="@+id/linearLayout3">
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack1"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack1Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack2"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack2Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack3"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack3Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack4"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack4Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack5"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack5Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack6"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack6Button"></Button>
<Button 安卓:layout_height="wrap_content" 安卓:text="@string/stack7"
安卓:layout_weight="1" 安卓:layout_width="0dp" 安卓:id="@+id/stack7Button"></Button>
</LinearLayout>
<LinearLayout 安卓:id="@+id/LinearLayout5"
安卓:layout_height="wrap_content" 安卓:layout_width="wrap_content"
安卓:layout_below="@+id/linearLayout4" 安卓:layout_alignLeft="@+id/linearLayout4">
<TextView 安卓:layout_height="wrap_content" 安卓:id="@+id/textView1"
安卓:text="@string/numCards" 安卓:layout_width="wrap_content">
</TextView>
<EditText 安卓:layout_height="wrap_content"
安卓:inputType="number" 安卓:id="@+id/editText1"
安卓:layout_width="40dp" 安卓:text="@string/default_number_of_cards">
<requestFocus></requestFocus>
</EditText>
</LinearLayout>
</RelativeLayout>
# 1 楼答案
您的活动是否正在实现OnClickListener
# 2 楼答案
我认为问题在于您试图使用以下方法进行调试:
在android上,您应该使用:
然后,您可以通过在Eclipse中打开logcat视图来查看此信息。 读一读这本书也许会有帮助。 http://developer.android.com/reference/android/util/Log.html