Android全局主题样式控制,语言设置、主题颜色、字体大小、字体样式、长短震动、铃声设置,App必备实用功能集合
涉及Mp3播放服务
本地数据储存读写
代码简明易懂,适合初学者
使用开发工具 Android Studio 3.2
开发环境:JDK 1.8
废话不多说,有图有真相
【语言设置的效果】
【字体和字体大小设置的效果】
【主题颜色设置的效果】
【效果会和其他页面以及弹出的Toast同步】
震动和铃声就没办法截图啦!
核心代码架构如下:
【主页面布局代码】
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="@+id/Setting_RelativeLayout01"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="1dp"
android:background="@drawable/button_selector_theme01">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="@mipmap/home_icon_5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="45dp"
android:layout_marginLeft="45dp"
android:text="@string/AccountManagement"
android:textSize="12sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:src="@drawable/arrow2" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/Setting_RelativeLayout02"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="1dp"
android:background="@drawable/button_selector_theme01">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="@mipmap/home_icon_3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="45dp"
android:layout_marginLeft="45dp"
android:text="@string/UserFeedback"
android:textSize="12sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:src="@drawable/arrow2" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/Setting_RelativeLayout03"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginBottom="1dp"
android:background="@drawable/button_selector_theme01">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="@mipmap/home_icon_6" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="45dp"
android:layout_marginLeft="45dp"
android:text="@string/Version"
android:textSize="12sp" />
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="15dp"
android:layout_marginRight="15dp"
android:src="@drawable/arrow2" />
</RelativeLayout>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:textSize="12sp"
android:visibility="gone"
android:hint="@string/Please_enter_content"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<Button
android:id="@+id/Button_Test01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Chinese"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Button_Test02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/English"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Button_Test03"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Japanese"
tools:ignore="TextViewEdits" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/Size01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/minimum"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Size02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/small"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Size03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/norm"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Size04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/big"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Size05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/oversized"
tools:ignore="TextViewEdits" />
</LinearLayout>
<SeekBar
android:id="@+id/seekBar01"
style="@style/Widget.AppCompat.SeekBar.Discrete"
android:layout_width="match_parent"
android:layout_height="20dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:max="4"
android:progress="0" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/Font01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Founder_blackbody"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Font02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Founder_smooth_round"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Font03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/STXingkai"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Font04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/STXinwei"
tools:ignore="TextViewEdits" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/Theme01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Theme01"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Theme02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Theme02"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Theme03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Theme03"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Theme04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Theme04"
tools:ignore="TextViewEdits" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/shake01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Shaking_the"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/shake02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Length_of_vibration"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/shake03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Continuous_vibration"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/shake04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Closed_vibration"
tools:ignore="TextViewEdits" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="3dp"
android:layout_marginBottom="2dp"
android:orientation="horizontal">
<Button
android:id="@+id/Ringtone01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Ringtone01"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Ringtone02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Ringtone02"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Ringtone03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/Ringtone03"
tools:ignore="TextViewEdits" />
<Button
android:id="@+id/Ringtone04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:layout_marginRight="2dp"
android:layout_weight="1"
android:inputType="textCapSentences"
android:text="@string/close_Ringtone"
tools:ignore="TextViewEdits" />
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="测试按钮" />
</LinearLayout>
</ScrollView>
</RelativeLayout>
【Mp3 服务】
public class MusicService extends Service {
private MediaPlayer mMediaPlayer; //定义一个媒体文件
@Override
public void onCreate() {
super.onCreate();
L.e("onCreate");
int RingtoneType = DataAccess.ReadDataInt(this,"RINGTONE");
if (RingtoneType != 0){
switch (RingtoneType){
case 1:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone01_9420); //引入mp3文件
break;
case 2:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone02_matteo_panama); //引入mp3文件
break;
case 3:
mMediaPlayer = MediaPlayer.create(this,R.raw.ringtone03_planef); //引入mp3文件
break;
}
}
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
L.e("onStartCommand");
mMediaPlayer.start(); //启动媒体文件
return super.onStartCommand(intent, flags, startId);
}
@Override
public void onDestroy() {
L.e("onDestroy");
mMediaPlayer.stop(); //停止播放
super.onDestroy();
}
@Override
public IBinder onBind(Intent intent) {
L.e("onBind");
return null;
}
}
【BaseActivity】
public class BaseActivity extends AppCompatActivity {
public static Typeface typeFace;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//主题设置
setTheme();
//显示返回键
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
Objects.requireNonNull(getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
}
//取消任务栏(全屏)
// this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setTypeface();
}
@Override
public Resources getResources() {
float s = DataAccess.ReadDataFloat(BaseActivity.this,"size");
L.e("---------" + s);
//字体大小缩放
Resources resources = super.getResources();
Configuration configuration = resources.getConfiguration();
if(s == 0.0){
configuration.fontScale = StatcClass.Size;
}else{
configuration.fontScale = s;
}
resources.updateConfiguration(configuration, resources.getDisplayMetrics());
return resources;
}
//字体设置(需要 : <item name="android:typeface">serif</item>)
public void setTypeface(){
int fontsType = DataAccess.ReadDataInt(BaseActivity.this,"FONTS");
if(fontsType == 0){
typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZHT.TTF");
}else{
switch (fontsType){
case 1: typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZHT.TTF");
break;
case 2: typeFace = Typeface.createFromAsset(getAssets(), "fonts/FZXY.TTF");
break;
case 3: typeFace = Typeface.createFromAsset(getAssets(), "fonts/HWXK.TTF");
break;
case 4: typeFace = Typeface.createFromAsset(getAssets(), "fonts/HWXW.TTF");
break;
}
}
try
{
Field field = Typeface.class.getDeclaredField("SERIF");
field.setAccessible(true);
field.set(null, typeFace);
}
catch (NoSuchFieldException e)
{
e.printStackTrace();
}
catch (IllegalAccessException e)
{
e.printStackTrace();
}
}
//APP主题设置
public void setTheme(){
int ThemeType = DataAccess.ReadDataInt(BaseActivity.this,"THEME");
switch (ThemeType){
case 0: setTheme(R.style.AppTheme);
break;
case 1: setTheme(R.style.AppTheme01);
break;
case 2: setTheme(R.style.AppTheme02);
break;
case 3: setTheme(R.style.AppTheme03);
break;
case 4: setTheme(R.style.AppTheme04);
break;
}
}
}
【MainActivity】
public class MainActivity extends BaseActivity implements View.OnClickListener {
private Vibrator mVibrator; //创建震动服务对象
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String Language = DataAccess.ReadDataString(MainActivity.this,"Language"); //读取保存的语言标识
set(Language); //设置语言
setContentView(R.layout.activity_main);
//获取手机震动的服务
mVibrator=(Vibrator)getApplication().getSystemService(Service.VIBRATOR_SERVICE);
initView();
}
@SuppressLint("ClickableViewAccessibility")
private void initView() {
Button Button_Test01 = findViewById(R.id.Button_Test01);
Button Button_Test02 = findViewById(R.id.Button_Test02);
Button Button_Test03 = findViewById(R.id.Button_Test03);
View Setting_RelativeLayout01 = findViewById(R.id.Setting_RelativeLayout01);
View Setting_RelativeLayout02 = findViewById(R.id.Setting_RelativeLayout02);
View Setting_RelativeLayout03 = findViewById(R.id.Setting_RelativeLayout03);
Setting_RelativeLayout01.setOnClickListener(this);
Setting_RelativeLayout02.setOnClickListener(this);
Setting_RelativeLayout03.setOnClickListener(this);
Button Size01 = findViewById(R.id.Size01);
Button Size02 = findViewById(R.id.Size02);
Button Size03 = findViewById(R.id.Size03);
Button Size04 = findViewById(R.id.Size04);
Button Size05 = findViewById(R.id.Size05);
Button Font01 = findViewById(R.id.Font01);
Button Font02 = findViewById(R.id.Font02);
Button Font03 = findViewById(R.id.Font03);
Button Font04 = findViewById(R.id.Font04);
Button Theme01 = findViewById(R.id.Theme01);
Button Theme02 = findViewById(R.id.Theme02);
Button Theme03 = findViewById(R.id.Theme03);
Button Theme04 = findViewById(R.id.Theme04);
Button shake01 = findViewById(R.id.shake01);
Button shake02 = findViewById(R.id.shake02);
Button shake03 = findViewById(R.id.shake03);
Button shake04 = findViewById(R.id.shake04);
Button Ringtone01 = findViewById(R.id.Ringtone01);
Button Ringtone02 = findViewById(R.id.Ringtone02);
Button Ringtone03 = findViewById(R.id.Ringtone03);
Button Ringtone04 = findViewById(R.id.Ringtone04);
Button_Test01.setOnClickListener(this);
Button_Test02.setOnClickListener(this);
Button_Test03.setOnClickListener(this);
Size01.setOnClickListener(this);
Size02.setOnClickListener(this);
Size03.setOnClickListener(this);
Size04.setOnClickListener(this);
Size05.setOnClickListener(this);
Font01.setOnClickListener(this);
Font02.setOnClickListener(this);
Font03.setOnClickListener(this);
Font04.setOnClickListener(this);
Theme01.setOnClickListener(this);
Theme02.setOnClickListener(this);
Theme03.setOnClickListener(this);
Theme04.setOnClickListener(this);
shake01.setOnClickListener(this);
shake02.setOnClickListener(this);
shake03.setOnClickListener(this);
shake04.setOnClickListener(this);
Ringtone01.setOnClickListener(this);
Ringtone02.setOnClickListener(this);
Ringtone03.setOnClickListener(this);
Ringtone04.setOnClickListener(this);
SeekBar seekbar02 = findViewById(R.id.seekBar01);
//int position = readCodingInt("position");
int position = DataAccess.ReadDataInt(MainActivity.this, "position");
seekbar02.setMax(4); //最大值为4
seekbar02.setProgress(position); //默认值为0
seekbar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
L.e("拖动过程中的值:"+ String.valueOf(progress) + ":" + String.valueOf(fromUser));
}
@Override
public void onStartTrackingTouch(SeekBar seekBar) {
L.e("开始滑动时的值:"+ String.valueOf(seekBar.getProgress()));
}
@Override
public void onStopTrackingTouch(SeekBar seekBar) {
L.e("停止滑动时的值:" + String.valueOf(seekBar.getProgress()));
switch (seekBar.getProgress()){
case 0:
StatcClass.Size = 0.5f;
break;
case 1:
StatcClass.Size = 0.7f;
break;
case 2:
StatcClass.Size = 0.9f;
break;
case 3:
StatcClass.Size = 1.1f;
break;
case 4:
StatcClass.Size = 1.3f;
break;
}
DataAccess.DeleteData(MainActivity.this,"size");
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",seekBar.getProgress()); //储存进度条的位置
ResetActivity();
}
});
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.Button_Test01:
set("zh");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","zh"); //更新/储存数据
ResetActivity();
break;
case R.id.Button_Test02:
set("en");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","en"); //更新/储存数据
ResetActivity();
break;
case R.id.Button_Test03:
set("ja");
DataAccess.DeleteData(MainActivity.this,"Language"); //删除数据
DataAccess.SaveDataString(MainActivity.this,"Language","ja"); //更新/储存数据
ResetActivity();
break;
case R.id.Setting_RelativeLayout01:
startActivity(new Intent(MainActivity.this,zhanghaoguanli.class));
break;
case R.id.Setting_RelativeLayout02:
startActivity(new Intent(MainActivity.this,yonghufankui.class));
break;
case R.id.Setting_RelativeLayout03:
Toast.makeText(MainActivity.this,"版本号",Toast.LENGTH_SHORT).show();
break;
case R.id.Size01:
StatcClass.Size = 0.5f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",0); //储存进度条的位置
ResetActivity();
break;
case R.id.Size02:
StatcClass.Size = 0.7f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",1); //储存进度条的位置
ResetActivity();
break;
case R.id.Size03:
StatcClass.Size = 0.9f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",2); //储存进度条的位置
ResetActivity();
break;
case R.id.Size04:
StatcClass.Size = 1.1f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",3); //储存进度条的位置
ResetActivity();
break;
case R.id.Size05:
StatcClass.Size = 1.3f;
DataAccess.DeleteData(MainActivity.this,"size"); //删除数据
DataAccess.SaveDataFloat(MainActivity.this,"size", StatcClass.Size);
DataAccess.SaveDataInt(MainActivity.this,"position",4); //储存进度条的位置
ResetActivity();
break;
case R.id.Font01:
StatcClass.FONTS = 1;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font02:
StatcClass.FONTS = 2;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font03:
StatcClass.FONTS = 3;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Font04:
StatcClass.FONTS = 4;
DataAccess.SaveDataInt(MainActivity.this,"FONTS", StatcClass.FONTS);
ResetActivity();
break;
case R.id.Theme01:
StatcClass.THEME = 1;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme02:
StatcClass.THEME = 2;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme03:
StatcClass.THEME = 3;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.Theme04:
StatcClass.THEME = 4;
DataAccess.SaveDataInt(MainActivity.this,"THEME", StatcClass.THEME);
ResetActivity();
break;
case R.id.shake01:
mVibrator.vibrate(new long[]{100,100},-1);
break;
case R.id.shake02:
mVibrator.vibrate(new long[]{100,100,100,1000},0);
break;
case R.id.shake03:
mVibrator.vibrate(new long[]{1,1000},0);
break;
case R.id.shake04:
mVibrator.cancel();
break;
case R.id.Ringtone01:
StatcClass.RINGTONE = 1;
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE); //保存选定的音乐
stopService(new Intent(MainActivity.this,MusicService.class)); //停止服务
startService(new Intent(MainActivity.this,MusicService.class)); //启动服务
break;
case R.id.Ringtone02:
StatcClass.RINGTONE = 2;
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE);
stopService(new Intent(MainActivity.this,MusicService.class));
startService(new Intent(MainActivity.this,MusicService.class));
break;
case R.id.Ringtone03:
StatcClass.RINGTONE = 3;
stopService(new Intent(MainActivity.this,MusicService.class));
DataAccess.SaveDataInt(MainActivity.this,"RINGTONE", StatcClass.RINGTONE);
startService(new Intent(MainActivity.this,MusicService.class));
break;
case R.id.Ringtone04:
stopService(new Intent(MainActivity.this,MusicService.class));
break;
}
}
// 本地语言设置 关键代码
private void set(String language_type) {
Locale myLocale = new Locale(language_type);
Resources res = getResources();
DisplayMetrics dm = res.getDisplayMetrics();
Configuration conf = res.getConfiguration();
conf.locale = myLocale;
res.updateConfiguration(conf,dm);
}
//重启MainActivity,用于设置成功后实时刷新页面
private void ResetActivity(){
Intent intent=new Intent(this, MainActivity.class); //跳转到自己
startActivity(intent);
finish();//关闭自己
overridePendingTransition(0, 0); //去掉Activity切换间的动画
}
}
【DataAccess】本地数据保存/删除/读写
public class DataAccess {
/** Int **/
//储存数据
public static void SaveDataInt(Context context, String Key, int num){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
//存入数据
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putInt(Key, num);
editor01.apply(); //储存信息
}
//读取数据
public static int ReadDataInt(Context context, String Key){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
int num = sp01.getInt(Key,0);
return num;
}
/** String **/
public static void SaveDataString(Context context, String Key, String data){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putString(Key, data);
editor01.apply();
}
public static String ReadDataString(Context context, String Key){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
String num = sp01.getString(Key,"null");
return num;
}
/** Float **/
public static void SaveDataFloat(Context context, String Key, float data){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor01 = sp01.edit();
editor01.putFloat(Key, data);
editor01.apply();
}
public static float ReadDataFloat(Context context, String Key){
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
float num = sp01.getFloat(Key,0.0f);
return num;
}
//删除指定Key数据,当 KEY = ALL 时清空所有数据
public static void DeleteData(Context context, String Key){
//获取SharedPreferences对象
SharedPreferences sp01 = context.getSharedPreferences("SP01", MODE_PRIVATE);
SharedPreferences.Editor editor = sp01.edit();
if(Key.equals("ALL")){
editor.clear();
}else{
editor.remove(Key);
}
editor.commit();
}
}
【StatcClass】
用于默认标识
public class StatcClass {
public static float Size = 1.0f; //字体缩放倍数
public static int FONTS = 1; //字体大小
public static int THEME = 0; //主题设置
public static int RINGTONE = 0; //铃声设置
}
【创建文件夹放字体和铃声】
【主题设置】
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<!--设置字体-->
<item name="android:typeface">serif</item>
</style>
<style name="AppTheme01">
<item name="colorPrimary">@color/colorPrimary01</item> <!--导航栏颜色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark01</item> <!--状态栏颜色-->
<item name="android:windowBackground">@color/Bai</item> <!--背景颜色-->
<item name="colorAccent">@color/colorAccent01</item> <!--控件主题颜色-->
<item name="android:textColor">@color/textColorPrimary01</item> <!--字体颜色-->
<item name="editTextBackground">@color/Bai</item> <!--editText 背景-->
<item name="android:editTextColor">@color/textColorPrimary01</item> <!--editText 文字颜色 -->
<item name="android:buttonStyle">@style/ButtonStyle01</item> <!--Button 样式设置-->
</style>
<style name="AppTheme02">
<item name="colorPrimary">@color/colorPrimary02</item> <!--导航栏颜色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark02</item> <!--状态栏颜色-->
<item name="android:windowBackground">@color/String1</item> <!--背景颜色-->
<item name="colorAccent">@color/colorAccent02</item> <!--控件主题颜色-->
<item name="android:textColor">@color/textColorPrimary02</item> <!--字体颜色-->
<item name="editTextBackground">@color/Bai</item> <!--editText 背景-->
<item name="android:editTextColor">@color/textColorPrimary02</item> <!--editText 文字颜色 -->
<item name="android:buttonStyle">@style/ButtonStyle02</item> <!--Button 样式设置-->
</style>
<style name="AppTheme03">
<item name="colorPrimary">@color/colorPrimary03</item> <!--导航栏颜色-->
<item name="colorPrimaryDark">@color/colorPrimaryDark03</item> <!--状态栏颜色-->
<item name="android:windowBackground">@color/String1</item> <!--背景颜色-->
<item name="colorAccent">@color/colorAccent03</item> <!--控件主题颜色-->
<item name="android:textColor">@color/textColorPrimary03</item> <!--字体颜色-->
<item name="editTextBackground">@color/Bai</item> <!--editText 背景-->
<item name="android:editTextColor">@color/textColorPrimary03</item> <!--editText 文字颜色 -->
<item name="android:buttonStyle">@style/ButtonStyle03</item> <!--Button 样式设置-->
</style>
<style name="AppTheme04" >
<item name="android:windowFullscreen">false</item> <!-- true:取消状态栏, false:显示状态栏 -->
<item name="colorPrimaryDark">@color/colorPrimaryDark04</item> <!-- 系统状态栏颜色 -->
<item name="colorPrimary">@color/colorPrimary04</item> <!-- 标题 导航栏颜色 -->
<item name="android:windowBackground">@color/String1</item> <!-- 窗体背景颜色 -->
<item name="colorAccent">@color/colorAccent04</item> <!-- 系统中控件主题颜色 -->
<item name="android:textColor">@color/textColorPrimary04</item> <!-- 系统字体颜色 -->
<item name="editTextBackground">@color/Bai</item> <!--editText 背景-->
<item name="android:editTextColor">@color/textColorPrimary04</item> <!--editText 文字颜色 -->
<item name="android:buttonStyle">@style/ButtonStyle04</item> <!--Button 样式设置-->
</style>
<style name="ButtonStyle01" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button_selector_theme01</item>
<item name="android:textColor">@color/textColorPrimary01</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
<style name="ButtonStyle02" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button_selector_theme02</item>
<item name="android:textColor">@color/textColorPrimary02</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
<style name="ButtonStyle03" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button_selector_theme03</item>
<item name="android:textColor">@color/textColorPrimary03</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
<style name="ButtonStyle04" parent="@android:style/Widget.Button">
<item name="android:background">@drawable/button_selector_theme04</item>
<item name="android:textColor">@color/textColorPrimary04</item>
<item name="android:paddingTop">10dp</item>
<item name="android:paddingBottom">10dp</item>
</style>
</resources>
扫码下载App
App下载:https://aifabu.com/Q7Fn
源码下载:https://download.csdn.net/download/erp_lxkun_jak/11234212
【注意:使用开发工具是 Android Studio 哦!】
感谢你的查阅,希望可以帮到你,祝你学习愉快!