四、Androidの携帯電話の画面の向き

12294 ワード

シミュレーションボタンをクリックすると、携帯電話の向きが変わります.
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;

publicclass OrientationActivity extends Activity {
private EditText editText=null;

@Override
publicvoid 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(){
publicvoid 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);
}
elseif(OrientationActivity.this.getRequestedOrientation()==ActivityInfo.SCREEN_ORIENTATION_PORTRAIT){
OrientationActivity.
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}

}

}
});
}

/**
*
*/
@Override
publicvoid 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);

}
}


実行結果
四、 Android之手机屏幕朝向