import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class ActivityMain extends Activity
{
private TextView mTextView01;
private Button mButton01;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// 設置爲無標題欄
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 設置爲全屏模式
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
mButton01 = (Button) findViewById(R.id.myButton1);
mTextView01 = (TextView) findViewById(R.id.myTextView1);
// Return the current requested orientation of the activity
if (getRequestedOrientation() == -1)
{
mTextView01.setText(getResources().getText(R.string.str_err_1001));
}
/* 當點擊按鈕旋轉屏幕畫面 */
mButton01.setOnClickListener(new Button.OnClickListener()
{
// @Override
public void onClick(View arg0)
{
/* 方法一:重寫getRequestedOrientation */
/* 若沒法取得screenOrientation屬性 */
if (getRequestedOrientation() == -1)
{
/* 提示沒法進行畫面旋轉功能,因沒法判別Orientation */
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
/* 判斷要更改的方向,以Toast提示 */
switch (requestedOrientation)
{
/* 更改成LANDSCAPE */
case (ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE):
mMakeTextToast(getResources().getText(R.string.str_msg1).toString(),
false);
break;
/* 更改成PORTRAIT */
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
/* 此重寫getRequestedOrientation方法,可取得當下屏幕的方向 */
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();
}
}
}