Android屏幕横屏竖屏切换的方法

package lxd.copenhagen.clickListern;

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

public class ClickEventActivity extends Activity
{
   private Button button;

   /** Called when the activity is first created. */
   @Override
   public void onCreate(Bundle savedInstanceState)
   {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);

      button = (Button) super.findViewById(R.id.btnTest);
      button.setOnClickListener(new OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            int o = getRequestedOrientation();// 获取手机的朝向
            switch (o)
            {
               case ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE:
                  button.setText("当前屏幕朝向为: 横屏");
                  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
                  break;
               case ActivityInfo.SCREEN_ORIENTATION_PORTRAIT:
                  button.setText("当前屏幕朝向为: 竖屏");
                  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                  break;
                  default:
                  button.setText("sb");
            }
            // 不能省略,否则会报android.app.SuperNotCalledException: Activity
            // OrientationActivity did not
            // call through to super.onConfigurationChanged()异常
         //   super.onConfigurationChanged(newConfig);
         }
      });

   }
}
源代码如上
需要配置 ManiFest菜单 这样配置的:
<?xml version= "1.0" encoding= "utf-8"?>
<manifest xmlns:android= "http://schemas.android.com/apk/res/android"
    package= "lxd.copenhagen.clickListern"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="8" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:screenOrientation="landscape"
            android:name=".ClickEventActivity"
            android:label="@string/app_name" >
            <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>

你可能感兴趣的:(android,手机,button,encoding)