Android——图片视图(ImageView)状态开关按钮(ToggleButton)时钟图片透明度滚动和时间选择器

Posted 让心开始

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android——图片视图(ImageView)状态开关按钮(ToggleButton)时钟图片透明度滚动和时间选择器相关的知识,希望对你有一定的参考价值。

activity_ui1.xml

dth="wrap_content"
        android:layout_height="wrap_content"
        android:textOn=" "
        android:textOff=" "
        android:background="@drawable/qq"
        android:id="@+id/tob1"/>


    <AnalogClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <DigitalClock
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <DatePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:startYear="2000"
        android:endYear="2016"
        android:minDate="1/1/2000"
        android:maxDate="12/31/2016"
        android:id="@+id/dp_1"
        android:calendarViewShown="false"
        android:spinnersShown="false">
        <!--startYear选择开始年
        endYear   选择结束年
        minDate  开始时间
        maxDate  结束时间
        calendarViewShown  日历是否显示 只对版本4 起作用
        spinnersShown   是否下拉
        -->

    </DatePicker>
    <TimePicker
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tp_1">

    </TimePicker>
    <!--AnalogClock时钟表盘
    DigitalClock  数字时钟
    DatePicker 日期选择器
    TimePicker  时间选择器
    CalendarView  显示日历  版本4  不支持
    -->
    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:firstDayOfWeek="1">

    </CalendarView>

</LinearLayout>

</ScrollView>

在drawable目录中的qq.xml

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

    <item android:drawable="@drawable/on" android:state_checked="true"/>
    <item android:drawable="@drawable/off" android:state_checked="false"/>
    </selector>

UIActivity1.java

package com.hanqi.application3;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Switch;
import android.widget.TimePicker;
import android.widget.Toast;
import android.widget.ToggleButton;

import static android.widget.CompoundButton.*;

public class UIActivity1 extends Activity {
    ImageView iv;

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

        RadioGroup radioGroup = (RadioGroup)findViewById(R.id.rb);

        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {

                if (checkedId == R.id.rb3) {
                    Toast.makeText(UIActivity1.this, "选对了", Toast.LENGTH_SHORT).show();
                }
                RadioButton rb = (RadioButton) findViewById(checkedId);

                Toast.makeText(UIActivity1.this, rb.getText(), Toast.LENGTH_SHORT).show();
            }


        });

        CheckBox cb_st= (CheckBox)findViewById(R.id.cb_st);
        cb_st.setOnCheckedChangeListener(new CBOnCheckChangeListenter());
        CheckBox cb_xt= (CheckBox)findViewById(R.id.cb_xt);
        cb_xt.setOnCheckedChangeListener(new CBOnCheckChangeListenter());
        CheckBox cb_jc= (CheckBox)findViewById(R.id.cb_jc);
        cb_jc.setOnCheckedChangeListener(new CBOnCheckChangeListenter());
        CheckBox cb_xhx= (CheckBox)findViewById(R.id.cb_xhx);
        cb_xhx.setOnCheckedChangeListener(new CBOnCheckChangeListenter());

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

        ToggleButton tob = (ToggleButton)findViewById(R.id.tob);
        tob.setOnCheckedChangeListener(new TOncheckchangeLitenter() );
        Switch sw = (Switch)findViewById(R.id.sw);
        //初始化
        DatePicker dp_1= (DatePicker)findViewById(R.id.dp_1);
        //月份是从0开始的
        dp_1.init(2000, 1, 1, new DatePicker.OnDateChangedListener() {
            @Override
            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth) {

                Toast.makeText(UIActivity1.this, year+"-"+(monthOfYear+1)+"-"+dayOfMonth, Toast.LENGTH_SHORT).show();

            }
        });
        TimePicker tp_1= (TimePicker)findViewById(R.id.tp_1);

        Toast.makeText(UIActivity1.this, dp_1.getYear()+"-"+(dp_1.getMonth()+1)+"-"+dp_1.getDayOfMonth()
                +" "+tp_1.getHour()+":"+tp_1.getMinute(), Toast.LENGTH_SHORT).show();

        tp_1.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {

                Toast.makeText(UIActivity1.this, hourOfDay+":"+minute, Toast.LENGTH_SHORT).show();

            }
        });

    }

    private class TOncheckchangeLitenter implements OnCheckedChangeListener
    {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            if (isChecked)
            {
                iv.setImageResource(R.drawable.open);
            }
            else
            {
                iv.setImageResource(R.drawable.close);
            }
        }
    }



    private class CBOnCheckChangeListenter implements OnCheckedChangeListener
    {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

            CheckBox cb = (CheckBox)buttonView;
            if (isChecked) {
                Toast.makeText(UIActivity1.this, "选中了", Toast.LENGTH_SHORT).show();
            }
            else
            {
                Toast.makeText(UIActivity1.this, "取消了", Toast.LENGTH_SHORT).show();
            }
        }
    }
}

 

以上是关于Android——图片视图(ImageView)状态开关按钮(ToggleButton)时钟图片透明度滚动和时间选择器的主要内容,如果未能解决你的问题,请参考以下文章

Android从零单排系列九《Android视图控件——ImageView》

Android从零单排系列九《Android视图控件——ImageView》

Android从零单排系列九《Android视图控件——ImageView》

Android ImageView视图的七种图片缩放类型

Android——图片视图(ImageView)状态开关按钮(ToggleButton)时钟图片透明度滚动和时间选择器

图像视图-ImageView