码迷,mamicode.com
首页 > 其他好文 > 详细

sadfa

时间:2015-03-16 19:08:09      阅读:169      评论:0      收藏:0      [点我收藏+]

标签:

2015/03/16         星期一

在Android平台下编写客户端程序,界面只有开关若干个。

MainActivity.java代码:

package com.fan.myapp;

 

import android.app.Activity;

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;

 

public class MainActivity extends Activity {

       private TextView mTestView;

       private Switch mLightSwitch1;

       private Switch mLightSwitch2;

       private ImageView mLight;

       boolean isChanged = false;

 

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

       

        mTestView = (TextView)findViewById(R.id.test1);

        mLight = (ImageView)findViewById(R.id.lightShow);

       

        //设置图片开关

        mLight.setOnClickListener(new View.OnClickListener(){

              @Override

              public void onClick(View v){

                     if(v == mLight){

                           if(isChanged){

                                  mLight.setImageDrawable(getResources().getDrawable(R.drawable.light_off));

                           } else {

                                  mLight.setImageDrawable(getResources().getDrawable(R.drawable.light_on));

                           }

                           isChanged = !isChanged;

                     }

              }

        });

       

        //设置Switch开关

        mLightSwitch1 = (Switch)findViewById(R.id.switch1);

        mLightSwitch2 = (Switch)findViewById(R.id.switch2);

        mLightSwitch1.setOnCheckedChangeListener(new OnCheckedChangeListener() {

             

              @Override

              public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){

                     if(isChecked){

                           //开启mLightSwitch1

                           mTestView.setText(getString(R.string.light1_on));

                     } else {

                           //关闭mLightSwitch1

                           mTestView.setText(getString(R.string.light1_off));

                     }

              }

        });

        mLightSwitch2.setOnCheckedChangeListener(new OnCheckedChangeListener(){

             

              @Override

              public void onCheckedChanged(CompoundButton buttonView,boolean isChecked){

                     if(isChecked){

                           //开启mLightSwitch2

                           mTestView.setText(getString(R.string.light2_on));

                     } else {

                           //关闭mLightSwitch2

                           mTestView.setText(getString(R.string.light2_off));

                     }

              }

             

        });

            

    }

}

 

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>

strings.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="NULL">NULL</string>

 

</resources>

 

 

      

 

 

 

使用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>

 

  

sadfa

标签:

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!