使用默认姓名:
默认范围:
保存设置
默认姓名
100
1000
10000
该模块的逻辑实现代码在“SettingFragment”类中实现:
package com.example.mygame;
import android.content.Context;
import android.content.SharedPreferences;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;
public class SettingFragment extends Fragment{
private EditText edit_name;
private Button btn_save;
private CheckBox chb_defaultName;
private Spinner spinner;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_setting,container,false);
context = view.getContext();
initView(view);
btn_save.setOnClickListener(new ButtonOnClickLisener());
return view;
}
private void initView(View view){
edit_name = view.findViewById(R.id.edit_defaultName);
btn_save = view.findViewById(R.id.btn_save);
chb_defaultName = view.findViewById(R.id.chb_name);
spinner = view.findViewById(R.id.spinner);
}
public class ButtonOnClickLisener implements View.OnClickListener{
@Override
public void onClick(View v) {
SharedPreferences sharedPreferences = context.getSharedPreferences(“Info”,Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
int defaultRange = Integer.valueOf(spinner.getSelectedItem().toString());
editor.putInt(“range”,defaultRange);
if (chb_defaultName.isChecked()){
String defaultName = edit_name.getText().toString();
if (defaultName.isEmpty()){
Toast.makeText(context,“请输入默认姓名!”,Toast.LENGTH_SHORT).show();
return;
}
editor.putString(“name”,defaultName);
}
else {
editor.remove(“name”);
}
if (editor.commit()){
Toast.makeText(context,“保存成功!”,Toast.LENGTH_SHORT).show();
}
}
}
}
完成可执行run,在模拟器跑一下。
【“猜数”模块的实现】
猜数模块布局文件"fragment_guess.xml"内容如下:
<?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”
xmlns:app=“http://schemas.android.com/apk/res-auto”
xmlns:tools=“http://schemas.android.com/tools”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
tools:context=".GuessFragment"
android:background="#ffffff"
android:orientation=“vertical”>
<TextView
android:id="@+id/txt_range"
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marg
in=“5dp”
android:textSize=“22dp”/>
<EditText
android:id="@+id/edit_num"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:layout_below="@id/txt_range"
android:padding=“8dp”
android:inputType=“numberSigned”
android:textSize=“22dp”/>
<Button
android:id="@+id/btn_submit"
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:layout_below="@id/edit_num"
android:textSize=“22dp”/>
<TextView
android:id="@+