溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點(diǎn)擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

Android 實(shí)現(xiàn)開關(guān)燈效果

發(fā)布時(shí)間:2020-06-22 13:31:39 來源:網(wǎng)絡(luò) 閱讀:1708 作者:taiyi928 欄目:移動開發(fā)

Android  實(shí)現(xiàn)開關(guān)燈效果

Android  實(shí)現(xiàn)開關(guān)燈效果



activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:id="@+id/linear_1"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    android:orientation="horizontal" >



    <ImageView

        android:id="@+id/p_w_picpath_1"

        android:layout_width="150dp"

        android:layout_height="150dp"

        android:contentDescription="@string/deng"

        android:src="@drawable/bulb_off"

        android:text="@string/hello_world" />


    <LinearLayout

        android:id="@+id/linear_2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:orientation="vertical"

        android:paddingLeft="10dip"

        android:paddingTop="10dip" >


        <ToggleButton

            android:id="@+id/toggle"

            android:layout_width="140dip"

            android:layout_height="wrap_content"

            android:textOff="@string/off"

            android:textOn="@string/on" />


        <RadioGroup

            android:id="@+id/radio_1"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:orientation="horizontal" >


            <RadioButton

                android:id="@+id/button_off"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:checked="true"

                android:text="@string/off" />


            <RadioButton

                android:id="@+id/button_on"

                android:layout_width="wrap_content"

                android:layout_height="wrap_content"

                android:text="@string/on" />

        </RadioGroup>


        <CheckBox

            android:id="@+id/check"

            android:layout_width="wrap_content"

            android:layout_height="wrap_content"

            android:text="@string/on" />

    </LinearLayout>


</LinearLayout>



string.xml


<?xml version="1.0" encoding="utf-8"?>

<resources>


    <string name="app_name">ToggleButton</string>

    <string name="action_settings">Settings</string>

    <string name="hello_world">Hello world!</string>

    <string name="deng">燈</string>

    <string name="on">開燈</string>

    <string name="off">關(guān)燈</string>


</resources>




MainActivity.java



package com.malakana.togglebutton;


import android.os.Bundle;

import android.widget.CheckBox;

import android.widget.CompoundButton;

import android.widget.CompoundButton.OnCheckedChangeListener;

import android.widget.ImageView;

import android.widget.RadioButton;

import android.widget.ToggleButton;

import android.app.Activity;


public class MainActivity extends Activity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);


ToggleButton tb = (ToggleButton) findViewById(R.id.toggle);

tb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(arg1);

}

});


CheckBox cb = (CheckBox) this.findViewById(R.id.check);

cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(arg1);

}


});

RadioButton rb = (RadioButton) findViewById(R.id.button_off);

rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {


@Override

public void onCheckedChanged(CompoundButton arg0, boolean arg1) {

// TODO Auto-generated method stub

setBulbState(!arg1);

}


});

}


public void setBulbState(boolean state) {

// TODO Auto-generated method stub

ImageView iv = (ImageView) findViewById(R.id.p_w_picpath_1);

iv.setImageResource((state) ? R.drawable.bulb_on : R.drawable.bulb_off);


ToggleButton tb = (ToggleButton) findViewById(R.id.toggle);

tb.setChecked(state);


CheckBox cb = (CheckBox) findViewById(R.id.check);

//cb.setText((state) ? R.string.on : R.string.off);

cb.setChecked(state);


RadioButton rb = (RadioButton) findViewById(R.id.button_off);

rb.setChecked(!state);

rb = (RadioButton) findViewById(R.id.button_on);

rb.setChecked(state);


}


}


向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI