国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 系統 > Android > 正文

簡單實現Android數獨游戲

2019-10-22 18:19:54
字體:
來源:轉載
供稿:網友

本文實例為大家分享了Android數獨游戲的具體代碼,供大家參考,具體內容如下

實現了點擊了相關的單元格之后會顯示出對話框提示可選數字。

原始的自定義對話框仍舊不能滿足我們的要求,原始的自定義對話框只能夠生成Bulider對象  然后通過LayoutInflater獲取相應的View 對象
(其實就是Layout 布局文件)
其實也是可以的,只是我們不能再次進行一些其他的操作了,比如說我們即使設置了TableLayout但是我們不能夠在上面完成任何操作,因為并不允許使用

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

至于控件,任何控件都可以復寫

并且可以自定義View控件   當然也是可以自定義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) {   //計算當前 單元格的寬度和高度   this.width = w/9f;   this.height = h/9f;   super.onSizeChanged(w, h, oldw, oldh);  }    @Override  protected void onDraw(Canvas canvas) {   //生成用于繪制當前 背景色的畫筆   Paint backgroundPaint = new Paint();      //設置背景顏色畫筆的顏色   backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));      //繪制背景色 左邊界都是0 右邊界位置是寬下邊界是高 覆蓋整個屏幕   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);   //設置空心   numberPaint.setStyle(Paint.Style.STROKE);   //設置文字大小為0.75 單元格 大小   numberPaint.setTextSize(height*0.75f);   //設置文字居中對齊   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默認是‘3'這個字符的左邊在屏幕的位置,如果設置了   //paint.setTextAlign(Paint.Align.CENTER);   //那就是字符的中心,y是指定這個字符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);//其實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();   //生成一個LayoutInflater對象   //LayoutInflater layoutInflater = LayoutInflater.from(ShuduView.this); 這樣寫還是不行的   //LayoutInflater layoutInflater = LayoutInflater.from(this.getContext());   //使用LayoutInflater 對象根據一個布局文件 生成一個布局文件   //View layoutView = layoutInflater.inflate(R.layout.dialog,null);   //從生成好的layoutView當中,取出相應的控件   //TextView textView = (TextView)layoutView.findViewById(R.id.usedTextId);   //設置textView的內容為已經使用的內容為哪些   //textView.setText(sb.toString());   //生成一個對話框當的Builder對象   //AlertDialog.Builder builder = new AlertDialog.Builder(this.getContext());   //設置對話框所顯示的內容   //builder.setView(layoutView);   //生成對話框對象,并將其顯示出來   //AlertDialog dialog = builder.create();   //dialog.show();            //return super.onTouchEvent(event);   return true;//用于設置回調函數一直處于等待調用狀態  }         } 

新增加的類

package com.example.shudu;  import android.app.Dialog; import android.content.Context; import android.os.Bundle; import android.view.View;  //該類用于實現Dialog,實現自定義的對話框功能 這樣的非Activity 其實就是一個組件 都是 //都是需要Context的參數的 在運用的時候用來表明 是哪一個應用程序調用了他 public class KeyDialog extends Dialog{  //用來存放代表對話框中的按鈕對象  private final View keys[] = new View[9];// Button能夠這樣使用 他的父類自然也能夠  private final int used[];    //Context 是必須的 第二個參數int[] used保存著當前單元格已經使用過的數字  public KeyDialog(Context context,int[] used) {   super(context);   this.used = used;   }    //當一個Dialog第一次顯示的時候,會調用其onCreate方法  @Override  protected void onCreate(Bundle savedInstanceState) {   // TODO Auto-generated method stub   super.onCreate(savedInstanceState);   //設置對話框的標題    setTitle("KeyDialog");    //設置布局文件    setContentView(R.layout.keypad);    //設置出九個按鈕    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> 

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VEVB武林網。


注:相關教程知識閱讀請移步到Android開發頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 昭通市| 蓝山县| 碌曲县| 九江县| 从江县| 保定市| 宝丰县| 靖宇县| 德昌县| 肥西县| 冷水江市| 内丘县| 阿克陶县| 张北县| 搜索| 福贡县| 台南县| 奉贤区| 桂阳县| 通州区| 九江县| 新田县| 三亚市| 蒙阴县| 乃东县| 外汇| 阿荣旗| 招远市| 获嘉县| 娱乐| 湖南省| 日照市| 阿尔山市| 瑞昌市| 裕民县| 漳平市| 桓台县| 洛阳市| 饶平县| 江孜县| 商水县|