I am writing a simple war card game in android studio using Java. The app seems to work however when you click deal the cards disapear. To clarify, when I run the app I can see the back of the cards, but when I click deal, the cards simply disappear, but the score keeps counting. I thought about putting the images in an array but I am unsure how to call them in the onclick. Can someone give me some guidance? Thank you!!
Main Activity:
public class MainActivity extends AppCompatActivity { ImageView iv_card_left, iv_card_right; TextView tv_score_left, tv_score_right; Button b_deal; Random r; int leftScore = 0, rightScore = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); iv_card_left = (ImageView) findViewById(R.id.iv_card_left); iv_card_right = (ImageView) findViewById(R.id.iv_card_right); tv_score_left = (TextView) findViewById(R.id.tv_score_left); tv_score_right = (TextView) findViewById(R.id.tv_score_right); b_deal = (Button) findViewById(R.id.b_deal); r = new Random(); b_deal.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //generate two card numbers, add points, and display them int leftCard = r.nextInt(13) + 2; //this is for cards 2-14 int rightCard = r.nextInt(13) + 2; //this is for cards 2-14 //show card images int leftImage = getResources().getIdentifier("card" + leftCard, "drawable", getPackageName()); iv_card_left.setImageResource(leftImage); int rightImage = getResources().getIdentifier("card" + rightCard, "drawable", getPackageName()); iv_card_right.setImageResource(rightImage); //compare cards if(leftCard > rightCard) { leftScore++; tv_score_left.setText(String.valueOf(leftScore)); } else if(leftCard < rightCard) { rightScore++; tv_score_right.setText(String.valueOf(rightScore)); } else { Toast.makeText(MainActivity.this, "WAR", Toast.LENGTH_SHORT).show(); } } }); }
activity_main:
<RelativeLayout 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:background="@drawable/jackpot" android:layout_height="match_parent" tools:context=".MainActivity"> <LinearLayout android:id="@+id/lay_cards" android:layout_width="match_parent" android:layout_alignParentTop="true" android:orientation="horizontal" android:layout_height="wrap_content"> <ImageView android:id="@+id/iv_card_left" android:layout_width="wrap_content" android:layout_height="253dp" android:layout_weight="1" android:scaleType="centerInside" android:src="@drawable/red_back" /> <ImageView android:id="@+id/iv_card_right" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="1" android:scaleType="centerInside" android:src="@drawable/red_back" /> </LinearLayout> <Button android:id="@+id/b_deal" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/lay_cards" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:text="Deal" /> <LinearLayout android:id="@+id/lay_points" android:layout_width="match_parent" android:layout_alignParentBottom="true" android:orientation="horizontal" android:layout_height="wrap_content"> <TextView android:id="@+id/tv_score_left" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="0" android:textColor="#FFFFFF" android:textSize="24sp" /> <TextView android:id="@+id/tv_score_right" android:layout_width="0dp" android:layout_weight="1" android:textSize="32sp" android:text="0" android:textColor="#FFFFFF" android:gravity="center" android:layout_height="wrap_content"/> </LinearLayout>
https://stackoverflow.com/questions/67352153/imageview-not-showing-up-after-onclick May 02, 2021 at 09:04AM
没有评论:
发表评论