<LinearLayout
android:id=“@+id/llay_guess”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:gravity=“center”
android:background=“#E8EEF5”
android:orientation=“vertical”>
<ImageView
android:id=“@+id/img_guess”
android:layout_width=“40dp”
android:layout_height=“40dp”
android:src=“@drawable/game_on”/>
<TextView
android:id=“@+id/txt_guess”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/txt_guess”
android:textSize=“20dp”
android:textStyle=“bold”
android:textColor=“@color/color_txt_pressed”/>
<LinearLayout
android:layout_width=“2dp”
android:layout_height=“match_parent”
android:divider=“@color/color_txt_pressed”
android:showDividers=“end”>
<LinearLayout
android:id=“@+id/llay_order”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:orientation=“vertical”
android:gravity=“center”>
<ImageView
android:id=“@+id/img_order”
android:layout_width=“40dp”
android:layout_height=“40dp”
android:src=“@drawable/rank_off”
android:layout_gravity=“center_horizontal”/>
<TextView
android:id=“@+id/txt_order”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/txt_order”
android:textSize=“20dp”
android:textStyle=“bold”
android:textColor=“@color/color_txt_normal”/>
<LinearLayout
android:id=“@+id/llay_setting”
android:layout_width=“0dp”
android:layout_height=“match_parent”
android:layout_weight=“1”
android:orientation=“vertical”
android:gravity=“center”>
<ImageView
android:id=“@+id/img_setting”
android:layout_width=“40dp”
android:layout_height=“40dp”
android:src=“@drawable/setting_off”/>
<TextView
android:id=“@+id/txt_setting”
android:layout_width=“wrap_content”
android:layout_height=“wrap_content”
android:text=“@string/txt_setting”
android:textSize=“20dp”
android:textStyle=“bold”
android:textColor=“@color/color_txt_normal”/>
<FrameLayout
android:id=“@+id/fragment_content”
android:layout_width=“match_parent”
android:layout_height=“match_parent”
android:layout_above=“@id/linearLayout”
android:layout_alignParentLeft=“true”
android:layout_alignParentStart=“true”/>
布局中涉及的“strings.xml”文件如下:
猜数
排行榜
设置
布局中涉及的“colors.xml”文件如下:
#737373
#d4257b
在MainActivity中实现导航逻辑之前,先搭个基本页面框架:
新建GuessFragment(猜数字页)、OrderFragment(积分排行榜页)和SettingFragment(设置页)3个类,以及对应的布局文件“fragment_guess.xml”、“fragment_order.xml”和“fragment_setting.xml”。
在MainActivity中的代码如下:
package com.example.mygame;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
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;
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
架构师筑基包括哪些内容
我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。
由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容,详细完整版的【架构师筑基必备技能】文档领取方式:点赞+关注,然后私信关键词 【666】即可获得免费领取方式!或者 可以查看我的【Github】
注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!
入阿里一直到现在。**
深知大多数初中级安卓工程师,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年最新Android移动开发全套学习资料》送给大家,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
[外链图片转存中…(img-6rUu79Hx-1711352070544)]
[外链图片转存中…(img-fceKKV6R-1711352070545)]
[外链图片转存中…(img-X1kkx6P5-1711352070545)]
[外链图片转存中…(img-Kc1zVXYk-1711352070546)]
由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频
如果你觉得这些内容对你有帮助,可以添加下面V无偿领取!(备注Android)
[外链图片转存中…(img-XlXDJe91-1711352070546)]
架构师筑基包括哪些内容
我花了将近半个月时间将:深入 Java 泛型.、注解深入浅出、并发编程.、数据传输与序列化、Java 虚拟机原理、反射与类加载、高效 IO、Kotlin项目实战等等Android架构师筑基必备技能整合成了一套系统知识笔记PDF,相信看完这份文档,你将会对这些Android架构师筑基必备技能有着更深入、更系统的理解。
由于文档内容过多,为了避免影响到大家的阅读体验,在此只以截图展示部分内容,详细完整版的【架构师筑基必备技能】文档领取方式:点赞+关注,然后私信关键词 【666】即可获得免费领取方式!或者 可以查看我的【Github】
注:资料与上面思维导图一起看会更容易学习哦!每个点每个细节分支,都有对应的目录内容与知识点!
[外链图片转存中…(img-Yl21Tfi3-1711352070547)]
[外链图片转存中…(img-SOOQF5rW-1711352070548)]
这份资料就包含了所有Android初级架构师所需的所有知识!需要的可以在我的GIthub里面去查看!