1、对话框的使用方法
2、自定义对话框的方法
3、在对话框当中显示候选的数字
MyView.java文件:
package com.example.shudu03;
import android.app.AlertDialog;
import android.content.Context;
import android.graphics.Canvas;
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 MyView extends View {
public MyView(Context context) {
super(context);
}
private float width;
private float height;
private Game game=new Game();
@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 paintBackgroud=new Paint();
paintBackgroud.setColor(getResources().getColor(R.color.backgroundColor));
canvas.drawRect(0, 0, getWidth(), getHeight(), paintBackgroud);
//画第一条颜色重的线的画笔
Paint paintOne=new Paint();
paintOne.setColor(getResources().getColor(R.color.oneLine));
//画第二条颜色轻的线的画笔
Paint paintTwo=new Paint();
paintTwo.setColor(getResources().getColor(R.color.twoLine));
//画颜色重的线
Paint paintThree=new Paint();
paintThree.setColor(getResources().getColor(R.color.threeLine));
for(int i=0;i<9;i++){
canvas.drawLine(0, i*height, getWidth(), i*height, paintOne);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, paintTwo);
canvas.drawLine(i*width, 0, i*width, getHeight(), paintOne);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), paintTwo);
}
for(int i=0;i<9;i++){
if(i%3 != 0){
continue;
}
canvas.drawLine(0, i*height, getWidth(), i*height, paintThree);
canvas.drawLine(0, i*height+1, getWidth(), i*height+1, paintTwo);
canvas.drawLine(i*width, 0, i*width, getHeight(),paintThree);
canvas.drawLine(i*width+1, 0, i*width+1, getHeight(), paintTwo);
}
//画数字的画笔
Paint paintData=new Paint();
paintData.setColor(getResources().getColor(R.color.textColor));
paintData.setStyle(Paint.Style.STROKE);
paintData.setTextSize((float) (height*0.75));
paintData.setTextAlign(Paint.Align.CENTER);
FontMetrics fm=paintData.getFontMetrics();
float x=width/2;
float y=height/2-(fm.ascent+fm.descent)/2;
for(int i=0;i<9;i++){
for(int j=0;j<9;j++){
canvas.drawText(game.getTileString(i,
j), i*width+x, j*height+y, paintData);
}
}
super.onDraw(canvas);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if(event.getAction()!=event.ACTION_DOWN){
return super.onTouchEvent(event);
}
int selectedx=(int)(event.getX()/width);
int selectedy=(int)(event.getY()/height);
int used[]=game.getUsedByArray(selectedx, selectedy);
StringBuffer result=new StringBuffer(0);
for(int i=0; i<used.length;i++){
System.out.println(used[i]);
result=result.append(String.valueOf(used[i]));
}
LayoutInflater layoutInflater=LayoutInflater.from(this.getContext());
View layout=layoutInflater.inflate(R.layout.dialog, null);
TextView textView=(TextView)layout.findViewById(R.id.textView1);
textView.setText(result);
AlertDialog.Builder builder=new AlertDialog.Builder(this.getContext());
builder.setView(layout);
AlertDialog alertDialog=builder.create();
alertDialog.show();
return true;
}
}