package training.android.com;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
//创建人:xinzhouc
//创建时间:2010-3-25
public class paygamesCard extends Activity {
// 按钮
private Button btnStart;
// 图片控件
private ImageView imageview1;
private ImageView imageview2;
private ImageView imageview3;
// 初始化图片数组;
/*
* R.drawable.p01 红心A R.drawable.p02 黑桃2 R.drawable.p03 梅花3
*/
private static int[] imagearray = new int[] { R.drawable.p01,
R.drawable.p02, R.drawable.p03 };
// 显示文本控件
private TextView txtview;
// 是否开始
private int isStart = 0;
// 当前选中的图片编号
private int Current_Select_ID = 0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 初始化找到各控件
btnStart = (Button) findViewById(R.id.Button01);
imageview1 = (ImageView) findViewById(R.id.ImageView01);
imageview2 = (ImageView) findViewById(R.id.ImageView02);
imageview3 = (ImageView) findViewById(R.id.ImageView03);
txtview = (TextView) findViewById(R.id.TextView01);
// 初始时为控件赋初值
imageStart();
// 洗牌
RandomImage();
// imageview1控件事件
imageview1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isStart == 0) {
Current_Select_ID = 0;
onclickImage();
} else {
RestStartImage();
}
}
});
// imageview2控件事件
imageview2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// if(imageview2.getResources().getDrawable(id)
if (isStart == 0) {
Current_Select_ID = 1;
onclickImage();
} else {
RestStartImage();
}
}
});
// imageview3控件事件
imageview3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (isStart == 0) {
Current_Select_ID = 2;
onclickImage();
} else {
RestStartImage();
}
}
});
// 重来按钮事件
btnStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
RestStartImage();
}
});
}
// 洗牌事件
private void RandomImage() {
for (int i = 0; i < 3; i++) {
int tmp = imagearray[i];
int srandom = (int) (Math.random() * 2);
imagearray[i] = imagearray[srandom];
imagearray[srandom] = tmp;
}
}
// 给图片赋初值
private void imageStart() {
imageview1.setImageResource(R.drawable.p04);
imageview2.setImageResource(R.drawable.p04);
imageview3.setImageResource(R.drawable.p04);
Current_Select_ID = 0;
imageview1.setAlpha(255);
imageview2.setAlpha(255);
imageview3.setAlpha(255);
txtview.setText("");
isStart = 0;
}
// 点图片时产生效果
private void onclickImage() {
isStart = 1;
new AlertDialog.Builder(paygamesCard.this)
.setTitle("确认")
.setMessage("是否继续")
.setPositiveButton("确定", new Dialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
imageview1.setImageDrawable(getResources().getDrawable(imagearray[0]));
imageview2.setImageDrawable(getResources().getDrawable(imagearray[1]));
imageview3.setImageDrawable(getResources().getDrawable(imagearray[2]));
// TODO Auto-generated method stub
if(Current_Select_ID==0)
{
imageview2.setAlpha(100);
imageview3.setAlpha(100);
}
else if(Current_Select_ID==1)
{
imageview1.setAlpha(100);
imageview3.setAlpha(100);
}
else
{
imageview1.setAlpha(100);
imageview2.setAlpha(100);
}
if (imagearray[Current_Select_ID] == R.drawable.p01) {
txtview.setText("恭喜您选中了红桃A");
Toast.makeText(paygamesCard.this, "恭喜您选中了红桃A", Toast.LENGTH_SHORT)
.show();
} else {
txtview.setText("没有选中红桃A。点牌重新始");
Toast.makeText(paygamesCard.this, "没有选中红桃A。点牌重新始",
Toast.LENGTH_SHORT).show();
}
}}).setNegativeButton("取消", new Dialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
isStart=0;
}}).show();
}
//重新开始
public void RestStartImage()
{
new AlertDialog.Builder(paygamesCard.this)
.setTitle("重新开始")
.setMessage("是否重新开始")
.setPositiveButton("确定",new Dialog.OnClickListener(){
@Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
imageStart();
RandomImage();
}})
.setNegativeButton("取消", null).show();
}
}