Android——横屏和竖屏的切换,以及明文密码的显示
查看API文档: android.content.pm.ActivityInfo
在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏。在Android中要想完成屏幕方向的切换,需要Activity类的一些方法的支持。
showPass =(CheckBox) this.findViewById(R.id.showpass);
showPass.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if (isChecked == true) {
// 如果复选框被选中,文本框内容可见
LoginGalleryActivity.this.userpass
.setTransformationMethod(HideReturnsTransformationMethod
.getInstance());
} else {
//如果复选框没有被选中,文本框内容不可见
LoginGalleryActivity.this.userpass
.setTransformationMethod(PasswordTransformationMethod
.getInstance());
}
}
});
下面就是横屏和竖屏的显示功能的实现了,由于横屏和竖屏的切换会引起系统配置的改变,我们还需要在Manifest.xml中进行声明
如图:
chageScrean = (Button) this.findViewById(R.id.screanChange);
//改变屏幕显示为横屏或竖屏
chageScrean.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
if (ShowGallery.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED) {
chageScrean.setText("错误,无法改变屏幕方向"); } else if (ShowGallery.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
ShowGallery.this.setRequestedOrientation(1);//设置当期屏幕为竖屏
}else if(ShowGallery.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
ShowGallery.this.setRequestedOrientation(0);//设置当前屏幕为横屏
}
}
});
//系统设置改变时触发该方法,还需要在Manifest.xml文件中进行配置
@Override
public void onConfigurationChanged(Configuration newConfig) {
if(newConfig.orientation==Configuration.ORIENTATION_LANDSCAPE)
{
ShowGallery.this.chageScrean.setText("改变屏幕方向为竖屏(当前为横屏)");
}
else if(newConfig.orientation==Configuration.ORIENTATION_PORTRAIT)
{
ShowGallery.this.chageScrean.setText("改变屏幕方向为横屏(当前为竖屏)");
}
super.onConfigurationChanged(newConfig);
}
下面是横竖屏测试的程序:
我们这里主要是运用了getRequestedOrientation(),和setRequestedorientation()两个方法.但是要利用这两个方法必须先在AndroidManiefst.xml设置一下屏幕方属性,不然程序将不能正常的工作.
Step 1:我们建立一个Android工程,命名为ChangeScreemOrientationDemoActivity.
Step 2:设计UI,打开main.xml,将其代码修改如下,我们这里只是增加了一个按钮,其他什么都没有动.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/press"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="press me change screem orientation"/>
</LinearLayout>
Step 3:设计主程序ChangeScreemOrientationDemoActivity.java,修改其代码如下:
package com.lp.ecjtu;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class ChangeScreemOrientationDemoActivity extends Activity {
/** Called when the activity is first created. */
private Button pressBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pressBtn = (Button) findViewById(R.id.press);
pressBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//如果是竖屏,改为横屏
if(getRequestedOrientation()== ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT);
}
//如果是横屏,改为竖屏
else if(getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
});
}
}
Step 4:在AndroidManifest.xml文件里设置默认方向,不然程序不能正常工作哦.代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".ChangeOrientationDemo"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<uses-sdk android:minSdkVersion="3" />
</manifest>
Step 5:运行程序,效果如下图:
Android——横屏和竖屏的切换,以及明文密码的显示的更多相关文章
- android界面横屏和竖屏的切换
关于android横屏和竖屏的切换网上给了很多种.但是有些介绍的方法都是在android旧版本上. 我现在把握用到的情况写下来用于备忘: android 版本:4.0 - 4.4 要求:android ...
- Android 设置横屏或竖屏
方法一:在AndroidManifest.xml中配置 如果不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...
- android 强制设置横屏 判断是横屏还是竖屏
判断activity 是横屏还是竖屏 方法 1: //根据设备配置信息 Configuration cf= this.getResources().getConfiguration(); //获取设 ...
- activity横屏竖屏的切换
原理: 其实总结起来,我们可以得到以下的一些结论 1.当内存不足(不容易模拟).切屏时会调用onSaveInstanceState().onRestoreInstanceState()方法 对于onS ...
- android设置横屏和竖屏的方法
方法一:在AndroidManifest.xml中配置 假设不想让软件在横竖屏之间切换,最简单的办法就是在项目的AndroidManifest.xml中找到你所指定的activity中加上androi ...
- JQuery 判断IPad、IPhone、Android是横屏还是竖屏(Window.Orientation实现)
在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法. 代码如下: function ...
- iOS强制切换横屏、竖屏
切换横竖屏最直接的方式是调用device的setOrientation方法.但是从sdk3.0以后,这个方法转为似有API,如果要上AppStore的话,要慎用! if ([[UIDevice cur ...
- 基于jQuery的判断iPad、iPhone、Android是横屏还是竖屏的代码
在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法 其实主要是通过window.or ...
- activity的横屏和竖屏设置
主要在清单文件这样配置: <application android:allowBackup="true" android:icon="@drawable/ic_la ...
随机推荐
- Cygwin下软件安装 - apt-cyg
安装了cygwin,但不能像centos上装yum,装东西很不方便.找了下可以用apt-cyg来安装软件. 1.下载apt-cyg $ wget raw.github.com/transcode-op ...
- arcgis中求多点到一条曲线的最短欧几里得距离
1.使用的工具:Arctoolbox----Analysis Tools----Proximity----Near工具. 2.注意:在求距离之前一定要先设置好坐标系统.
- Nginx服务器架构简析
一.Nginx的模块化 模块化结构的思想是一个很久的概念,但也正是成熟的思想造就了Nginx的巨大优越性. 我们知道Nginx从总体上来讲是有许多个模块构成的.习惯将Nginx分为5大模块分别为:核心 ...
- 最简单的耗时组件(窗口activity里面放一个progressBar)
①.先定义一个activity package com.example.administrator.actionbardemo; import android.app.Activity; import ...
- iOS进阶学习-数据处理之文件读写
一.沙盒机制 1.什么是沙盒? 每一个iOS应用程序都会为自己创建一个文件系统目录(文件夹),这个独立.封闭.安全的空间,叫做沙盒. 2.沙盒机制(SandBox) 沙盒是一种安全体系. 它规定了应用 ...
- Extjs-工具条和菜单 Ext.menu和Ext.Toolbar
转载自:http://blog.csdn.net/itlwc/article/details/7878002 1.创建一个简单工具条效果图 <script type="text/jav ...
- 关于SVN 目录结构,使用教程
SVN使用教程:http://www.cnblogs.com/armyfai/p/3985660.html Subversion有一个很标准的目录结构,是这样的.比如项目是proj,svn地址为svn ...
- C++ vector介绍
<span style="font-family: Arial; ">在此总结一下模板vector的使用介绍</span> 标准库vector类型使用需要的 ...
- [SSH服务]——一些安全性配置和补充实验
SSH 安全性和配置 转载于 http://www.ibm.com/developerworks/cn/aix/library/au-sshsecurity/ 对于一些之前列举的代码示例,许多系统管理 ...
- Redis基础教程
说明:本文中涉及的代码是c#所写,连接redis的第三方驱动为ServiceStack.Redis.连接redis的客户端软件为redis-desktop-manager. 一.Redis是什么 Re ...