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;@Overrideprotected 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; @Overridepublic 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;@Overridepublic 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(){@Overridepublic 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}" ><Switchandroid: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" /><Switchandroid: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" /> <TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><TextViewandroid: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" /><ImageViewandroid: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" οnclick="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()方法提交數據。
轉載于:https://www.cnblogs.com/fansen/p/4342379.html
總結
- 上一篇: salient object detec
- 下一篇: spring 事务提交成功后,再去发送事