根据ToggleButton的选项来设置屏幕的方向

package com.example.androidtest;import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.content.pm.ActivityInfo;
import android.widget.CompoundButton;
import android.widget.ToggleButton;
import android.content.res.Configuration;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.util.Log;public class MainActivity extends Activity {public void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);ToggleButton toggle = (ToggleButton)findViewById(R.id.toggleButton);if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {toggle.setChecked(true);} else {toggle.setChecked(false);}toggle.setOnCheckedChangeListener(listener);}OnCheckedChangeListener listener = new OnCheckedChangeListener(){public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){int current = getResources().getConfiguration().orientation;Log.d("wuli", "current:" + current);if (isChecked) {switch (current) {case Configuration.ORIENTATION_LANDSCAPE:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);break;case Configuration.ORIENTATION_PORTRAIT:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);break;default:setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);}} else {setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);}}};}

xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"android:layout_width="fill_parent"android:layout_height="fill_parent"><ToggleButton android:id="@+id/toggleButton"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textOff="Lock"android:textOn="LOCKED"/>
</LinearLayout>

Button的使用(六):ToggleButton相关推荐

  1. Android ToggleButton:状态切换的Button

     Android ToggleButton:状态切换的Button Android ToggleButton和Android Button类似,但是ToggleButton提供了一种选择机制,可以 ...

  2. 使用Kotlin的Android ToggleButton

    In this tutorial, we'll learn how to implement Android ToggleButton using Kotlin. We will learn how ...

  3. 初学Android,音频管理器之控制音频(六十六)

    下面要写的是AudioManager这个音频管理器,它可以通过getSystemService得到 有一个重要方法adjustStreamVolumn(int streamType,int direc ...

  4. Android Button 属性详解

    Button认知 public class Button extends TextView java.lang.Object↳ android.view.View↳ android.widget.Te ...

  5. android入门基础笔记,Sqlite数据库下载

    * 移动通讯技术的发展: 第一代通讯技术:模拟信号 工作频段   缺点:保密性差 第二代通讯技术: 体积越来越小  数字信号(加密)   短信  彩信 第三代通讯技术: 数字信号   可处理图像.音乐 ...

  6. Android Studio 学习实例记录-手电筒

    Android Studio 学习实例记录-手电筒 刚安装好Android Studio3.1.2,上网搜了一个实例进行实践学习,仅用这篇文章来记录学习过程 MainActivity.java的源码来 ...

  7. 安卓应用开发期末与面试概念冲刺(概念与代码,from hitwh)

    安卓应用开发概念冲刺(概念与代码) 前三章 安卓整体体系结构(四个)以及其中包含什么 安卓将()和()分离? intent和Service的用处.ContentProvider用处 安卓四大核心组件 ...

  8. 微信公众平台开发(58)自定义菜单

    微信公众平台开发 微信公众平台开发模式 企业微信公众平台 自定义菜单 开发教程  作者:方倍工作室  地址:http://www.cnblogs.com/txw1958/p/weixin-58-cus ...

  9. android 变化字体,android 字体修改

    android字体的设置有以下方法: 1)直接在代码设置Typeface customFont = Typeface.createFromAsset(this.getAssets(), "1 ...

最新文章

  1. linux限制显卡频率,关于安装Linux (FC6)频率超出范围与显卡驱动
  2. 用博奥如何导入单项工程电子表_用博奥如何导入单项工程电子表_博奥常见问题处理汇总...
  3. 联想拯救者Y7000P 2021H deepin v20.2.4设置双屏显示:切记要用集显,NAVIDA独显不生效
  4. linux性能并发 带机量,性能测试笔记(一):吞吐量与并发数
  5. 一次Python性能调优经历
  6. Maven安装与配制环境【win7/win10】
  7. 深入剖析 redis AOF 持久化策略
  8. day31 模块 与包
  9. 学生宿舍管理项目开发计划书_学生宿舍管理系统项目-计划书.doc
  10. 2021-11-23日win10更新bug:共享打印机无法连接的修复
  11. 鸿蒙大陆7.1正式版隐藏英雄,守塔V7.01下载 守塔V7.01正式版 附游戏攻略及隐藏英雄密码 魔兽防守地图 下载-脚本之家...
  12. Apache下如何安装ssl证书?PHPWAMP如何开启SSL
  13. CodeSmith连接MySql数据库
  14. 用Python制作我的核酸检测日历
  15. 转:隐马尔可夫模型(HMM)攻略
  16. python PIL库中的getpixel函数
  17. 计算机视觉常用资源整理
  18. ASA与FTD的基本配置
  19. 微信公众号运营商,如何选择适合自己的微信第三方平台?
  20. Cramer-Rao下界

热门文章

  1. SQL SERVER 2008 R2 SP1更新时,遇上共享功能更新失败解决方案
  2. RS-232串行口连接线 com口连接线 并口链接线 双绞线 交叉线 直连线
  3. 开始喜欢Python了
  4. 零基础学python爬虫-我是如何零基础开始能写Python爬虫的
  5. python3.8.2安装教程-在服务器上安装python3.8.2环境的教程详解
  6. 快速排序 python菜鸟教程-十大编程算法助程序员走上高手之路
  7. python官方-Python官方中文文档强势来袭
  8. 简明python教程购买-自学Python买什么书?
  9. python变量类型-python变量的数据类型有哪些?
  10. python散点图拟合曲线-python – 将曲线拟合到散点图的边界