import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private LinearLayout ILay_guess,ILay_order,ILay_setting;
private ImageView img_guess,img_order,img_setting;
private TextView txt_guess,txt_order,txt_setting;
private Fragment fm_guess,fm_order,fm_setting;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
setDefaultFragment();
setOnClickLisener();
}
private void initView(){
ILay_guess = findViewById(R.id.llay_guess);
ILay_order = findViewById(R.id.llay_order);
ILay_setting = findViewById(R.id.llay_setting);
img_guess = findViewById(R.id.img_guess);
img_order = findViewById(R.id.img_order);
img_setting = findViewById(R.id.img_setting);
txt_guess = findViewById(R.id.txt_guess);
txt_order = findViewById(R.id.txt_order);
txt_setting = findViewById(R.id.txt_setting);
}
private void setDefaultFragment(){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
fm_guess = new GuessFragment();
transaction.replace(R.id.fragment_content,fm_guess,“GUESS”);
transaction.commit();
}
private void setOnClickLisener(){
ILay_guess.setOnClickListener(new LinearLayoutOnClickLisener());
ILay_order.setOnClickListener(new LinearLayoutOnClickLisener());
ILay_setting.setOnClickListener(new LinearLayoutOnClickLisener());
}
private class LinearLayoutOnClickLisener implements View.OnClickListener{
@Override
public void onClick(View v) {
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
resetLinearLayout();
switch (v.getId()){
case R.id.llay_guess:
if (fm_guess == null){
fm_guess = new GuessFragment();
}
img_guess.setImageResource(R.drawable.game_on);
txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_guess,“GUESS”);
break;
case R.id.llay_order:
if (fm_order == null){
fm_order = new OrderFragment();
}
img_order.setImageResource(R.drawable.rank_on);
txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_order,“ORDER”);
break;
case R.id.llay_setting:
if (fm_setting == null){
fm_setting = new SettingFragment();
}
img_setting.setImageResource(R.drawable.setting_on);
txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_pressed));
transaction.replace(R.id.fragment_content,fm_setting,“SETTING”);
break;
}
transaction.commit();
}
}
private void resetLinearLayout(){
img_guess.setImageResource(R.drawable.game_off);
img_order.setImageResource(R.drawable.rank_off);
img_setting.setImageResource(R.drawable.setting_off);
txt_guess.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
txt_order.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
txt_setting.setTextColor(ContextCompat.getColor(MainActivity.this,R.color.color_txt_normal));
}
}
【“设置”模块的实现】
在对应的布局文件“fragment_setting.xml”中,代码布局如下:
<?xml version="1.0" encoding="utf-8"?><LinearLayout 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=“.SettingFragment”
android:orientation=“vertical”>
<TextView
android:id=“@+id/txt_name”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:text=“@string/txt_name”
android:textSize=“22dp”/>
<EditText
android:id=“@+id/edit_defaultName”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:padding=“8dp”
android:hint=“@string/edit_name”
android:textSize=“22dp” />
<CheckBox
android:id=“@+id/chb_name”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text=“@string/chb_useDefaultName”
android:textSize=“22dp”
/>
<TextView
android:id=“@+id/txt_defaultRange”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text=“@string/txt_defaultRange”
android:textSize=“22dp”/>
<Spinner
android:id=“@+id/spinner”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:entries=“@array/range_array”
android:layout_marginTop=“10dp”/>
<Button
android:id=“@+id/btn_save”
android:layout_width=“match_parent”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:text=“@string/btn_setting”
android:textSize=“22dp”/>
布局文件中涉及的“strings.xml”文件内容如下:
请输入默认姓名:
使用默认姓名:
默认范围:
保存设置
默认姓名
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_margin=“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=“@+id/txt_compare”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:textSize=“22dp”
android:layout_below=“@id/btn_submit”
android:layout_centerHorizontal=“true”/>
<ImageView
android:id=“@+id/img_broad”
android:layout_width=“60dp”
android:layout_height=“60dp”
android:layout_margin=“10dp”
android:layout_toStartOf=“@id/txt_compare”
android:layout_toLeftOf=“@id/txt_compare”
android:layout_below=“@id/btn_submit”/>
<TextView
android:id=“@+id/txt_count”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_marginTop=“10dp”
android:textSize=“22dp”
android:layout_below=“@id/txt_compare”
android:layout_alignLeft=“@id/txt_compare”
android:layout_alignStart=“@id/txt_compare”
/>
<ImageView
android:id=“@+id/img_bingo”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:layout_below=“@id/img_broad”
android:layout_centerHorizontal=“true”
android:layout_marginTop=“10dp”
android:src=“@drawable/bingo”/>
在GuessFragment类中实现猜数判断逻辑:
package com.example.mygame;
import android.content.ContentValues;
import android.content.Context;
import android.content.SharedPreferences;
import android.database.sqlite.SQLiteDatabase;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Random;
public class GuessFragment extends Fragment {
private int count,rndNum;
private EditText edit_num;
private TextView txt_range,txt_compare,txt_count;
private Button btn_submit;
private ImageView img_broad,img_bingo;
private boolean flag;
private String name;
public int range = 0;
private Context context;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_guess,container,false);
context = view.getContext();
initView(view);
initData();
btn_submit.setOnClickListener(new ButtonOnClickLisener());
return view;
}
private void initView(View view){
edit_num = view.findViewById(R.id.edit_num);
txt_range = view.findViewById(R.id.txt_range);
txt_compare = view.findViewById(R.id.txt_compare);
txt_count = view.findViewById(R.id.txt_count);
btn_submit = view.findViewById(R.id.btn_submit);
img_broad = view.findViewById(R.id.img_broad);
img_bingo = view.findViewById(R.id.img_bingo);
}
public void initData(){
SharedPreferences sharedPreferences = context.getSharedPreferences(“Info”,Context.MODE_PRIVATE);
name = sharedPreferences.getString(“name”,“”);
range = sharedPreferences.getInt(“range”,100);
Random mRandom = new Random();
rndNum = mRandom.nextInt(range)+1;
txt_range.setText(“请输入1-”+range+“之间的整数”);
txt_count.setText(“”);
txt_compare.setText(“”);
img_broad.setVisibility(View.INVISIBLE);
img_bingo.setVisibility(View.INVISIBLE);
btn_submit.setText(“提交”);
flag = true;
count = 0;
}
private class ButtonOnClickLisener implements View.OnClickListener{
@Override
public void onClick(View v) {
if (flag){
String str_num = edit_num.getText().toString();
if (str_num.isEmpty()){
Toast.makeText(context,“请输入数字!”,Toast.LENGTH_SHORT).show();
return;
}
edit_num.setText(“”);
int numInput = Integer.valueOf(str_num);
if (numInput<1 || numInput > range){
Toast.makeText(context,“请输入范围内的数字!”,Toast.LENGTH_SHORT).show();
return;
}
count += 1;
img_broad.setVisibility(View.VISIBLE);
txt_count.setText(“猜数次数:”+count);
if (numInput > rndNum){
txt_compare.setText(“输入的”+str_num+“太大了!”);
}else if(numInput < rndNum){
txt_compare.setText(“输入的”+str_num+“太小了!”);
}else{
txt_compare.setText(“恭喜你猜对啦!正确答案是:”+rndNum+“!”);
img_bingo.setVisibility(View.VISIBLE);
flag = false;
btn_submit.setText(“再猜一次”);
if (name.isEmpty()){
final CustomDialog dialog = new CustomDialog(context,R.style.MyDialog);
dialog.show();
Button btn_confirm = dialog.findViewById(R.id.btn_confirm);
final EditText edit_name = dialog.findViewById(R.id.edit_inputName);
btn_confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = edit_name.getText().toString();
if (!name.isEmpty()){
insertData();
dialog.dismiss();
}
}
});
} else {
insertData();
}
}
}else {
initData();
}
}
}
private void insertData(){
MyHelper helper = new MyHelper(context,“Guess.db”,null,1);
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(“name”,name);
values.put(“count”,count);
values.put(“range”,range);
db.insert(“ordertb”,null,values);
db.close();
}
}
可以发现,在GuessFragment类中,引入了CustomDialog类,这是自定义的对话框,用于前面基本构思说的,用户在进行猜数游戏时,如果在设置页中,不选择使用默认姓名的话,游戏结束会要求用户输入用户名。
CustomDialog自定义对话框的具体实现如下:
布局文件“alertdialog_input.xml”:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
最后
由于题目很多整理答案的工作量太大,所以仅限于提供知识点,详细的很多问题和参考答案我都整理成了 PDF文件,需要的小伙伴可以私信我【面试】免费领取或者点击GitHub免费获取!
可以发现,在GuessFragment类中,引入了CustomDialog类,这是自定义的对话框,用于前面基本构思说的,用户在进行猜数游戏时,如果在设置页中,不选择使用默认姓名的话,游戏结束会要求用户输入用户名。
CustomDialog自定义对话框的具体实现如下:
布局文件“alertdialog_input.xml”:
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-eC3FvZOG-1711352039086)]
[外链图片转存中…(img-M5RR10ls-1711352039087)]
[外链图片转存中…(img-MM9naD7v-1711352039087)]
[外链图片转存中…(img-eBkJi4ai-1711352039088)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-qZqlf1rH-1711352039088)]
最后
由于题目很多整理答案的工作量太大,所以仅限于提供知识点,详细的很多问题和参考答案我都整理成了 PDF文件,需要的小伙伴可以私信我【面试】免费领取或者点击GitHub免费获取!
[外链图片转存中…(img-49SsfZcW-1711352039089)]
[外链图片转存中…(img-V7PMYu4v-1711352039089)]