Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> 紙牌游戲之五 主界面類

紙牌游戲之五 主界面類

編輯:關於Android編程

package com.example.azhipai;

import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.ContentValues;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
	private int imgpath [] =new int[]{R.drawable.img14,R.drawable.img15,R.drawable.img3,R.drawable.img4,
			R.drawable.img5,R.drawable.img6,R.drawable.img7,R.drawable.img8,R.drawable.img9,R.drawable.img10,R.drawable.img11
			,R.drawable.img12,R.drawable.img13};
	private int paire[]=new int [20];
	private int [] cmsjs5=new int [5];
	private int [] cmsjs7=new int [7];
	private String[]cmsjsindex=new String[7];
	private ImageView ivcpu,ivcm;
	private TextView tvshow;
	private EditText edtcm,edtusename;
	private Button btnesc,btnok,btnlogin,btnget;
	private int szcpu,szcm,cmexam,cpuexam,gamecishu,aa,cpusj;
	private MyDatabaseHelper mydbhelper;
	private String usename;
	private ListView lvZhipai,lvcmsjs;
	private List zhipai;
	protected boolean gamesz=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.usenamesave);
        ivcpu=(ImageView) findViewById(R.id.ivcpu);
        ivcm=(ImageView) findViewById(R.id.ivcm);
        tvshow=(TextView)findViewById(R.id.tvshow);
        edtcm=(EditText) findViewById(R.id.edtcm);
        btnok=(Button)findViewById(R.id.buttonok);
        btnlogin=(Button)findViewById(R.id.btnlogin);
        edtusename=(EditText)findViewById(R.id.edtusename);
        lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
        btnlogin.setOnClickListener(new View.OnClickListener(){
			@Override
			public void onClick(View v) {
				setusename();
				AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
		    	aldg.setIcon(R.drawable.ic_launcher);
		    	aldg.setTitle("提示");
		    	aldg.setMessage("你好"+usename+"歡迎進入紙牌游戲!");
		    	aldg.setButton(DialogInterface.BUTTON_POSITIVE, "進入", new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
							setContentView(R.layout.activity_main);
					      	ivcpu=(ImageView) findViewById(R.id.ivcpu);
					        ivcm=(ImageView) findViewById(R.id.ivcm);
					        tvshow=(TextView)findViewById(R.id.tvshow);
					        edtcm=(EditText) findViewById(R.id.edtcm);
					        btnok=(Button)findViewById(R.id.buttonok);
					        lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
					        star();
					        getcmpai();
					}
				});
		    	aldg.show();
			}});
        	
    }
    private void setusename(){
    	mydbhelper=new MyDatabaseHelper(MainActivity.this, "examsto.db", null, 2);
   	 	mydbhelper.getWritableDatabase();
   	 	usename=edtusename.getText().toString();
   	 	SQLiteDatabase db=mydbhelper.getWritableDatabase();
			ContentValues cv=new ContentValues();
			cv.clear();
			cv.put("name",usename);
			cv.put("examcm", 0);
			long l1=db.insert("exam", null, cv);
			if(l1!=-1){
				Toast.makeText(MainActivity.this, "插入數據成功", Toast.LENGTH_SHORT).show();
			}
			}
    private void ok(){
//    	cmsjsindex=new String[1];
//    	int i=cmsjsindex.length-1;
//    	if(i>0){
//    	cmsjsindex[i]="";
//    	i--;
//    	lvcmsjs=(ListView) findViewById(R.id.lvcmsz);
//    	ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系統已經實現好的xml文件simple_list_item_1
//    	lvcmsjs.setAdapter(adapter);
//    	}
    	btnok.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				paicm();
			}
		});
    }
    private void star(){
    	for(int i=0;i adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系統已經實現好的xml文件simple_list_item_1
    	lvcmsjs.setAdapter(adapter);
    	AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
    	aldg.setIcon(R.drawable.ic_launcher);
    	aldg.setTitle("提示");
    	aldg.setMessage("恭喜你獲得了5個數字!");
    	aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"確定",new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
		}});
    	aldg.show();
    }
    private void paicmsjs7(){
    	for(int k=0;k adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,cmsjsindex);//使用系統已經實現好的xml文件simple_list_item_1
    	lvcmsjs.setAdapter(adapter);
    	AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
    	aldg.setIcon(R.drawable.ic_launcher);
    	aldg.setTitle("提示");
    	aldg.setMessage("恭喜你獲得了7個數字!");
    	aldg.setButton(DialogInterface.BUTTON_NEGATIVE,"確定",new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
		}});
    	aldg.show();
    }
    private void finalyexam(){
    	if(gamecishu==5){
    	AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
    	aldg.setIcon(R.drawable.ic_launcher);
    	aldg.setTitle("提示");
    	aldg.setMessage("您選擇的是5局制。您已經進行了"+gamecishu+"場游戲了!\n"+"是否查看計分表?");
    	aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重開游戲", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				star();
			}
		});
    	aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戲", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				finish();
			}
		});
    	aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看計分表", new OnClickListener() {
			@Override
			public void onClick(DialogInterface dialog, int which) {
				Button1();
			}
		});
    	aldg.show();
    	
    	}else if(gamecishu==7){
    		AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
        	aldg.setIcon(R.drawable.ic_launcher);
        	aldg.setTitle("提示");
        	aldg.setMessage("您選擇的是七局制。您已經進行了"+gamecishu+"場游戲了!\n"+"是否查看計分表?");
        	aldg.setButton(DialogInterface.BUTTON_POSITIVE, "重開游戲", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				star();
    			}
    		});
        	aldg.setButton(DialogInterface.BUTTON_NEGATIVE, "退出游戲", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				finish();
    			}
    		});
        	aldg.setButton(DialogInterface.BUTTON_NEUTRAL, "查看計分表", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				Button1();
    			}
    		});
    	}
    }
    private void getcmpai(){
    	btnget=(Button)findViewById(R.id.btnget);
    	btnget.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
			if(gamesz==false){
				paicmsjs5();
			}
			else{
				paicmsjs7();
			}
			}
		});
    }
    private void paicm(){
    	String xz=edtcm.getText().toString();
    	if(xz.equals("")){
    		AlertDialog aldg=new AlertDialog.Builder(MainActivity.this).create();
        	aldg.setIcon(R.drawable.ic_launcher);
    		aldg.setTitle("提示");
    		aldg.setMessage("請您輸入相應的數字");
    		aldg.setButton(DialogInterface.BUTTON_POSITIVE, "確定", new OnClickListener() {
    			@Override
    			public void onClick(DialogInterface dialog, int which) {
    				edtcm.setText("");
    				edtcm.findFocus();
    			}
    		});
    		aldg.show();
    	}
    	else{
    		aa=Integer.parseInt(xz);
    		szcm = Integer.parseInt(xz);
    		if(aa<14&&aa>0){
    			if(paire[aa-1]==aa){
        			Toast.makeText(MainActivity.this, "你或電腦已經使用過此牌了。", Toast.LENGTH_SHORT).show();edtcm.setText("");return;
        		}
        	switch (aa){
        	case 1:ivcm.setImageResource(imgpath[0]);paire[aa-1]=aa;break;
        	case 2:ivcm.setImageResource(imgpath[1]);paire[aa-1]=aa;;break;
        	case 3:ivcm.setImageResource(imgpath[2]);paire[aa-1]=aa;;break;
        	case 4:ivcm.setImageResource(imgpath[3]);paire[aa-1]=aa;;break;
        	case 5:ivcm.setImageResource(imgpath[4]);paire[aa-1]=aa;;break;
        	case 6:ivcm.setImageResource(imgpath[5]);paire[aa-1]=aa;;break;
        	case 7:ivcm.setImageResource(imgpath[6]);paire[aa-1]=aa;;break;
        	case 8:ivcm.setImageResource(imgpath[7]);paire[aa-1]=aa;;break;
        	case 9:ivcm.setImageResource(imgpath[8]);paire[aa-1]=aa;;break;
        	case 10:ivcm.setImageResource(imgpath[9]);paire[aa-1]=aa;break;
        	case 11:ivcm.setImageResource(imgpath[10]);paire[aa-1]=aa;break;
        	case 12:ivcm.setImageResource(imgpath[11]);paire[aa-1]=aa;break;
        	case 13:ivcm.setImageResource(imgpath[12]);paire[aa-1]=aa;break;
        	}
        	pai();game();
    		}else
    			{
    			Toast.makeText(MainActivity.this, "請輸入1~13的數字", Toast.LENGTH_LONG).show();
    			edtcm.setText("");
				edtcm.findFocus();
    			return;
    			}
    			}
    		}
    public void game(){
    	gamecishu+=1;
    	if(szcm>szcpu){tvshow.setText("恭喜你獲勝了!分數+2");cmexam+=2;}
    	else if(szcm?", null, null, null, "pages desc");
				Cursor c=db.query("exam", null, null, null,null,null ,"examcm desc"); //最後一個參數:頁數降序! name:玩家;分數:score desc;
				zhipai=new ArrayList();
				if(c!=null){
					while(c!=null && c.moveToNext()){
						Zhipai zp=new Zhipai(); //新建一個book對象;實體類,要實體類的每個屬性;
						zp.setId(c.getInt(0));
						zp.setName(c.getString(1));
						zp.setExamcm(c.getInt(2));
						zhipai.add(zp); //每一個對象,都要加進books集合裡面;
					}
					c.close();   //關閉游標,切記,不要忘記完畢cursor對象;
				}
				//加載
				String []examInfo=new String[zhipai.size()]; //數組長度,集合長度;
				ArrayAdapterarrayAdapter=null; //適配器;
				int i=0;
				for(Zhipai z:zhipai){ //寫了一個迭代器,從books集合裡面逐個取對象book;
					//將圖書相關信息組合成一個字符串,存儲在字符串數組中的相應位置;
					examInfo[i]=z.getId()+"|\t\t\t"+z.getName()+" \t\t"+z.getExamcm();
					i++;
				}
				//使用字符串數組初始化ArrayAdapter對象;
				arrayAdapter=new ArrayAdapter(MainActivity.this,android.R.layout.simple_list_item_1,examInfo);
				lvZhipai.setAdapter(arrayAdapter);
			}
		});
		
  }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    
}

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved