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 验证匹配正则表达式使用单个字符串来描述.匹配一系列匹配某个句法规则的字符串.在很多文本编辑器里,正则表达式通常被用来检索.替 ...
随机推荐
- 菜鸟的MySQL学习笔记(四)
MySQL中的运算符和函数: 1.字符函数: 2.数值运算符与函数: 3.比较运算符与函数: 4.日期时间函数: 5.信息函数: 6.聚合函数: 7.加密函数等: 6-1.字符函数: CONCAT ...
- vim 配置文件 ,高亮+自动缩进+行号+折叠+优化
vim 配置文件 ,高亮+自动缩进+行号+折叠+优化 将一下代码copy到 用户目录下 新建文件为 .vimrc保存即可生效: 如果想所有用户生效 请修改 /etc/vimrc (建议先cp一份)& ...
- Fedora 21 安装QQ国际版
首先安装依赖包 sudo yum install freetype.i686 libpng.i686 libgcc.i686 libXau.i686 点击下载wine-2012qq国际版 unzip ...
- javascript 写农场迭代
/** * 农场一头小母牛 * 每年生头小母牛 * 母牛五岁产母牛 * 二十年上多少牛 */ 划分程序,母牛只管自己的年龄能不能产牛仔,母牛是model同时也是工厂 农场只管养牛,收获新牛. 一年一个 ...
- HTML新元素
<canvas> 标签定义图形,比如图表和其他图像.该标签基于 JavaScript 的绘图 API <audio> 定义音频内容 <video> 定义视频(vid ...
- Linux Makefile analysis for plain usr
一.本文主旨 笔者写了一篇linux内核Makefile整体分析 ,测重于理论分析,对于实际应用不算对头,所以需要写一篇实用性较强的文章,为以后内核.驱动移植做好铺垫. 二.本文内容概要 1.编译哪些 ...
- POJ 3122 Pie 二分枚举
题目:http://poj.org/problem?id=3122 这个题就好多了,没有恶心的精度问题,所以1A了.. #include <stdio.h> #include <ma ...
- SDUT 1305 查找基因序列问题 dp
题目: http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1305 这个题就是一个类似公共子串的dp ...
- iOS开发 - NSBundle, NSDevice, NSLocale
iOS的APP的应用开发的过程中,有时为了bug跟踪或者获取用反馈的需要自动收集用户设备.系统信息.应用信息等等,这些信息方便开发者诊断问题,当然这些信息是用户的非隐私信息,是通过开发api可以获取到 ...
- iOS Xcode制作模板类-b
为什么要定义模板类 遵守代码规范可以提高代码可读性, 降低后期维护成本. 当我们定下了一个团队都认同的代码规范, 如我们要求所有的viewController的代码都得按照下面来组织: #pragma ...