在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例Android平台开发 → android 白天 夜间模式切换 示例下载

android 白天 夜间模式切换 示例下载

Android平台开发

下载此实例
  • 开发语言:Java
  • 实例大小:2.29M
  • 下载次数:11
  • 浏览次数:281
  • 发布时间:2016-05-22
  • 实例类别:Android平台开发
  • 发 布 人:q5222890
  • 文件格式:.zip
  • 所需积分:2
 相关标签: 模式

实例介绍

【实例简介】

【实例截图】


【核心代码】

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());
		}

	}

}

标签: 模式

实例下载地址

android 白天 夜间模式切换 示例下载

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警