android实现猜扑克牌小游戏(改进:每次只可以选择一张)
程序员文章站
2024-02-26 17:19:01
...
android实现猜扑克牌小游戏
-
项目目录
-
实现效果
-
XML代码如下
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/cai"
android:textColor="#008"
android:id="@+id/result"
android:textSize="30dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="40dp"
android:orientation="horizontal">
<ImageView
android:id="@+id/img1"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_weight="1"
android:onClick="click"
android:layout_marginRight="20dp"
android:src="@drawable/p"/>
<ImageView
android:id="@+id/img2"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_weight="1"
android:onClick="click"
android:layout_marginRight="20dp"
android:src="@drawable/p"/>
<ImageView
android:id="@+id/img3"
android:layout_width="100dp"
android:layout_height="150dp"
android:layout_weight="1"
android:onClick="click"
android:src="@drawable/p"/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="再来一次"
android:id="@+id/btn"
android:background="#f66"
android:layout_gravity="center"
android:textSize="30dp"/>
</LinearLayout>
- java代码
package com.example.acer.pkp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ImageView img1;
private ImageView img2;
private ImageView img3;
private Button btn;
private TextView result; //定义按钮,图片视图,文本框
@Override
protected void onCreate(Bundle savedInstanceState) { //重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1=(ImageView)findViewById(R.id.img1); //获取图片资源
img2=(ImageView)findViewById(R.id.img2);
img3=(ImageView)findViewById(R.id.img3); //获取按钮和文本框
btn= (Button)findViewById(R.id.btn);
result= (TextView) findViewById(R.id.result);
btn.setOnClickListener(new View.OnClickListener() { //为按钮设置监听事件
@Override //重写onClick方法
public void onClick(View v) {
img1.setImageResource(R.drawable.p); //运用setImageResource设置初始的图片
img2.setImageResource(R.drawable.p);
img3.setImageResource(R.drawable.p);
TextView result= (TextView) findViewById(R.id.result); //定义文本框初始显示的内容
result.setText("猜猜K是哪一张"); //调用setText方法获取文本框内容
}
});
}
public void click(View view){ //运用click方法
switch (view.getId()){
case R.id.img1:
img1.setImageResource(R.drawable.j);
result.setText("选择错误!");
break;
}
switch (view.getId()){
case R.id.img2:
img2.setImageResource(R.drawable.k);
result.setText("选择正确!");
break;
}
switch (view.getId()){
case R.id.img3:
img3.setImageResource(R.drawable.q);
result.setText("选择错误!");
break;
}
}
}
-
思考
代码优化:每次只可以选择一张
优化方法:设置布尔变量 -
实现效果
package com.example.acer.pkp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private ImageView img1;
private ImageView img2;
private ImageView img3;
private Button btn;
private TextView result; //定义按钮,图片视图,文本框
private boolean a = true; //设置布尔变量
@Override
protected void onCreate(Bundle savedInstanceState) { //重写onCreate方法
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
img1 = (ImageView) findViewById(R.id.img1); //获取图片资源
img2 = (ImageView) findViewById(R.id.img2);
img3 = (ImageView) findViewById(R.id.img3); //获取按钮和文本框
btn = (Button) findViewById(R.id.btn);
result = (TextView) findViewById(R.id.result);
btn.setOnClickListener(new View.OnClickListener() { //为按钮设置监听事件
@Override //重写onClick方法
public void onClick(View v) {
img1.setImageResource(R.drawable.p); //运用setImageResource设置初始的图片
img2.setImageResource(R.drawable.p);
img3.setImageResource(R.drawable.p);
TextView result = (TextView) findViewById(R.id.result); //定义文本框初始显示的内容
result.setText("猜猜K是哪一张"); //调用setText方法获取文本框内容
a = true; //执行一次之后是false,重新设置布尔变量,每次初始化都是true
}
});
}
public void click(View view) { //运用click方法
if (a) {
switch (view.getId()) {
case R.id.img1:
img1.setImageResource(R.drawable.j);
result.setText("选择错误!");
a=false; //跳出循环下面的就不可执行
break;
case R.id.img2:
img2.setImageResource(R.drawable.k);
result.setText("选择正确!");
a=false;
break;
case R.id.img3:
img3.setImageResource(R.drawable.q);
result.setText("选择错误!");
a=false;
break;
}
}
}
}