Android开发——手把手写APP(四)——数独游戏编写(四)——显示候选数字

显示候选数字


对话框的使用

Android中有类库自带的对话框,比如alertDialog,为了弹出获取用户的一些选择
在这里插入图片描述
但是有时候类库自带的对话框不能满足我们的需求,很多情况我们需要设置自己想要的对话框,比如需要填写一些信息在这里插入图片描述

自定义对话框的方法

我们的目标是做出如下图一样的数字对话框供玩家选择数字
在这里插入图片描述
首先,自定义dialog需要有自己的布局文件
很明显,布局文件就是一个TableLayout做成的简单键盘
这个简单就不写注释了,仅仅是TableLayout和Button控件

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TableLayout
        android:layout_gravity="center"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/keypad">
        <TableRow>
            <Button
                android:text="1"
                android:textSize="16sp"
                android:id="@+id/keypad_1"
                />
            <Button
                android:text="2"
                android:textSize="16sp"
                android:id="@+id/keypad_2"/>
            <Button
                android:text="3"
                android:textSize="16sp"
                android:id="@+id/keypad_3"/>
        </TableRow>
        <TableRow>
            <Button
                android:text="4"
                android:textSize="16sp"
                android:id="@+id/keypad_4"/>
            <Button
                android:text="5"
                android:textSize="16sp"
                android:id="@+id/keypad_5"/>
            <Button
                android:text="6"
                android:textSize="16sp"
                android:id="@+id/keypad_6"/>
        </TableRow>
        <TableRow>
            <Button
                android:text="7"
                android:textSize="16sp"
                android:id="@+id/keypad_7"/>
            <Button
                android:text="8"
                android:textSize="16sp"
                android:id="@+id/keypad_8"/>
            <Button
                android:text="9"
                android:textSize="16sp"
                android:id="@+id/keypad_9"/>
        </TableRow>
    </TableLayout>
</LinearLayout>

第二步,创建一个KeyDialog类,并完善dialog的属性和方法

public class KeyDialog extends Dialog {

    /**
     * 构造函数
     * @param context 上下文对象
     */
    public KeyDialog(Context context) {
        super(context);
    }

    //当一个Dialog第一次显示的时候会调用onCreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置对话框标题
        setTitle("请选择数字");
        //用于为Diaglog设置布局文件
        setContentView(R.layout.keypad);
    }
}

这时候,Dialog已经完成
第三步,要显示Dialog只要在点击事件中创建它即可。

在对话框中显示候选数字

要只显示候选数字,换句话说,就是把不能用的数字设置为不可见
那么我们需要在dialog显示的时候,把不可用数字的数字传给它,并在显示时设置为不可见。

public class KeyDialog extends Dialog {

    //用来存放代表对话框当中9个按钮的对象
    private final View keys[] = new View[9];
    //已经不能使用的数字
    private final int used[];

    /**
     * 构造函数
     * @param context 上下文对象
     * @param used 已经不能使用的数字
     */
    public KeyDialog(Context context, int[] used) {
        super(context);
        this.used = used;
    }

    //当一个Dialog第一次显示的时候会调用onCreate方法
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //设置对话框标题
        setTitle("请选择数字");
        //用于为Diaglog设置布局文件
        setContentView(R.layout.keypad);
        findViews();
        //把不能使用的数字按钮设为不可见
        for(int i = 0;i < used.length;i++){
            if(used[i] != 0)
                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);
    }
}

效果如下:
在这里插入图片描述
本文完结,下一文讲述如何点击按钮,执行刷新把数字画在画布上,并重新计算不可用数字。

发布了230 篇原创文章 · 获赞 250 · 访问量 28万+

猜你喜欢

转载自blog.csdn.net/weixin_42247720/article/details/103508612
今日推荐