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 + "
" + "light2:" + lightshare2 + "
" + "image:" + imageshare + "
" ,

        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()方法提交数据。

原文地址:https://www.cnblogs.com/fansen/p/4342379.html