模拟当点击按钮时,使手机朝向发生改变。

main.xml布局文件

              <?        xml version        =        "        1.0        "         encoding        =        "        utf-8        "        ?>        
< LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android "
android:orientation
= " vertical " android:layout_width = " fill_parent "
android:layout_height
= " fill_parent " >
< Button android:id = " @+id/btn "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:text
= " 点击更改屏幕朝向 " />
<!-- android:hint: 当文本为空时显示该文本 -->
< EditText android:id = " @+id/editText "
android:layout_width
= " fill_parent "
android:layout_height
= " wrap_content "
android:cursorVisible
= " false "
android:hint
= " 显示当前屏幕朝向 " />
</ LinearLayout >

清单文件

              <?        xml version        =        "        1.0        "         encoding        =        "        utf-8        "        ?>        
< manifest xmlns:android = " http://schemas.android.com/apk/res/android "
package = " com.ljq.activity "
android:versionCode
= " 1 "
android:versionName
= " 1.0 " >
<!-- 设置手机的朝向,不然无法获取手机的朝向
android:screenOrientation
= " portrait "
android:configChanges
= " orientation " -->
< application android:icon = " @drawable/icon " android:label = " @string/app_name " >
< activity android:name = " .OrientationActivity "
android:label
= " @string/app_name "
android:screenOrientation
= " portrait "
android:configChanges
= " orientation " >
< intent - filter >
< action android:name = " android.intent.action.MAIN " />
< category android:name = " android.intent.category.LAUNCHER " />
</ intent - filter >
</ activity >

</ application >
< uses - sdk android:minSdkVersion = " 7 " />
<!-- 改变手机配置权限 -->
< uses - permission android:name = " android.permission.CHANGE_CONFIGURATION " />
</ manifest >

OrientationActivity类

              package         com.ljq.activity;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class OrientationActivity extends Activity {
private EditText editText = null ;

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

editText
= (EditText)findViewById(R.id.editText);
Button btn
= (Button)findViewById(R.id.btn);
btn.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v) {
// 判断是否可以获得requestedOrientation属性
if (OrientationActivity. this .getRequestedOrientation() ==- 1 ){
Toast.makeText(OrientationActivity.
this , " 系统的朝向无法获取 " , Toast.LENGTH_LONG).show();
}
else {
// 手机屏幕的朝向有7个可选值,分别如下
// SCREEN_ORIENTATION_BEHIND: 继承Activity堆栈中当前Activity下面的那个Activity的方向
// SCREEN_ORIENTATION_LANDSCAPE: 横屏(风景照) ,显示时宽度大于高度
// SCREEN_ORIENTATION_PORTRAIT: 竖屏 (肖像照) , 显示时高度大于宽度
// SCREEN_ORIENTATION_NOSENSOR: 忽略物理感应器——即显示方向与物理感应器无关,
// 不管用户如何旋转设备显示方向都不会随着改变("unspecified"设置除外)
// SCREEN_ORIENTATION_SENSOR: 由物理感应器决定显示方向,它取决于用户如何持有设备,
// 当设备被旋转时方向会随之变化——在横屏与竖屏之间
// SCREEN_ORIENTATION_UNSPECIFIED: 未指定,此为默认值,由Android系统自己选择适当的方向,
// 选择策略视具体设备的配置情况而定,因此不同的设备会有不同的方向选择
// SCREEN_ORIENTATION_USER: 用户当前的首选方向
if (OrientationActivity. this .getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE){
OrientationActivity.
this .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if (OrientationActivity. this .getRequestedOrientation() == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
OrientationActivity.
this .setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

}

}
});
}

/**
* 配置信息发生改变时触发
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
Toast.makeText(
this , " 系统的屏幕方向发生改变 " , Toast.LENGTH_LONG).show();
int o = getRequestedOrientation(); // 获取手机的朝向
switch (o) {
case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
editText.setText(
" 当前屏幕朝向为: 横屏 " );
break ;
case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
editText.setText(
" 当前屏幕朝向为: 竖屏 " );
break ;
}
// 不能省略,否则会报android.app.SuperNotCalledException: Activity OrientationActivity did not
// call through to super.onConfigurationChanged()异常
super .onConfigurationChanged(newConfig);

}
}

运行结果

更多相关文章

  1. android map
  2. Android(安卓)LayoutInflater.inflate(R.layout.activity_test,
  3. 设置TextView超过一定长度后显示省略号
  4. Android(安卓)去掉运营商STK对话框提示
  5. Android(安卓)WebView相关属性
  6. Android(安卓)开发中的一些小技巧
  7. android.intent.action.MAIN与android.intent.category
  8. android gif view
  9. android屏幕旋转在framework中的修改。

随机推荐

  1. PHP实现大文件断点下载
  2. K8S 之 通过kubeadmin安装K8S集群
  3. centos 将本地代码软链到web服务代码目录
  4. Terraform 学习笔记(1)
  5. 记录一次Postgresql的repmgr高可用集群切
  6. 【硬核干货】如何高效找到优质编程项目?
  7. 类成员重载、全局成员、命名空间声明与访
  8. 数据可视化工具在小学信息技术教学中的实
  9. Python语言的排序算法有哪些?Python学习
  10. Oracle OCP 19c 认证1Z0-083考试题库(第1