Android白天夜晚模式切换实现
今天群里聊天说是实现白天夜晚模式切换不好搞,网上看了一下净是些设置theme的。仔细想了一下,白天夜晚模式的切换无非就是重新设置空间的背景色与字体颜色,如果这样的话,用broadcast receiver不是一个很好的实现方式吗?
上代码:
新建一个工程DayAndNightDemo。打开fragment_main.xml添加一个Button.
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.dayandnightdemo.MainActivity$PlaceholderFragment" > <TextView android:id="@+id/main_hello" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_world" /> <Button android:id="@+id/main_btn" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:text="@string/jump" /> </RelativeLayout>
打开res/menu/main.xml添加一个菜单
<menu 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" tools:context="com.example.dayandnightdemo.MainActivity" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:title="@string/action_settings" app:showAsAction="never"/> <item android:id="@+id/action_change" android:orderInCategory="100" app:showAsAction="never"/> </menu>
到MainActivity
package com.example.dayandnightdemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v7.app.ActionBarActivity; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.Button; import android.widget.TextView; /** * (用的比较新的ADT),最下面会有demo链接,导入自己的项目中的时候记得导入你自己的appcompat_v7, * 具体方法是鼠标右键DayAndNightDemo--》Properties--》Android--》Library--》remove--》add。 * * @author Administrator * */ public class MainActivity extends ActionBarActivity { private static MenuItem changeItem; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if (savedInstanceState == null) { getSupportFragmentManager().beginTransaction() .add(R.id.container, new PlaceholderFragment()).commit(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); changeItem = menu.findItem(R.id.action_change); if (!Util.flag)// 夜晚 changeItem.setTitle(R.string.action_change_day); else changeItem.setTitle(R.string.action_change_night); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } else if (id == R.id.action_change) { Intent intent = new Intent(); if (item.getTitle().toString() .equals(getString(R.string.action_change_night))) Util.flag = false;// 夜晚 else if (item.getTitle().toString() .equals(getString(R.string.action_change_day))) Util.flag = true;// 白天 intent.setAction(Util.filter); this.sendBroadcast(intent); return true; } return super.onOptionsItemSelected(item); } /** * A placeholder fragment containing a simple view. */ public static class PlaceholderFragment extends Fragment { View rootView; TextView helloText; Button button; BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (!Util.flag) { rootView.setBackgroundColor(Color.BLACK); helloText.setBackgroundColor(Color.BLACK); helloText.setTextColor(Color.WHITE); button.setBackgroundColor(Color.DKGRAY); button.setTextColor(Color.WHITE); MainActivity.changeItem .setTitle(R.string.action_change_day); } else if (Util.flag) { rootView.setBackgroundColor(Color.WHITE); helloText.setBackgroundColor(Color.WHITE); helloText.setTextColor(Color.BLACK); button.setBackgroundColor(Color.GRAY); button.setTextColor(Color.BLACK); MainActivity.changeItem .setTitle(R.string.action_change_night); } } }; public PlaceholderFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { rootView = inflater.inflate(R.layout.fragment_main, container, false); helloText = (TextView) rootView.findViewById(R.id.main_hello); button = (Button) rootView.findViewById(R.id.main_btn); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { // TODO Auto-generated method stub Intent intent = new Intent(getActivity(), SecondActivity.class); getActivity().startActivity(intent); } }); IntentFilter filter = new IntentFilter(); filter.addAction(Util.filter); getActivity().registerReceiver(receiver, filter); return rootView; } @Override public void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); getActivity().unregisterReceiver(receiver); android.os.Process.killProcess(android.os.Process.myPid()); } } }
添加一个新Activity,名字叫SecondActivity
package com.example.dayandnightdemo; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.graphics.Color; import android.os.Bundle; import android.support.v7.app.ActionBarActivity; import android.view.Menu; import android.view.MenuItem; import android.widget.LinearLayout; import android.widget.TextView; public class SecondActivity extends ActionBarActivity { private LinearLayout linear; private TextView text; private MenuItem changeItem; @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); changeItem = menu.findItem(R.id.action_change); if (!Util.flag)// 夜晚 changeItem.setTitle(R.string.action_change_day); else changeItem.setTitle(R.string.action_change_night); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } else if (id == R.id.action_change) { Intent intent = new Intent(); if (item.getTitle().toString() .equals(getString(R.string.action_change_night))) Util.flag = false;// 夜晚 else if (item.getTitle().toString() .equals(getString(R.string.action_change_day))) Util.flag = true;// 白天 intent.setAction(Util.filter); this.sendBroadcast(intent); return true; } return super.onOptionsItemSelected(item); } @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.activity_second); init(); IntentFilter filter = new IntentFilter(); filter.addAction(Util.filter); registerReceiver(receiver, filter); } private void init() { // TODO Auto-generated method stub linear = (LinearLayout) findViewById(R.id.second_linear); text = (TextView) findViewById(R.id.second_text); if (!Util.flag)// 夜晚 { linear.setBackgroundColor(Color.BLACK); text.setBackgroundColor(Color.BLACK); text.setTextColor(Color.WHITE); } } BroadcastReceiver receiver = new BroadcastReceiver() { @Override public void onReceive(Context context, Intent intent) { // TODO Auto-generated method stub if (!Util.flag) { linear.setBackgroundColor(Color.BLACK); text.setBackgroundColor(Color.BLACK); text.setTextColor(Color.WHITE); changeItem.setTitle(R.string.action_change_day); } else if (Util.flag) { linear.setBackgroundColor(Color.WHITE); text.setBackgroundColor(Color.WHITE); text.setTextColor(Color.BLACK); changeItem.setTitle(R.string.action_change_night); } } }; @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); unregisterReceiver(receiver); } }
在mainifest.xml中注册
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.dayandnightdemo" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name="com.example.dayandnightdemo.MainActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <activity android:name="com.example.dayandnightdemo.SecondActivity" /> </application> </manifest>
Util
package com.example.dayandnightdemo; public class Util { public static String filter = "com.example.dayandnightdemo.change"; public static boolean flag = true;// true表示白天模式,false表示夜晚模式 }
strings.xml
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">DayAndNightDemo</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="action_change_night">夜晚</string> <string name="action_change_day">白天</string> <string name="jump">跳转</string> <string name="second_activity">这是个第二个activity</string> </resources>
源码下载 (用的比较新的ADT),最下面会有demo链接,导入自己的项目中的时候记得导入你自己的appcompat_v7, 具体方法是鼠标右键DayAndNightDemo--》Properties--》Android--》Library--》remove--》add。
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。