我爱编程

Android 设置界面appSetActivity

2018-05-28  本文已影响0人  蒙伟

自定义开关按钮:
SwitchButton.java

package com.huatec.myapplication;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;

/*
 * 开关  自定义button
 */
public class SwitchButton extends FrameLayout {

    private ImageView openImage;
    private ImageView closeImage;

    public SwitchButton(Context context) {
        this(context, null);
    }

    public SwitchButton(Context context, AttributeSet attrs, int defStyleAttr) {
        this(context, attrs);
    }

    public SwitchButton(Context context, AttributeSet attrs) {
        super(context, attrs);

        //通过调用obtainStyledAttributes方法来获取一个TypeArray,然后由该TypeArray来对属性进行设置
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SwitchButton);
        //取出打开的图片
        Drawable openDrawable = typedArray.getDrawable(R.styleable.SwitchButton_switchOpenImage);
        //取出关闭的图片
        Drawable closeDrawable = typedArray.getDrawable(R.styleable.SwitchButton_switchCloseImage);

        int switchStatus = typedArray.getInt(R.styleable.SwitchButton_switchStatus, 0);
        //调用结束后务必调用recycle()方法,否则这次的设定会对下次的使用造成影响
        typedArray.recycle();


        LayoutInflater.from(context).inflate(R.layout.switch_button, this);//添加到父控件
        openImage = (ImageView) findViewById(R.id.iv_switch_open);
        closeImage = (ImageView) findViewById(R.id.iv_switch_close);

        if (openDrawable != null) {
            openImage.setImageDrawable(openDrawable);
        }
        if (closeDrawable != null) {
            closeImage.setImageDrawable(closeDrawable);
        }
        if (switchStatus == 1) {
            closeSwitch();
        }
    }

    /**
     * 开关是否为打开状态
     */
    public boolean isSwitchOpen() {
        return openImage.getVisibility() == View.VISIBLE;
    }

    /**
     * 打开开关
     */
    public void openSwitch() {
        openImage.setVisibility(View.VISIBLE);  //显示打开的图片
        closeImage.setVisibility(View.INVISIBLE);  //隐藏关闭的图片
    }
    /**
     * 关闭开关
     */
    public void closeSwitch() {
        openImage.setVisibility(View.INVISIBLE);//隐藏打开的图片
        closeImage.setVisibility(View.VISIBLE);  //显示关闭的图片
    }

}

values/attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="SwitchButton">
        <attr name="switchOpenImage" format="reference"/>
        <attr name="switchCloseImage" format="reference"/>
        <attr name="switchStatus">
            <enum name="open" value="0"/>
            <enum name="close" value="1"/>
        </attr>
    </declare-styleable>
</resources>

开关布局:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <!--将图片放在mipmap不会拉伸-->
    <!--打开的图片-->
    <ImageView
        android:id="@+id/iv_switch_open"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ease_open_icon"
        android:visibility="visible" />

    <!--关闭的图片-->
    <ImageView
        android:id="@+id/iv_switch_close"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:background="@mipmap/ease_close_icon"
        android:visibility="invisible" />
</FrameLayout>

SetAvtivity.java

package com.huatec.myapplication;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;


public class SetAvtivity extends AppCompatActivity implements View.OnClickListener {

    private SwitchButton switch_accept_news, switch_sound, switch_shock, switch_loundspeaker;
    private LinearLayout ll_sound, ll_shock;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_set);

        //接收消息通知开关
        switch_accept_news = (SwitchButton) findViewById(R.id.switch_accept_news);
        //声音开关
        switch_sound = (SwitchButton) findViewById(R.id.switch_sound);
        //震动开关
        switch_shock = (SwitchButton) findViewById(R.id.switch_shock);
        //使用扬声器播放语音
        switch_loundspeaker = (SwitchButton) findViewById(R.id.switch_loundspeaker);

        //声音item布局
        ll_sound = (LinearLayout) findViewById(R.id.ll_sound);
        //震动item布局
        ll_shock = (LinearLayout) findViewById(R.id.ll_shock);

        //开关布局
        switch_accept_news.setOnClickListener(this);
        switch_sound.setOnClickListener(this);
        switch_shock.setOnClickListener(this);
        switch_loundspeaker.setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            //声音开关
            case R.id.switch_accept_news:

                if (switch_accept_news.isSwitchOpen()) {//开关为打开状态
                    //关闭逻辑
                    Toast.makeText(getApplicationContext(),"关闭了",Toast.LENGTH_SHORT).show();
                    switch_accept_news.closeSwitch();
                    ll_sound.setVisibility(View.GONE);
                    ll_shock.setVisibility(View.GONE);
                } else {
                    //打开逻辑
                    Toast.makeText(getApplicationContext(),"打开了",Toast.LENGTH_SHORT).show();
                    switch_accept_news.openSwitch();
                    ll_sound.setVisibility(View.VISIBLE);
                    ll_shock.setVisibility(View.VISIBLE);
                }
                break;
            case R.id.switch_sound:
                if (switch_sound.isSwitchOpen()) {
                    switch_sound.closeSwitch();
                } else {
                    switch_sound.openSwitch();
                }
                break;
            case R.id.switch_shock:
                if (switch_shock.isSwitchOpen()) {
                    switch_shock.closeSwitch();
                } else {
                    switch_shock.openSwitch();
                }
                break;
            case R.id.switch_loundspeaker:
                if (switch_loundspeaker.isSwitchOpen()) {
                    switch_loundspeaker.closeSwitch();
                } else {
                    switch_loundspeaker.openSwitch();
                }
                break;
        }
    }
}

activity_set布局:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              xmlns:app="http://schemas.android.com/apk/res-auto"
              android:orientation="vertical">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:background="@android:color/holo_blue_light"
        android:gravity="center"
        android:text="设置"
        android:textColor="@android:color/white"
        android:textSize="20sp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dip"
        android:gravity="bottom|left"
        android:paddingLeft="10dip"
        android:text="@string/news_notice"
        android:textColor="@android:color/darker_gray"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="3dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:padding="10dp"
            android:text="@string/accept_news"
            android:textSize="13sp"/>

        <com.huatec.myapplication.SwitchButton
            android:id="@+id/switch_accept_news"
            android:layout_width="wrap_content"
            app:switchStatus="open"
            android:layout_height="28dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="15dp">

        </com.huatec.myapplication.SwitchButton>
    </LinearLayout>


    <LinearLayout
        android:id="@+id/ll_sound"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="@string/sound"
            android:textSize="13sp"/>

        <com.huatec.myapplication.SwitchButton
            android:id="@+id/switch_sound"
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="15dp">

        </com.huatec.myapplication.SwitchButton>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/ll_shock"
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="@string/shock"
            android:textSize="13sp"/>

        <com.huatec.myapplication.SwitchButton
            android:id="@+id/switch_shock"
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="15dp">

        </com.huatec.myapplication.SwitchButton>
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="4dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="bottom|left"
        android:paddingLeft="10dp"
        android:text="@string/chat"
        android:textColor="@android:color/darker_gray"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="3dp"
        android:background="@android:color/white"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="40dp"
            android:layout_weight="1"
            android:background="@android:color/white"
            android:padding="10dp"
            android:text="@string/loundspeaker"
            android:textSize="13sp"/>

        <com.huatec.myapplication.SwitchButton
            android:id="@+id/switch_loundspeaker"
            android:layout_width="wrap_content"
            android:layout_height="28dp"
            android:layout_gravity="center_vertical"
            android:layout_marginRight="15dp">

        </com.huatec.myapplication.SwitchButton>
    </LinearLayout>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="4dp"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="bottom|left"
        android:paddingLeft="10dp"
        android:text="自定义设置"
        android:textColor="@android:color/darker_gray"/>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="闹钟铃声"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableRight="@mipmap/right"
            android:text="爱要坦荡荡"
            android:textColor="@android:color/darker_gray"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical"
        >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="电话铃声"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableRight="@mipmap/right"
            android:text="壁咚"
            android:textColor="@android:color/darker_gray"/>
    </RelativeLayout>


    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="短信铃声"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableRight="@mipmap/right"
            android:text="Fadeln"
            android:textColor="@android:color/darker_gray"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="个性化设置"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableRight="@mipmap/right"
            android:textColor="@android:color/darker_gray"/>
    </RelativeLayout>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="40dp"
        android:layout_marginTop="1dp"
        android:background="@android:color/white"
        android:gravity="center_vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="亮度设置"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_marginRight="10dp"
            android:drawableRight="@mipmap/right"
            android:textColor="@android:color/darker_gray"/>
    </RelativeLayout>
</LinearLayout>

效果图:


2018-05-28 15_14_10.gif

源码地址:https://github.com/280357392/appSetActivity

上一篇下一篇

猜你喜欢

热点阅读