Four, Android mobile phone screen facing

Source: Internet
Author: User

Simulates when a button is clicked, causing the phone to change direction.

Main.xml Layout file

<?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
="Click Change screen Orientation"/>
<!--android:hint: Displays the text when the text is empty -
<EditText Android:id="@+id/edittext"
Android:layout_width
="fill_parent"
Android:layout_height
="wrap_content"
Android:cursorvisible
="false"
Android:hint
="Show current screen orientation"/>
</LinearLayout>

Manifest file

<?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">
<!--set your phone's orientation or you won't be able to get the phone's orientation
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"/>
<!--Change phone configuration permissions -
<uses-Permission Android:name="Android.permission.CHANGE_CONFIGURATION"/>
</Manifest>

Orientationactivity class

 Packagecom.ljq.activity;

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

PublicclassorientationactivityextendsActivity {
PrivateEditText EditText=NULL;

@Override
PublicvoidonCreate (Bundle savedinstancestate) {
Super. OnCreate (savedinstancestate);
Setcontentview (R.layout.main);

EditText
=(EditText) Findviewbyid (R.id.edittext);
Button btn
=(Button) Findviewbyid (R.ID.BTN);
Btn.setonclicklistener (
NewView.onclicklistener () {
PublicvoidOnClick (View v) {
//determine if the Requestedorientation property can be obtained
if(orientationactivity. This. Getrequestedorientation ()==-1){
Toast.maketext (orientationactivity.
This, "system orientation is not available", Toast.length_long). Show ();
}
Else{
//the mobile screen faces 7 selectable values, as follows
//Screen_orientation_behind: Inherits the direction of the activity under current activity in the activity stack
//screen_orientation_landscape: Horizontal screen (Landscape), the width of the display is greater than the height
//screen_orientation_portrait: Vertical screen (portrait photo), height greater than width when displayed
//Screen_orientation_nosensor: Ignoring physical sensors-that is, the display direction is independent of the physical sensor,
//No matter how the user rotates the display direction of the device will not change (except for the "unspecified" setting)
//Screen_orientation_sensor: The physical sensor determines the direction of the display, depending on how the user holds the device,
//The direction changes when the device is rotated-between the horizontal screen and the vertical screen
//screen_orientation_unspecified: Unspecified, this is the default value, the Android system chooses the appropriate direction for itself,
//The selection strategy depends on the configuration of the specific device, so different devices will have different orientation options
//screen_orientation_user: User's current preferred orientation
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);
}

}

}
});
}

/**
* Triggered when the configuration information has changed
*/
@Override
PublicvoidOnconfigurationchanged (Configuration newconfig) {
Toast.maketext (
This, "the screen orientation of the system has changed", Toast.length_long). Show ();
into=getrequestedorientation ();//get the phone's orientation
Switch(o) {
CaseActivityinfo.screen_orientation_landscape:
Edittext.settext (
"current screen facing: Horizontal screen");
Break;
CaseActivityinfo.screen_orientation_portrait:
Edittext.settext (
"current screen facing to: Vertical screen");
Break;
}
//cannot be omitted, otherwise it will be reported android.app.SuperNotCalledException:Activity Orientationactivity did not
//Call through to super.onconfigurationchanged () exception
Super. onconfigurationchanged (Newconfig);

}
}

Run results

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.