Android 设置界面appSetActivity

自定义开关按钮:
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



    
        
        
        
            
            
        
    

开关布局:




    
    
    

    
    

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布局:




    

    

    

        

        

        
    


    

        

        

        
    

    

        

        

        
    

    

    

    

        

        

        
    

    

    

    

        

        
    

    

        

        
    


    

        

        
    

    

        

        
    

    

        

        
    

效果图:


2018-05-28 15_14_10.gif

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

你可能感兴趣的:(Android 设置界面appSetActivity)