动态更改屏幕方向……LANDSCAPE与PORTRAIT
package irdc.ex05_22;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Display;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class EX05_22 extends Activity
{
private TextView mTextView01;
private Button mButton01;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton01 = (Button)findViewById(R.id.myButton1);
mTextView01 = (TextView)findViewById(R.id.myTextView1);
if(getRequestedOrientation()==-1)
{
mTextView01.setText(getResources().getText
(R.string.str_err_1001));
}
mButton01.setOnClickListener(new Button.OnClickListener()
{
@Override
public void onClick(View arg0)
{
if(getRequestedOrientation()==-1)
{
mTextView01.setText(getResources().getText
(R.string.str_err_1001));
}
else
{
if(getRequestedOrientation()==
ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
{
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
else if(getRequestedOrientation()==
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT)
{
setRequestedOrientation
(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}
}
}
});
}
@Override
public void setRequestedOrientation(int requestedOrientation)
{
// TODO Auto-generated method stub
switch(requestedOrientation)
{
case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):
mMakeTextToast
(
getResources().getText(R.string.str_msg1).toString(),
false
);
break;
case (ActivityInfo.SCREEN_ORIENTATION_PORTRAIT):
mMakeTextToast
(
getResources().getText(R.string.str_msg2).toString(),
false
);
break;
}
super.setRequestedOrientation(requestedOrientation);
}
@Override
public int getRequestedOrientation()
{
// TODO Auto-generated method stub
return super.getRequestedOrientation();
}
public void mMakeTextToast(String str, boolean isLong)
{
if(isLong==true)
{
Toast.makeText(EX05_22.this, str, Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(EX05_22.this, str, Toast.LENGTH_SHORT).show();
}
}
}
AndroidMinifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="irdc.ex05_22"
android:versionCode="1"
android:versionName="1.0.0">
<application
android:icon="@drawable/icon"
android:label="@string/app_name">
<activity
android:name=".EX05_22"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>