Android changes the way the phone is facing the screen _android

Source: Internet
Author: User

The example in this article describes how Android changes the screen orientation of your phone. Share to everyone for your reference. Specifically as follows:

Simulate when the button is clicked, the phone is facing a change.

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 toward"/>
  <!--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=" displays 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 the direction of the mobile phone, otherwise it will not be Take the phone's direction 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 "Andr" oid:configchanges= "Orientation" > <intent-filter> <action android:name= "Android.intent.action.MAI N "/> <category android:name=" Android.intent.category.LAUNCHER "/> </intent-filter> </ activity> </application> <uses-sdk android:minsdkversion= "7"/> <!--change the phone configuration permissions--> <uses-pe Rmission android:name= "Android.permission.CHANGE_CONFIGURATION"/&GT

 </manifest>

Orientationactivity class:

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) {//Determine if the Requestedorientat can be obtained Ion Property if (OrientationActivity.this.getRequestedOrientation () ==-1) {Toast.maketext (Orientationactivity.thi
        S, "system orientation not available", Toast.length_long. Show (); }else{//Mobile screen orientation has 7 optional values, respectively, as follows//screen_orientation_behind: The current acti in the inheritance activity stackThe direction of the activity below the vity//screen_orientation_landscape: Horizontal screen (Landscape), display width greater than height//screen_orientation_portrait : Vertical screen (portrait photo), display height greater than width//screen_orientation_nosensor: ignoring physical sensors-that is, the display direction is independent of the physical sensor,///No matter how the user rotates the device display direction will not change with 
          (Except for the "unspecified" setting)//screen_orientation_sensor: The physical sensor determines the display direction, 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,//selection policy depending on the configuration of the specific equipment, so different devices will have different direction to choose Screen_orientation_user: User's current preferred direction if (OrientationActivity.this.getRequestedOrientation () ==activityin Fo. Screen_orientation_landscape) {OrientationActivity.this.setRequestedOrientation (activityinfo.screen_orientatio
          n_portrait); }else if (OrientationActivity.this.getRequestedOrientation () ==activityinfo.screen_orientation_portrait) {Orient
          AtionActivity.this.setRequestedOrientation (Activityinfo.screen_orientation_landscape);
   }
        }
      } }); /** * When configuration information changes trigger/@Override public void onconfigurationchanged (Configuration newconfig) {Toast.maket
    Ext (This, "the system's screen orientation has changed", Toast.length_long). Show (); int o=getrequestedorientation ()//Get the phone's facing switch (o) {case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:editT
      Ext.settext ("Current screen orientation: horizontal screen");
    Break
      Case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:editText.setText ("Current screen orientation: vertical screen");
    Break //Can not omit, otherwise will report android.app.SuperNotCalledException:Activity Orientationactivity did not//call through to Super.onc
  Onfigurationchanged () Abnormal super.onconfigurationchanged (newconfig);

 }
}

Run Result:

I hope this article will help you with your Android program.

Related Article

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.