欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

簡(jiǎn)單實(shí)現(xiàn)Android數(shù)獨(dú)游戲

 更新時(shí)間:2017年12月18日 11:20:31   作者:java是一門好語(yǔ)言  
這篇文章主要教大家如何簡(jiǎn)單實(shí)現(xiàn)Android數(shù)獨(dú)游戲,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了Android數(shù)獨(dú)游戲的具體代碼,供大家參考,具體內(nèi)容如下

實(shí)現(xiàn)了點(diǎn)擊了相關(guān)的單元格之后會(huì)顯示出對(duì)話框提示可選數(shù)字。

原始的自定義對(duì)話框仍舊不能滿足我們的要求,原始的自定義對(duì)話框只能夠生成Bulider對(duì)象  然后通過LayoutInflater獲取相應(yīng)的View 對(duì)象
(其實(shí)就是Layout 布局文件)
其實(shí)也是可以的,只是我們不能再次進(jìn)行一些其他的操作了,比如說我們即使設(shè)置了TableLayout但是我們不能夠在上面完成任何操作,因?yàn)椴⒉辉试S使用

自定義方法設(shè)置相關(guān)功能,只能推出一些新穎的自定義顯示控件而已了。

至于控件,任何控件都可以復(fù)寫

并且可以自定義View控件   當(dāng)然也是可以自定義Button控件的。

具體代碼:

package com.example.shudu; 
 
import android.app.AlertDialog; 
import android.content.Context; 
import android.graphics.Canvas; 
import android.graphics.Color; 
import android.graphics.Paint; 
import android.graphics.Paint.FontMetrics; 
import android.view.LayoutInflater; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.TextView; 
 
public class ShuduView extends View{ 
 
 
 //單元格的寬度和高度 
 private float width; 
 private float height; 
  
 private Game game = new Game(); 
  
 public ShuduView(Context context) { 
  super(context); 
 } 
 
  
 @Override 
 protected void onSizeChanged(int w, int h, int oldw, int oldh) { 
  //計(jì)算當(dāng)前 單元格的寬度和高度 
  this.width = w/9f; 
  this.height = h/9f; 
  super.onSizeChanged(w, h, oldw, oldh); 
 } 
 
 
 @Override 
 protected void onDraw(Canvas canvas) { 
  //生成用于繪制當(dāng)前 背景色的畫筆 
  Paint backgroundPaint = new Paint(); 
   
  //設(shè)置背景顏色畫筆的顏色 
  backgroundPaint.setColor(getResources().getColor(R.color.shudu_background)); 
   
  //繪制背景色 左邊界都是0 右邊界位置是寬下邊界是高 覆蓋整個(gè)屏幕 
  canvas.drawRect(0,0,getWidth(),getHeight(),backgroundPaint); 
   
  Paint darkPaint = new Paint(); 
  darkPaint.setColor(getResources().getColor(R.color.shudu_dark)); 
   
  Paint hilitePaint = new Paint(); 
  hilitePaint.setColor(getResources().getColor(R.color.shudu_hilite)); 
   
  Paint lightPaint = new Paint(); 
  lightPaint.setColor(getResources().getColor(R.color.shudu_light)); 
   
  for(int i=0;i<9;i++){ 
   //以下兩行代碼用戶繪制橫向的單元格線 并且利用像素差和 顏色深淺變化 顯示出凹槽效果,增加逼真感。 
   canvas.drawLine(0,i*height,getWidth(),i*height, lightPaint); 
   canvas.drawLine(0,i*height+1,getWidth(),i*height+1, hilitePaint); 
    
   canvas.drawLine(i*width,0,i*width,getHeight(),lightPaint); 
   canvas.drawLine(i*width+1,0,i*width+1,getHeight(),hilitePaint); 
  } 
   
   
  for(int i=0;i<9;i++){ 
   if(i%3!=0){ 
    continue; 
   } 
   canvas.drawLine(0,i*height,getWidth(),i*height, darkPaint); 
   canvas.drawLine(0,i*height+1,getWidth(),i*height+1, hilitePaint); 
    
   canvas.drawLine(i*width,0,i*width,getHeight(),darkPaint); 
   canvas.drawLine(i*width+1,0,i*width+1,getHeight(),hilitePaint); 
  } 
   
  //繪制文字 
  Paint numberPaint = new Paint(); 
  numberPaint.setColor(Color.BLACK); 
  //設(shè)置空心 
  numberPaint.setStyle(Paint.Style.STROKE); 
  //設(shè)置文字大小為0.75 單元格 大小 
  numberPaint.setTextSize(height*0.75f); 
  //設(shè)置文字居中對(duì)齊 
  numberPaint.setTextAlign(Paint.Align.CENTER); 
   
  FontMetrics fm =numberPaint.getFontMetrics(); 
   
   
  float x = width/2; 
   
  float y = height/2-(fm.ascent+fm.descent)/2; 
  System.out.println(y); 
  //x默認(rèn)是‘3'這個(gè)字符的左邊在屏幕的位置,如果設(shè)置了 
  //paint.setTextAlign(Paint.Align.CENTER); 
  //那就是字符的中心,y是指定這個(gè)字符baseline在屏幕上的位置 
   
  for(int i=0;i<9;i++) 
   for(int j=0;j<9;j++){ 
    //將getLocaString方法聲明成public是有必要的 0 是空字符串 所以不顯示的 
    canvas.drawText(game.getLocaString(i,j),i*width+x,j*height+y, numberPaint); 
   } 
  
   
  super.onDraw(canvas); 
 } 
 
 
 @Override 
 public boolean onTouchEvent(MotionEvent event) { 
  if(event.getAction() !=MotionEvent.ACTION_DOWN ){ 
   return super.onTouchEvent(event);//其實(shí)return true;也是一樣的 
  } 
  //返回值是float類型的 
  int selectX = (int)(event.getX()/width); 
  int selectY = (int)(event.getY()/height); 
   
  int used[] = game.getUsedNums(selectX, selectY); 
  for(int i=0;i<used.length;i++){ 
   //byte int 都是length 
   System.out.println(used[i]); 
  } 
   
  StringBuffer sb = new StringBuffer(); 
  for(int i=0;i<used.length;i++){ 
   sb.append(used[i]); 
  } 
   
  KeyDialog keyDialog = new KeyDialog(this.getContext(),used); 
  keyDialog.show(); 
  //生成一個(gè)LayoutInflater對(duì)象 
  //LayoutInflater layoutInflater = LayoutInflater.from(ShuduView.this); 這樣寫還是不行的 
  //LayoutInflater layoutInflater = LayoutInflater.from(this.getContext()); 
  //使用LayoutInflater 對(duì)象根據(jù)一個(gè)布局文件 生成一個(gè)布局文件 
  //View layoutView = layoutInflater.inflate(R.layout.dialog,null); 
  //從生成好的layoutView當(dāng)中,取出相應(yīng)的控件 
  //TextView textView = (TextView)layoutView.findViewById(R.id.usedTextId); 
  //設(shè)置textView的內(nèi)容為已經(jīng)使用的內(nèi)容為哪些 
  //textView.setText(sb.toString()); 
  //生成一個(gè)對(duì)話框當(dāng)?shù)腂uilder對(duì)象 
  //AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext()); 
  //設(shè)置對(duì)話框所顯示的內(nèi)容 
  //builder.setView(layoutView); 
  //生成對(duì)話框?qū)ο?,并將其顯示出來 
  //AlertDialog dialog = builder.create(); 
  //dialog.show(); 
   
   
   
  //return super.onTouchEvent(event); 
  return true;//用于設(shè)置回調(diào)函數(shù)一直處于等待調(diào)用狀態(tài) 
 } 
  
  
  
  
} 

新增加的類

package com.example.shudu; 
 
import android.app.Dialog; 
import android.content.Context; 
import android.os.Bundle; 
import android.view.View; 
 
//該類用于實(shí)現(xiàn)Dialog,實(shí)現(xiàn)自定義的對(duì)話框功能 這樣的非Activity 其實(shí)就是一個(gè)組件 都是 
//都是需要Context的參數(shù)的 在運(yùn)用的時(shí)候用來表明 是哪一個(gè)應(yīng)用程序調(diào)用了他 
public class KeyDialog extends Dialog{ 
 //用來存放代表對(duì)話框中的按鈕對(duì)象 
 private final View keys[] = new View[9];// Button能夠這樣使用 他的父類自然也能夠 
 private final int used[]; 
  
 //Context 是必須的 第二個(gè)參數(shù)int[] used保存著當(dāng)前單元格已經(jīng)使用過的數(shù)字 
 public KeyDialog(Context context,int[] used) { 
  super(context); 
  this.used = used;  
 } 
  
 //當(dāng)一個(gè)Dialog第一次顯示的時(shí)候,會(huì)調(diào)用其onCreate方法 
 @Override 
 protected void onCreate(Bundle savedInstanceState) { 
  // TODO Auto-generated method stub 
  super.onCreate(savedInstanceState); 
  //設(shè)置對(duì)話框的標(biāo)題 
   setTitle("KeyDialog"); 
   //設(shè)置布局文件 
   setContentView(R.layout.keypad); 
   //設(shè)置出九個(gè)按鈕 
   findViews(); 
   for(int i=0;i<used.length;i++){ 
    if(used[i]!=0){ 
     System.out.println(used[i]); 
     //利用View的成員變量和View的方法setVisibility使按鈕不可見。 
     keys[used[i]-1].setVisibility(View.INVISIBLE); 
    } 
   } 
 } 
  
 private void findViews(){ 
  keys[0] = findViewById(R.id.keypad_1); 
  keys[1] = findViewById(R.id.keypad_2); 
  keys[2] = findViewById(R.id.keypad_3); 
  keys[3] = findViewById(R.id.keypad_4); 
  keys[4] = findViewById(R.id.keypad_5); 
  keys[5] = findViewById(R.id.keypad_6); 
  keys[6] = findViewById(R.id.keypad_7); 
  keys[7] = findViewById(R.id.keypad_8); 
  keys[8] = findViewById(R.id.keypad_9); 
 } 
  
} 

TableLayout

<TableLayout 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" 
  > 
  <TableRow> 
   
  <Button 
  android:id="@+id/keypad_1" 
  android:text="1" 
  > 
  </Button>  
  
  <Button 
  android:id="@+id/keypad_2" 
  android:text="2" 
  > 
  </Button> 
   
  <Button 
  android:id="@+id/keypad_3" 
  android:text="3" 
  > 
  </Button> 
  </TableRow> 
 
  
  <TableRow> 
   
  <Button 
  android:id="@+id/keypad_4" 
  android:text="4" 
  > 
  </Button>  
  
  <Button 
  android:id="@+id/keypad_5" 
  android:text="5" 
  > 
  </Button> 
   
  <Button 
  android:id="@+id/keypad_6" 
  android:text="6" 
  > 
  </Button> 
  </TableRow> 
  
  
  <TableRow> 
   
  <Button 
  android:id="@+id/keypad_7" 
  android:text="7" 
  > 
  </Button>  
  
  <Button 
  android:id="@+id/keypad_8" 
  android:text="8" 
  > 
  </Button> 
   
  <Button 
  android:id="@+id/keypad_9" 
  android:text="9" 
  > 
  </Button> 
  </TableRow> 
  
</TableLayout> 

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論