查看API文档: android.content.pm.ActivityInfo

在手机的使用中,我们要根据不同的需求来改变屏幕的显示方向,一般在浏览信息时是竖屏,在玩游戏的时候就要切换到横屏。在Android中要想完成屏幕方向的切换,需要Activity类的一些方法的支持。

getRequestedOrientation();取得当前手机屏幕的方向 setReqestedOrentation(ing requestedOrientation);设置手机屏幕方向 onConfigurationChanged(Configuration newConfig);系统设置改变时触发此事件(当使用setRequestedOrientation()方法改变了屏幕显示方向后触发此事件,表示 对系统设置改变进行监听,当系统改变之后,对于每一个Activity程序而言都相当于重新加载,如果要对一些组件做操作,就只能通过这个方法来完成)。 在设置和取得手机屏幕方向的操作中,需要一个int型的操作数据,这个数据由android.content.pm.ActivityInfo类所提供,该类中定义了3个常量 SCREEN_ORIENTATION_LANDSCAPE:屏幕横屏显示,表示数值为0 SCREEN_ORIENTATION-PROTRAIT:屏幕竖屏显示,表示数值为1 SCREEN_ORIENTATION_UNSPECIFIED:未指定的屏幕显示,表示数值为-1 有时候,我们在输入密码的时候为了确保输入的密码正确,希望可以以明文的方式来显示输入的密码,而不是*代替,此功能就可以借助EditText的setTransformationMethod()方法来完成, 密文显示:android.text.method.HideReturnsTransformationMethod 明文显示:android.text.method.PasswordTransformationMethod 当复选框选中和不选中的时候,密码明文和密文进行显示:
showPass =(CheckBox) this.findViewById(R.id.showpass);showPass.setOnCheckedChangeListener(new OnCheckedChangeListener() {@Overridepublic 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() {@Overridepublic 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文件中进行配置@Overridepublic 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:运行程序,效果如下图:

更多相关文章

  1. Android开机自动启动Wi-Fi热点的方法
  2. Android实例剖析笔记(三)
  3. Android中称为四大组件
  4. android bionic缺失pthread_cancel的解决方法
  5. Android四大组件之Activity---生命周期那些你可能不知道的事
  6. Andriod学习笔记(二)
  7. Android(安卓)事件传递机制
  8. android 积累一点关于fragment的知识
  9. 使用TraceView进行方法跟踪

随机推荐

  1. Android(安卓)开发者学习路线(2020 版本)
  2. 自制最简Android(安卓)MusicPlayer
  3. android实现应用程序无图标(快捷方式)问
  4. Android(安卓)将asserts文件夹内文件写入
  5. Android(安卓)or java https ssl excepti
  6. Android腾讯微薄客户端开发十三:提及篇(与
  7. Android(安卓)shape layer-list实现左右
  8. Android(安卓)SQLiteDatabase 的使用
  9. android AlertDialog更改显示位置
  10. android 模拟器 PANIC: Could not open: