手把手教你写《雷神》游戏(三)

这里写图片描述
这是游戏选项界面:

option.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="100dp" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="15sp"
            android:text="@string/bgmusic" />

        <RadioGroup
            android:id="@+id/radioGroup1"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio1"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:checked="true"
                android:textSize="15sp"
                android:text="@string/on" />

            <RadioButton
                android:id="@+id/radio2"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="15sp"
                android:text="@string/off" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout1"
        android:layout_marginTop="50dp" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="15sp"
            android:text="@string/gamemusic" />

        <RadioGroup
            android:id="@+id/radioGroup2"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio3"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:checked="true"
                android:textSize="15sp"
                android:text="@string/on" />

            <RadioButton
                android:id="@+id/radio4"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="15sp"
                android:text="@string/off" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayout3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout2"
        android:layout_marginTop="57dp" >

        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:textSize="15sp"
            android:text="@string/planemodel" />

        <RadioGroup
            android:id="@+id/radioGroup3"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal" >

            <RadioButton
                android:id="@+id/radio5"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:checked="true"
                android:textSize="15sp"
                android:text="@string/model1" />

            <RadioButton
                android:id="@+id/radio6"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="15sp"
                android:text="@string/model2" />

            <RadioButton
                android:id="@+id/radio7"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:textSize="15sp"
                android:text="@string/model3" />
        </RadioGroup>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/linearLayout3"
        android:gravity="center" >

        <ImageView
            android:id="@+id/imageView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="55sp"
            android:contentDescription="@null"
            android:src="@drawable/red" />

        <ImageView
            android:id="@+id/imageView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="50sp"
            android:contentDescription="@null"
            android:src="@drawable/yellow" />

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="45sp"
            android:contentDescription="@null"
            android:src="@drawable/blue" />
    </LinearLayout>

    <Button
        android:id="@+id/ok"
        android:textSize="15sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="35dp"
        android:text="@string/ok" />

</RelativeLayout>

OptionActivity.java

package com.example.gamedemo;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class OptionActivity extends Activity {
    private Button btnOk;
    private RadioGroup rg1, rg2, rg3;
    private SharedPreferences sharedPreferences;
    private String bgMusic, gameMusic, planeModel;

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

        btnOk = (Button) findViewById(R.id.ok);
        rg1 = (RadioGroup) findViewById(R.id.radioGroup1);
        rg2 = (RadioGroup) findViewById(R.id.radioGroup2);
        rg3 = (RadioGroup) findViewById(R.id.radioGroup3);

        // 把选项值重新写入sharedPreferences
        btnOk.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                sharedPreferences = getSharedPreferences("option",
                        Activity.MODE_PRIVATE);
                Editor editor = sharedPreferences.edit();
                editor.putString("bgMusic", bgMusic);
                editor.putString("gameMusic", gameMusic);
                editor.putString("planeModel", planeModel);
                editor.apply();

                setResult(RESULT_OK, new Intent(OptionActivity.this,
                        Login.class));
                finish();
            }
        });

        // 获取bg music
        rg1.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                bgMusic = radioButton.getText().toString();
            }
        });

        // 获取game music
        rg2.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                gameMusic = radioButton.getText().toString();
            }
        });

        // 获取plane model
        rg3.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) findViewById(checkedId);
                planeModel = radioButton.getText().toString();
            }
        });
    }

    /**
     * 根据已存在的sharedPreferences生成已经生成过的选项 (non-Javadoc)
     * 
     * @see android.app.Activity#onStart()
     */
    @Override
    protected void onStart() {
        super.onStart();
        sharedPreferences = getSharedPreferences("option",
                Activity.MODE_PRIVATE);
        bgMusic = sharedPreferences.getString("bgMusic", "开");
        gameMusic = sharedPreferences.getString("gameMusic", "开");
        planeModel = sharedPreferences.getString("planeModel", "机型一");

        if (bgMusic.equals("开")) {
            rg1.check(R.id.radio1);
        } else {
            rg1.check(R.id.radio2);
        }

        if (gameMusic.equals("开")) {
            rg2.check(R.id.radio3);
        } else {
            rg2.check(R.id.radio4);
        }

        if (planeModel.equals("机型一")) {
            rg3.check(R.id.radio5);
        } else if (planeModel.equals("机型二")) {
            rg3.check(R.id.radio6);
        } else if (planeModel.equals("机型三")) {
            rg3.check(R.id.radio7);
        }
    }

    /**
     * 重写返回键事件 (non-Javadoc)
     * 
     * @see android.app.Activity#onKeyDown(int, android.view.KeyEvent)
     */
    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            setResult(RESULT_OK);
            finish();
            return true;
        } else
            return super.onKeyDown(keyCode, event);
    }
}

游戏选项界面也简单

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值