zoukankan      html  css  js  c++  java
  • 3月2日学习日志

    今天学习了android studio中的开关按钮。

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:drawable="@drawable/switch_btn_pressed"/>
        <item android:state_pressed="false" android:drawable="@drawable/switch_btn_normal"/>
    </selector>
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:drawable="@drawable/switch_btn_bg_green"/>
        <item android:state_checked="false" android:drawable="@drawable/switch_btn_bg_white"/>
    </selector>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">
    
        <ToggleButton
            android:id="@+id/tbtn_open"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:textOff="关闭声音"
            android:textOn="打开声音" />
    
        <Switch
            android:id="@+id/swh_status"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textOff=""
            android:textOn=""
            android:thumb="@drawable/thumb_selctor"
            android:track="@drawable/track_selctor" />
    
    </LinearLayout>
    public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{
    
        private ToggleButton tbtn_open;
        private Switch swh_status;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            tbtn_open = (ToggleButton) findViewById(R.id.tbtn_open);
            swh_status = (Switch) findViewById(R.id.swh_status);
            tbtn_open.setOnCheckedChangeListener(this);
            swh_status.setOnCheckedChangeListener(this);
        }
    
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
            switch (compoundButton.getId()){
                case R.id.tbtn_open:
                    if(compoundButton.isChecked()) Toast.makeText(this,"打开声音",Toast.LENGTH_SHORT).show();
                    else Toast.makeText(this,"打开声音",Toast.LENGTH_SHORT).show();
                    break;
                case R.id.swh_status:
                    if(compoundButton.isChecked()) Toast.makeText(this,"开关:ON",Toast.LENGTH_SHORT).show();
                    else Toast.makeText(this,"开关:OFF",Toast.LENGTH_SHORT).show();
                    break;
    
            }
        }
    }
  • 相关阅读:
    Mysql主从复制(基于Log)
    Linux系统开机启动流程
    JS的 验证组织机构的合法性
    Linux以下基于TCP多线程聊天室(client)
    浅谈Java集合框架
    疯狂Java学习笔记(72)-----------大话程序猿面试
    Android自己定义View之组合控件 ---- LED数字时钟
    C/C++学习:函数指针
    springmvc+spring+jpa(hibernate)+redis+maven配置
    数组进行多少次OP操作,才干有序
  • 原文地址:https://www.cnblogs.com/20193925zxt/p/14883152.html
Copyright © 2011-2022 走看看