微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我也来开发2048之配置界面设计

我们的程序当然不像现在市面上的2048那样死板,一辈子4X4的面板,现在的人,讲究的就是随心所欲,所有的东西都在我的掌控中,这才爽。

所以我们的程序也得有个配置界面,国际惯例,上图:


这个配置界面主要完成以下几个功能

1、可配置游戏面板的维数,即4X4,5X5,6X6,其实继续写下去也是可以的,但是,欲望也是要有限度的啊,差不多就够了

2、要达到的目标,你可以选择到1024就爽了呢,还是2048才爽,还是4096才爽,其实也是可以一直写下去的,老规矩,不要把自己逼的太紧了,差不多就行了,虐自己何必呢

3、Contact Me,给我的博客打个广告啦

配置功能,基本写入SharedPreferences,这个因为大家使用都比较多了,就不详细说了

下面是源码:

package com.xys.game2048.activity;

import com.xys.game2048.R;
import com.xys.game2048.config.Config;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class ConfigPreference extends Activity implements OnClickListener {

    private Button btnGameLines;

    private Button btnGoal;

    private Button btnBack;

    private Button btnDone;

    private String[] gameLinesList;

    private String[] gameGoalList;

    private AlertDialog.Builder builder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.config_preference);
	initView();
    }

    private void initView() {
	btnGameLines = (Button) findViewById(R.id.btn_gamelines);
	btnGoal = (Button) findViewById(R.id.btn_goal);
	btnBack = (Button) findViewById(R.id.btn_back);
	btnDone = (Button) findViewById(R.id.btn_done);
	btnGameLines.setText("" + Config.sp.getInt(Config.KEY_GameLines, 4));
	btnGoal.setText("" + Config.sp.getInt(Config.KEY_GameGoal, 2048));
	btnGameLines.setonClickListener(this);
	btnGoal.setonClickListener(this);
	btnBack.setonClickListener(this);
	btnDone.setonClickListener(this);
	gameLinesList = new String[] { "4", "5", "6" };
	gameGoalList = new String[] { "1024", "2048", "4096" };
    }

    private void saveConfig() {
	Editor editor = Config.sp.edit();
	editor.putInt(Config.KEY_GameLines, Integer.parseInt(btnGameLines.getText().toString()));
	editor.putInt(Config.KEY_GameGoal, Integer.parseInt(btnGoal.getText().toString()));
	editor.commit();
    }

    @Override
    public void onClick(View v) {
	switch (v.getId()) {
	case R.id.btn_gamelines:
	    builder = new AlertDialog.Builder(this);
	    builder.setTitle("choose the lines of the game");
	    builder.setItems(gameLinesList, new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
		    btnGameLines.setText(gameLinesList[which]);
		}
	    });
	    builder.create().show();
	    break;
	case R.id.btn_goal:
	    builder = new AlertDialog.Builder(this);
	    builder.setTitle("choose the goal of the game");
	    builder.setItems(gameGoalList, new DialogInterface.OnClickListener() {

		@Override
		public void onClick(DialogInterface dialog, int which) {
		    btnGoal.setText(gameGoalList[which]);
		}
	    });
	    builder.create().show();
	    break;
	case R.id.btn_back:
	    this.finish();
	    break;
	case R.id.btn_done:
	    saveConfig();
	    setResult(RESULT_OK);
	    this.finish();
	    break;
	default:
	    break;
	}
    }
}

以上

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐