---
title: "手机屏幕方向切换：横屏 竖屏"
author: "AndLi"
date: "2011-11-24T19:47:44Z"
tags: []
categories: ["移动开发"]
slug: "untitled"
draft: false
lastmod: "2011-11-24T19:47:44Z"
id: 1277105
url: "https://andli.iteye.com/blog/1277105"
---

![](http://dl.iteye.com/upload/attachment/594675/437da361-21d8-34ee-8f4e-c35fbc737d22.jpg)      ![](http://dl.iteye.com/upload/attachment/594677/297c5595-7335-3f6a-beca-478005fca795.jpg)

```
public class Main extends Activity {
	EditText et;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Button btn = (Button) findViewById(R.id.btn);
		et = (EditText) findViewById(R.id.et);
		btn.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				if (Main.this.getRequestedOrientation() == -1) { // 判断是否可以获得requestedOrientation属性
					Toast.makeText(Main.this, "系统的屏幕方无法获取!!", 3000).show();
				} else {
					if (Main.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
						Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
					} else if (Main.this.getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
						Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
					}
				}
			}
		});
	}

	public void onConfigurationChanged(Configuration newConfig) {
		Toast.makeText(this, "系统的屏幕方向发生改变", 3000).show();
		updateEditText(); // 更新EditText显示的内容
		super.onConfigurationChanged(newConfig);
	}

	public void updateEditText() {
		int o = getRequestedOrientation(); // 获取屏幕朝向
		switch (o) { // 判断屏幕当前朝向
		case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
			et.setText("当前屏幕朝向为：PORTRAIT");
			break;
		case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
			et.setText("当前屏幕朝向为：LANDSCAPE");
			break;
		}
	}
}
```

 

```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="wyf.wpf"
    android:versionCode="1"
    android:versionName="1.0" >

    <application
        android:icon="@drawable/icon"
        android:label="@string/app_name" >
        <activity
            android:configChanges="orientation"
            android:label="@string/app_name"
            android:name=".Main"
            android:screenOrientation="portrait" >
            <intent-filter >
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

    <uses-permission android:name="android.permission.CHANGE_CONFIGURATION" />

</manifest>
```

   
     
![](http://dl.iteye.com/upload/attachment/594680/1ce22bdf-f57f-3f56-992c-dbbb6f068290.jpg)