sadfa
2015/03/16 星期一
在Android平台下编写客户端程序,界面只有开关若干个。
代码更新与3.17号
mainActivity.java:
package com.fan.myapp;
import android.app.Activity;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView mTestView;
private Switch mLightSwitch1;
private Switch mLightSwitch2;
private ImageView mLight;
boolean isChanged = false;
SharedPreferences store_light2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//显示开关动作状态
mTestView = (TextView)findViewById(R.id.test1);
//设置Switch开关
mLightSwitch1 = (Switch)findViewById(R.id.switch1);
mLightSwitch2 = (Switch)findViewById(R.id.switch2);
//设置图片开关
mLight = (ImageView)findViewById(R.id.lightShow);
//保存开关信息
//得到配置参数的类 ,参数1 配置参数文件的名字,没有后缀名 ,参数2 文件访问模式 MODE_PRIVATE只能是生成这个文件的应用访问
SharedPreferences store_light1 = getSharedPreferences("light",MODE_PRIVATE); //数据只能被本应用程序读、写
final Editor editor = store_light1.edit(); // 使用匿名内部类,隐式调用外部变量,外部变量需要final修饰。
//读取配置信息中保存的的开关状态
SharedPreferences share_light = getSharedPreferences("light", MODE_PRIVATE);
String lightshare1 = share_light.getString("lightSwitch1", ""); //根据key寻找值 参数1 key 参数2 如果没有value显示的内容
String lightshare2 = share_light.getString("lightSwitch2", "");
String imageshare = share_light.getString("imageSwitch", "");
Toast.makeText(this, "light1:" + lightshare1 + "\n" + "light2:" + lightshare2 + "\n" + "image:" + imageshare + "\n" ,
Toast.LENGTH_LONG).show();
if(lightshare1.equals("on")){ //判断开关的状态,注意不是用“==”直接比较
mLightSwitch1.setChecked(true);
}
if(lightshare2.equals("on")){ //判断开关的状态
mLightSwitch2.setChecked(true);
}
if(imageshare.equals("on")){ //判断图片状态
mLight.setImageDrawable(getResources().getDrawable(R.drawable.light_on));
isChanged = true;
}
mLightSwitch1.setOnCheckedChangeListener(new OnCheckedChangeListener() { //监听开关1的动作
boolean lightSwitch1 = false;
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){
//开启mLightSwitch1
mTestView.setText(getString(R.string.light1_on));
lightSwitch1 = true;
} else {
//关闭mLightSwitch1
mTestView.setText(getString(R.string.light1_off));
lightSwitch1 = false;
}
if(lightSwitch1){
editor.putString("lightSwitch1", "on"); //存储配置 参数1 是key 参数2 是值
editor.commit();//提交刷新数据
} else {
editor.putString("lightSwitch1", "off");
editor.commit();
}
}
});
mLightSwitch2.setOnCheckedChangeListener(new OnCheckedChangeListener(){
boolean lightSwitch2 = false;
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){
if(isChecked){
//开启mLightSwitch2
mTestView.setText(getString(R.string.light2_on));
lightSwitch2 = true;
} else {
//关闭mLightSwitch2
mTestView.setText(getString(R.string.light2_off));
lightSwitch2 = false;
}
if(lightSwitch2){
editor.putString("lightSwitch2", "on"); //存储配置 参数1 是key 参数2 是值
editor.commit();//提交刷新数据
} else {
editor.putString("lightSwitch2", "off");
editor.commit();
}
}
});
mLight.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
if(v == mLight){
if(isChanged){ //若isChanged当前状态是true,灯亮,则点击图片后变为灯灭。
mLight.setImageDrawable(getResources().getDrawable(R.drawable.light_off));
mTestView.setText(getString(R.string.imageLight_off));
editor.putString("imageSwitch", "off"); //存储配置 参数1 是key 参数2 是值
editor.commit();//提交刷新数据
} else {
mLight.setImageDrawable(getResources().getDrawable(R.drawable.light_on));
mTestView.setText(getString(R.string.imageLight_on));
editor.putString("imageSwitch", "on");
editor.commit();
}
isChanged = !isChanged;
}
}
});
}
}
Main.xml:
<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"
tools:context="${relativePackage}.${activityClass}" >
<Switch
android:id="@+id/switch1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/switch2"
android:layout_alignParentTop="true"
android:layout_marginTop="45dp" />
<Switch
android:id="@+id/switch2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/switch1"
android:layout_marginRight="25dp"
android:layout_marginTop="45dp" />
<TextView
android:id="@+id/light1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/switch1"
android:layout_marginLeft="30dp"
android:text="@string/Light1" />
<TextView
android:id="@+id/light2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/light1"
android:layout_alignTop="@+id/switch2"
android:text="@string/Light2" />
<TextView
android:id="@+id/test1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/NULL" />
<TextView
android:id="@+id/light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="290dp"
android:text="@string/Light" />
<ImageView
android:id="@+id/lightShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/switch2"
android:layout_below="@+id/test1"
android:layout_marginRight="26dp"
android:layout_marginTop="28dp"
android:src="@drawable/light_off" />
</RelativeLayout>
String.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">myapp</string>
<string name="hello_world">Hello world!</string>
<string name="Light1">Light1</string>
<string name="Light2">Light2</string>
<string name="Light">Light</string>
<string name="light1_on">light1 is on.</string>
<string name="light1_off">light1 is off.</string>
<string name="light2_on">light2 is on.</string>
<string name="light2_off">light2 is off.</string>
<string name="imageLight_on">imageLight is on.</string>
<string name="imageLight_off">imageLight is off.</string>
<string name="NULL">NULL</string>
</resources>
3.16完成情况:
3.17完成情况:
主要是完成了配置信息(即开关状态)的存储。
使用HTML5开发客户端应用:
Html5代码:
<!DOCTYPE
html>
<html>
<head>
<meta
charset="utf-8" />
<title>毕业设计</title>
</head>
<body>
<p>light1:<img
src="img/light_off.png" onclick="change(this)"/></p>
</body>
<script
type="text/javascript">
var ischecked =
false;
function
change(obj){
if(ischecked){
obj.src="img/light_off.png";
}
else {
obj.src="img/light_on.png";
}
ischecked
= !ischecked;
}
</script>
</html>
2015/03/17 星期二
完成开关和图片开关的配置信息保存,主要使用SharedPreferences。
除了SQLite数据库外,SharedPreferences也是一种轻型的数据存储方式,它的本质是基于XML文件存储key-value键值对数据,通常用来存储一些简单的配置信息。其存储位置在/data/data/<包名>/shared_prefs目录下。SharedPreferences对象本身只能获取数据而不支持存储和修改,存储修改是通过Editor对象实现。实现SharedPreferences存储的步骤如下:
一、根据Context获取SharedPreferences对象
二、利用edit()方法获取Editor对象。
三、通过Editor对象存储key-value键值对数据。
四、通过commit()方法提交数据。
sadfa的更多相关文章
- Linux shell入门基础(二)
二.shell对文本的操作 01.查看文本的命令 #cat /etc/passwd(并非对文本文件操作) #tail -5 /etc/passwd(查看末尾5行) #tail -f /var/log/ ...
- python_11 装饰器,闭包
装饰器:本质就是函数,功能是为其他函数添加附加功能 原则: 1.不修改被修饰函数的源代码 2.不修改被修饰函数的调用方式 装饰器的知识储备 装饰器=高阶函数+函数嵌套+闭包 #装饰器: def tim ...
- flask框架--cookie,session
今天我又给大家分享一下怎么用flask框架来实现像淘宝购物车一样存储数据,并且把存储的数据删除,这个方法可以用两个方法都可以做成,一个是cookie,另一个是session. session是依赖于c ...
- python-day16
一.正则表达式 regular expression -----regex 验证匹配正则表达式使用单个字符串来描述.匹配一系列匹配某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替 ...
随机推荐
- 在阿里云服务器ubuntu14.04运行netcore
从netcore1.0正式发布就很激动,想要赶紧学习. 最近博客园的一篇文章给了完整的指导非常感谢,但是在实际实现到发布到阿里云服务器遇到一些问题,记录下来. 首先上基础文章http://www.cn ...
- You have new mail in /var/spool/mail/root 烦不烦你(转)
转自(http://blog.csdn.net/yx_l128125/article/details/7425182) 有时在进入系统的时候经常提示You have new mail in /var/ ...
- [C#]Task异步操作
1.代码示例 using System; using System.Threading; using System.Threading.Tasks; namespace ConsoleApplicat ...
- mui H5 js动态添加不同类型的数据
html页面需要添加的页面的数据格式 <ul class="mui-table-view" id="OA_task_1"> <li class ...
- Sublime Text 3 LESS、SASS、SCSS高亮插件、提示插件
为sublime text 添加LESS语法高亮 功能:LESS高亮插件 下载 https://packagecontrol.io/packages/LESS 简介:用LESS的同学都知道,s ...
- HTML 5 video 视频标签全属性详解
Video标签的使用 Video标签含有src.poster.preload.autoplay.loop.controls.width.height等几个属性, 以及一个内部使用的标签<sour ...
- c# datagridview导出到excel【转载】
c# datagridview导出到excel[转载] http://hi.baidu.com/weizier/blog/item/8212caea1123b4d6d439c9fe.html 本作者使 ...
- zzuli oj 1167逆转数(指针专题)
Description 任意给你一个整数,这个数可能很大(最长不超过100位),你能求出它的逆转数吗? 逆转数定义如下: 1.一个末尾没有0的整数,它的逆转数就是各位数字逆序输出: 2.一个负数 ...
- vs2013调试崩溃,重启电脑依旧崩溃
如果大家遇到 VS断点调试程序崩溃的问题,可以排查是不是有这个问题 VSx新安装了插件 点击工具---扩展和更新 禁用最新安装的程序 一般就没有问题了
- ▲历史回眸--abbr和acronym的渊源
网景和微软的浏览器之战早已淡去多年,最终以微软的IE浏览器胜出,特别是IE6的出现,一度成为世界浏览器的霸主,至今无人能敌.去年IE6荣获“终身成就奖”,真是实至名归.本文涉及的两个标签abbr和ac ...