一,调用系统给的对话框
如下是一个简单的提示是否退出的对话框
(1)生成AlertDialog.Buidler对象,并传入默认
AlertDialog.Builder builder = new AlartDialog.Builder(DialogAlertDialog.this);
(2)设置积极按钮
builder.setMessage("你确定退出吗?").setPositiveButon("是的" , new DialogInferface.OnClickListener(){
public void onClick(DialogInterface dialog , int id){
dialog.dismiss();
}
});
(3)设置消极按钮
builder.setMessage("你确定退出吗?").setNegativeButon("不是" , new DialogInferface.OnClickListener(){
public void onClick(DialogInterface dialog , int id){
dialog.cancel();
}
});
(4)用builder.create()方法创建新窗口对象
AlertDialog alert = builder.create();
(5)展示
alert.show();
二,自定义对话框
(1)/定义AlertDialog.Builer和AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
(2)生成LayoutInflater对象
LayoutInflater inflater = LayoutInflater.from(this.getContext());
(3)使用inflater对象,根据一个XML文件生成一个View对象
View layout = inflater.inflate(R.layout.dialog , null);
//这里在R.layout下自己创建了个名为dialog的XML文件
(4) 使用layout.findViewById()获取自定义XML属性,并在里面设置你想要的结果
TextView text = (TextView)layout.findViewById(R.id.usedTextId);
如我的:(设置结果)
//设置文本
text.setText(sb.toString());
(5) 生成AlertDialog.Builer对象
builder = new AlertDialog.Builder(this.getContext());
(6) /把当前布局添加到builder中
builder.setView(layout);
(7) 用builder.create()生成AlertDialog对象
alertDialog = builder.create();
(8)调用AlertDialog.show()方法展示出来
alertDialog.show();
例子:这里不举了
给一段关键代码吧,这是我自定义View里的,我是搞一个数独游戏
//定义AlertDialog.Builer和AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
//生成LayoutInflater对象
LayoutInflater inflater = LayoutInflater.from(this.getContext());
//使用inflater对象,根据一个XML文件生成一个View对象
View layout = inflater.inflate(R.layout.dialog , null);
//使用layout.findViewById()获取自定义XML属性
TextView text = (TextView)layout.findViewById(R.id.usedTextId);
//设置文本
text.setText(sb.toString());
//生成AlertDialog.Builer对象
builder = new AlertDialog.Builder(this.getContext());
//把当前布局添加到builder中
builder.setView(layout);
//用builder.create()生成AlertDialog对象
alertDialog = builder.create();
//调用AlertDialog.show()方法展示出来
alertDialog.show();
若是想看完整代码,我就把还没完成的游戏布局发上来:
package com.cai.maiyu.shudu;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.graphics.Paint.FontMetrics;
import android.widget.TextView;
/**
* Created by maiyu on 2016/4/11.
*/
public class Shudu extends View {
private float width;
private float height;
//生成一个游戏对象(自定义的类),用于填数字
private Game game = new Game();
public Shudu(Context context) {
super(context);
}
//在onSizeChanged方法中,利用获取的宽高来设置每个格子的宽高
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
//因为总共有9*9个格
this.width = w/9f;
this.height = h/9f;
super.onSizeChanged(w, h, oldw, oldh);
}
@Override
protected void onDraw(Canvas canvas) {
//生成4个画笔对象
//(1)背景,绘制大框
Paint backgroundPaint = new Paint();
backgroundPaint.setTextSize(100);
backgroundPaint.setColor(getResources().getColor(R.color.shudu_background));
canvas.drawRect(0, 0, getWidth(), getHeight(), backgroundPaint);
//3个线
Paint hilitePaint = new Paint();
hilitePaint.setColor(getResources().getColor(R.color.shudu_hilite));
Paint darkPaint = new Paint();
darkPaint.setColor(getResources().getColor(R.color.shudu_dark));
Paint lightPaint = new Paint();
lightPaint.setColor(getResources().getColor(R.color.shudu_light));
//绘制9*9个格:即横竖各8条线
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
//绘制横竖各两条大线,用于分割每9个小格成为一个中格
for(int i = 0; i < 9; i++){
//每3个格画一条线
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);
canvas.drawLine(0, i * height + 2 , getWidth() , i * height + 2 , darkPaint);
canvas.drawLine(0, i * height + 3 , getWidth() , i * height + 3 , hilitePaint);
canvas.drawLine(i * width + 2 , 0 , i * width + 2 , getHeight() ,darkPaint);
canvas.drawLine(i * width + 3 , 0 , i * width + 3 , getHeight() , hilitePaint);
}//for
//生成数字画笔
Paint numberPaint = new Paint();
numberPaint.setColor(Color.BLACK);
numberPaint.setStyle(Paint.Style.STROKE);
numberPaint.setTextSize(height * 0.75f);
//设置内容居中
numberPaint.setTextAlign(Paint.Align.CENTER);
//设置FontMetrics
FontMetrics fm = numberPaint.getFontMetrics();
float x = width/2;
//利用基准线来设置高度:正中间
float y = height/2 - (fm.ascent + fm.descent)/2;
//float y = height/2 + fm.descent;
//用一个两层的for循环来填数字
for(int i = 0; i < 9; i++){
for(int j = 0; j < 9; j++){
canvas.drawText(game.getTitleString(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);
}
int selectedX = (int)(event.getX()/width);
int selectedY = (int)(event.getY()/height);
int used[] = game.getUsedTitlesByCoor(selectedX , selectedY);
//生成一个StringBuffered对象
StringBuffer sb = new StringBuffer();
//这里在后台打印
for(int i = 0; i < used.length; i++){
sb.append(used[i]);
}
//定义AlertDialog.Builer和AlertDialog
AlertDialog.Builder builder;
AlertDialog alertDialog;
//生成LayoutInflater对象
LayoutInflater inflater = LayoutInflater.from(this.getContext());
//使用inflater对象,根据一个XML文件生成一个View对象
View layout = inflater.inflate(R.layout.dialog , null);
//使用layout.findViewById()获取自定义XML属性
TextView text = (TextView)layout.findViewById(R.id.usedTextId);
//设置文本
text.setText(sb.toString());
//生成AlertDialog.Builer对象
builder = new AlertDialog.Builder(this.getContext());
//把当前布局添加到builder中
builder.setView(layout);
//用builder.create()生成AlertDialog对象
alertDialog = builder.create();
//调用AlertDialog.show()方法展示出来
alertDialog.show();
return true;
}
}
另一个类
package com.cai.maiyu.shudu;
/**
* Created by maiyu on 2016/4/12.
*/
public class Game {
//初始化界面
private final String str = "360000000004230800000004200" + "070460003820000014500013020"
+ "001900000007048300000000045";
//定义一个整形数字,存放数字
private int shudu[] = new int[9 * 9];
//用于存储每个单元格已经不可用的数据
private int used[][][] = new int[9][9][];
//
public Game(){
//在生成game对象时调用
shudu = fromPuzzleString(str);
calculateAllUsedTiles();
}
//用于所有的单元格对应的不可用数据
public void calculateAllUsedTiles(){
for(int x = 0; x < 9; x++){
for(int y = 0; y < 9; y++){
used[x][y] = calculateUsedTiles(x,y);
}
}
}
//根据x,y坐标得到当前单元格当中不可用的数据
public int[] getUsedTitlesByCoor(int x , int y){
return used[x][y];
}
//计算某一单元格中已经不可用的数据
public int[] calculateUsedTiles(int x , int y){
int c[] = new int[9];
//分别判断竖行和横行
for(int i = 0; i < 9; i++){
//如果当前为空
if(i == y){
continue;
}
int t = getTitle(x , i);
if(t != 0){
c[t-1] = t;
}
}
for(int i = 0; i < 9; i++){
//如果当前为空
if(i == x){
continue;
}
int t = getTitle(i , y);
if(t != 0){
c[t-1] = t;
}
}
int startx = (x / 3) * 3;
int starty = (y / 3) * 3;
for (int i = startx; i < startx + 3; i++) {
for (int j = starty; j < starty + 3; j++) {
if (i == x && j == y)
continue;
int t = getTitle(i, j);
if (t != 0)
c[t - 1] = t;
}
}
//压缩0
int nused = 0;
for(int t : c){
if(t != 0)
nused++;
}
int c1[] = new int[nused];
nused = 0;
for(int t : c){
if(t != 0){
c1[nused++] = t;
}
}
return c1;
}
//分割字符串,并把每个字符以整形方式放入数组中
protected int[] fromPuzzleString(String str){
int[] sd = new int[str.length()];
for(int i = 0; i < sd.length; i++){
//以整形方式存
sd[i] = str.charAt(i) - '0';
}
return sd;
}
//获得当前空格值
public int getTitle(int x , int y){
return shudu[y * 9 + x];
}
//
public String getTitleString(int x , int y){
//获取当前空格值
int v = getTitle(x , y);
if(v == 0){
return "";
}
else{
return String.valueOf(v);
}
}
//
}
主类:
package com.cai.maiyu.shudu;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
//用View对象作为参数
//MyView myView = new MyView(this);
//setContentView(myView);
setContentView(new Shudu(this));
}
}