一、简介
二、代码
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name"
<intent-filter
<action android:name="android.intent.action.MAIN" /
<category android:name="android.intent.category.LAUNCHER" /
</intent-filter
</activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" </activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" </activity
</application
</manifest
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name"
<intent-filter
<action android:name="android.intent.action.MAIN" /
<category android:name="android.intent.category.LAUNCHER" /
</intent-filter
</activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" </activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" </activity
</application
</manifest
核心代码:android:configChanges=”keyboardHidden|orientation|screenSize”
com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen;
import com.fry.activityLifeCycle_3Screen.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_pause;//创建一个button对象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父类操作
setContentView(R.layout.activity_main);//引入名为activity_main的界面
btn_pause=(Button) findViewById(R.id.btn_pause);//找id为btn_pause的button
btn_stop=(Button) findViewById(R.id.btn_stop);//找id为btn_stop的button
btn_offLine=(Button) findViewById(R.id.btn_offLine);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_offLine.setOnClickListener(this);
/*
* activity被创建时执行
*/
Log.d(tag, "onCreate");
}
/*
* activity可见时执行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互时执行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可见时执行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暂停时执行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止时执行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity销毁时执行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改变时执行
* 比如横竖屏幕的切换,键盘有无的切换,屏幕大小的改变
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag, "onConfigurationChanged");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_pause:
Intent intent=new Intent();
intent.setClass(this, Activity02.class);
startActivity(intent);
break;
case R.id.btn_stop:
Intent intent2=new Intent();
intent2.setClass(this, Activity01.class);
startActivity(intent2);
break;
case R.id.btn_offLine://断开状态
finish();
default:
break;
}
}
}
三、一直横屏或者一直竖屏
很多手机游戏里面一进去就是横屏,而且不能切换为竖屏,那么怎么样达到这样的效果呢?
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
<activity
android:screenOrientation="portrait"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name"
<intent-filter
<action android:name="android.intent.action.MAIN" /
<category android:name="android.intent.category.LAUNCHER" /
</intent-filter
</activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" </activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" </activity
</application
</manifest
android:screenOrientation=”landscape”横屏 android:screenOrientation=”portrait”竖屏
四、如何获取手机是横屏还是竖屏
com.fry.activityLifeCycle_3Screen.MainActivity
package com.fry.activityLifeCycle_3Screen;
import com.fry.activityLifeCycle_3Screen.R;
import android.app.Activity;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
private Button btn_pause;//创建一个button对象
private Button btn_stop;
private Button btn_offLine;
private String tag=MainActivity.class.getSimpleName();
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父类操作
setContentView(R.layout.activity_main);//引入名为activity_main的界面
btn_pause=(Button) findViewById(R.id.btn_pause);//找id为btn_pause的button
btn_stop=(Button) findViewById(R.id.btn_stop);//找id为btn_stop的button
btn_offLine=(Button) findViewById(R.id.btn_offLine);
btn_pause.setOnClickListener(this);
btn_stop.setOnClickListener(this);
btn_offLine.setOnClickListener(this);
/*
* activity被创建时执行
*/
Log.d(tag, "onCreate");
}
/*
* activity可见时执行
*/
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
Log.d(tag, "onStart");
}
/*
* activity交互时执行
*/
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
Log.d(tag, "onResume");
}
/*
* activity重新可见时执行
*/
@Override
protected void onRestart() {
// TODO Auto-generated method stub
super.onRestart();
Log.d(tag, "onRestart");
}
/*
* activity暂停时执行
*/
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
Log.d(tag, "onPause");
}
/*
* activity停止时执行
*/
@Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
Log.d(tag, "onStop");
}
/*
* activity销毁时执行
*/
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
Log.d(tag, "onDestroy");
}
/*
* activity在配置改变时执行
* 比如横竖屏幕的切换,键盘有无的切换,屏幕大小的改变
*/
@Override
public void onConfigurationChanged(Configuration newConfig) {
// TODO Auto-generated method stub
super.onConfigurationChanged(newConfig);
Log.d(tag, "onConfigurationChanged");
int width=getWindowManager().getDefaultDisplay().getWidth();
int height=getWindowManager().getDefaultDisplay().getHeight();
if(width height) Log.d(tag, "landscape");
else Log.d(tag, "portrait");
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btn_pause:
Intent intent=new Intent();
intent.setClass(this, Activity02.class);
startActivity(intent);
break;
case R.id.btn_stop:
Intent intent2=new Intent();
intent2.setClass(this, Activity01.class);
startActivity(intent2);
break;
case R.id.btn_offLine://断开状态
finish();
default:
break;
}
}
}
/activityLifeCycle_3Screen/AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fry.activityLifeCycle_3Screen"
android:versionCode="1"
android:versionName="1.0"
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme"
<activity
android:configChanges="keyboardHidden|orientation|screenSize"
android:name="com.fry.activityLifeCycle_3Screen.MainActivity"
android:label="@string/app_name"
<intent-filter
<action android:name="android.intent.action.MAIN" /
<category android:name="android.intent.category.LAUNCHER" /
</intent-filter
</activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity01" </activity
<activity android:name="com.fry.activityLifeCycle_3Screen.Activity02" android:theme="@android:style/Theme.Translucent" </activity
</application
</manifest
以上就是本文的全部内容,希望对大家的学习有所帮助。