美文网首页开源项目Android开发
做一款自己的安卓天气闹钟(3)——用Switch控制元素的显示隐

做一款自己的安卓天气闹钟(3)——用Switch控制元素的显示隐

作者: 血之君殇 | 来源:发表于2017-11-21 11:36 被阅读39次

先看界面

image.png

知识点

  1. include标签
    include标签是为了方便界面的复用,比如今天的界面最上边的界面明显是第一篇中的布局文件,我们可以直接把它用include引用过来,而不必再重新写一次布局
  2. Switch(开关)
    很简单,就是一个开关按钮,也没有太多的知识点
  3. Spinner(列表选项框)
    这是一个下拉菜单,也比较简单
  4. visibility(显示隐藏属性)
    VISIBLE:设置控件可见
    INVISIBLE:设置控件不可见
    GONE:设置控件隐藏
    而INVISIBLE和GONE的主要区别是:当控件visibility属性为INVISIBLE时,界面保留了view控件所占有的空间;而控件属性为GONE时,界面则不保留view控件所占有的空间

开始写代码

  1. 界面文件config_show.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1">

            <include layout="@layout/content_main" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="2"
            android:orientation="vertical">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示时间"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_time"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:shadowColor="#00E266"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示日期"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示星期"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_week"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示天气地点"
                    android:textSize="16sp" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="北京市 海淀区" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示当天天气"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_weather1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示第二天天气"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_weather2"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示第三天天气"
                    android:textSize="16sp" />

                <Switch
                    android:id="@+id/switch_weather3"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:checked="true"
                    android:textOff=""
                    android:textOn="" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="1">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentStart="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:text="显示闹钟组数"
                    android:textSize="16sp" />

                <Spinner
                    android:id="@+id/spinner1"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentEnd="true"
                    android:layout_alignParentRight="true"
                    android:layout_centerInParent="true"
                    android:layout_margin="8sp"
                    android:entries="@array/clock_num"
                    android:spinnerMode="dialog" />
            </RelativeLayout>
        </LinearLayout>
    </LinearLayout>
</android.support.constraint.ConstraintLayout>

很简单的布局文件,暂时没有把相同的属性提取到style文件中,等时间充裕了再来修改
将每个switch都设置为默认选中状态

android:checked="true"
  1. 给content_main.xml中相关的标签绑定id,好通过switch控制显示与否
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/home"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="love.xzjs.t_android.MainActivity"
    tools:showIn="@layout/activity_main">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#000"
        android:orientation="horizontal">

        <RelativeLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="8">

            <LinearLayout
                android:layout_width="wrap_content"
                android:layout_height="wrap_content">

                <TextView
                    android:id="@+id/date"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginRight="8sp"
                    android:text="2017年10月23日"
                    android:textColor="#fff"
                    android:textSize="18sp" />

                <TextView
                    android:id="@+id/week"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="周一"
                    android:textColor="#fff"
                    android:textSize="18sp" />
            </LinearLayout>

            <TextView
                android:id="@+id/time_label"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_centerVertical="true"
                android:gravity="center"
                android:text="09:08"
                android:textColor="#fff"
                android:textSize="150sp" />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="28dp"
                android:layout_alignParentBottom="true">

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        app:srcCompat="@mipmap/clock" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="left"
                        android:text="12:00"
                        android:textColor="#fff" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        app:srcCompat="@mipmap/clock" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="left"
                        android:text="12:00"
                        android:textColor="#fff" />
                </LinearLayout>

                <LinearLayout
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1">

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        app:srcCompat="@mipmap/clock" />

                    <TextView
                        android:layout_width="0dp"
                        android:layout_height="wrap_content"
                        android:layout_weight="2"
                        android:gravity="left"
                        android:text="12:00"
                        android:textColor="#fff" />
                </LinearLayout>
            </LinearLayout>
        </RelativeLayout>

        <LinearLayout
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="5"
            android:orientation="vertical">

            <LinearLayout
                android:id="@+id/weather_today"
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="5"
                android:orientation="vertical">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="5">

                    <ImageView
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="5"
                        app:srcCompat="@mipmap/yu" />

                    <RelativeLayout
                        android:layout_width="0dp"
                        android:layout_height="match_parent"
                        android:layout_weight="2">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentBottom="true"
                            android:gravity="center"
                            android:text="阵雨 27℃"
                            android:textColor="#fff"
                            android:textSize="18sp" />
                    </RelativeLayout>
                </LinearLayout>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="0dp"
                    android:layout_weight="2">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:gravity="center"
                        android:text="阴转阵雨 北风4-5级 14-27℃"
                        android:textColor="#fff"
                        android:textSize="18sp" />
                </LinearLayout>
            </LinearLayout>

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="0dp"
                android:layout_weight="2">

                <LinearLayout
                    android:id="@+id/weather_tomorrow"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="5"
                        app:srcCompat="@mipmap/qing" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:text="明天 晴 19-29℃"
                        android:textColor="#fff" />
                </LinearLayout>

                <LinearLayout
                    android:id="@+id/weather_after_tomorrow"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="1"
                    android:orientation="vertical">

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="5"
                        app:srcCompat="@mipmap/leizhenyu" />

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="0dp"
                        android:layout_weight="2"
                        android:gravity="center"
                        android:text="后天 雷雨 19-29℃"
                        android:textColor="#fff" />
                </LinearLayout>
            </LinearLayout>

        </LinearLayout>
    </LinearLayout>

</android.support.constraint.ConstraintLayout>

  1. 创建show.java,用来当现实设置页
package love.xzjs.t_android;

import android.content.res.Resources;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.Switch;
import android.widget.TextView;



public class Show extends Fragment implements CompoundButton.OnCheckedChangeListener {
    private TextView textViewTime, textViewDate, textViewWeek;
    private LinearLayout linearLayout1,linearLayout2,linearLayout3;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.config_show, container, false);

        textViewTime = (TextView) view.findViewById(R.id.time_label);
        textViewDate = (TextView)view.findViewById(R.id.date);
        textViewWeek = (TextView)view.findViewById(R.id.week);
        linearLayout1=(LinearLayout)view.findViewById(R.id.weather_today);
        linearLayout2=(LinearLayout)view.findViewById(R.id.weather_tomorrow);
        linearLayout3=(LinearLayout)view.findViewById(R.id.weather_after_tomorrow);

        //根据屏幕宽度确定字体的大小
        Resources resources = this.getResources();
        DisplayMetrics dm = resources.getDisplayMetrics();
        int width = dm.widthPixels;
        textViewTime.setTextSize(TypedValue.COMPLEX_UNIT_SP, 150 * width / 1794);

        //绑定switch事件
        Switch switchTime = (Switch) view.findViewById(R.id.switch_time);
        Switch switchDate = (Switch) view.findViewById(R.id.switch_date);
        Switch switchWeek = (Switch) view.findViewById(R.id.switch_week);
        Switch switchWeather1=(Switch)view.findViewById(R.id.switch_weather1);
        Switch switchWeather2=(Switch)view.findViewById(R.id.switch_weather2);
        Switch switchWeather3=(Switch)view.findViewById(R.id.switch_weather3);
        switchTime.setOnCheckedChangeListener(this);
        switchDate.setOnCheckedChangeListener(this);
        switchWeek.setOnCheckedChangeListener(this);
        switchWeather1.setOnCheckedChangeListener(this);
        switchWeather2.setOnCheckedChangeListener(this);
        switchWeather3.setOnCheckedChangeListener(this);

        return view;
    }

    /**
     * 重写switch状态变更事件
     * @param compoundButton
     * @param b
     */
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
        switch (compoundButton.getId()) {
            case R.id.switch_time:
                textViewTime.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
            case R.id.switch_date:
                textViewDate.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
            case R.id.switch_week:
                textViewWeek.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
            case R.id.switch_weather1:
                linearLayout1.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
            case R.id.switch_weather2:
                linearLayout2.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
            case R.id.switch_weather3:
                linearLayout3.setVisibility(compoundButton.isChecked()?View.VISIBLE:View.GONE);
                break;
        }
    }
}

结果演示

deo.gif

代码地址

https://github.com/xzjs/t_android

相关文章

网友评论

本文标题:做一款自己的安卓天气闹钟(3)——用Switch控制元素的显示隐

本文链接:https://www.haomeiwen.com/subject/evxbvxtx.html