android 动态横竖屏,Android实现动态切换横竖屏,保存横竖屏数据(用Preference存储)...

package com.test;

import android.app.Activity;

import android.content.pm.ActivityInfo;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnLongClickListener;

import android.view.Window;

import android.view.WindowManager;

import android.widget.TextView;

import android.widget.Toast;

/**

* class name:TestAndroid_BasicActivity

* class description:动态设置横竖屏哦

* PS:既然要实现横竖屏的变更,所以设置的记录就必须持久化存储 你可以用数据库,这里用的是简单一点的Preference

*

* @version 1.00 2011/09/21

* @author CODYY)peijiangping

*/

public class TestAndroid_BasicActivity extends Activity implements

OnLongClickListener {

// 设置默认为横屏SCREEN_ORIENTATION_LANDSCAPE,竖屏SCREEN_ORIENTATION_PORTRAIT

private int ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;

private TextView textview;

private MyPreference mpf;

@Override

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

mpf = new MyPreference(this, "love");

try {

ORIENTATION = mpf.read("model");

} catch (Exception e) {

System.out.println("first");

}

requestWindowFeature(Window.FEATURE_NO_TITLE);// 去掉标题栏

getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,

WindowManager.LayoutParams.FLAG_FULLSCREEN);// 设置全屏

// 设置横竖屏

setRequestedOrientation(ORIENTATION);

setContentView(R.layout.main);

init();

}

private void init() {

textview = (TextView) this.findViewById(R.id.textview);

textview.setText("Loveing");

textview.setOnLongClickListener(this);// 实现长按事件监听

}

@Override

public boolean onLongClick(View v) {

// 如果当前为横屏

if (ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {

// 将竖屏保存进MyPreference

mpf.write("model", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);

// 显示一个toast提示

Toast toast = Toast.makeText(this, "横屏已经切换成竖屏,请重新进入应用查看",

Toast.LENGTH_SHORT);

toast.show();

// 如果当前为竖屏

} else if (ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {

// 将横屏保存进MyPreference

mpf.write("model", ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

// 显示一个toast提示

Toast toast = Toast.makeText(this, "竖屏已经切换成横屏,请重新进入应用查看",

Toast.LENGTH_SHORT);

toast.show();

}

return false;

}

}

package com.test;

import android.content.Context;

import android.content.SharedPreferences;

import android.content.pm.ActivityInfo;

public class MyPreference {

private SharedPreferences preference;

private String file_name;

private int value_int;

//设置默认为横屏

private int defultint = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;;

public MyPreference(Context context, String str) {

file_name = str;

value_int = 0;

preference = context.getSharedPreferences(file_name, 0);

}

public int read(String name) {

value_int = preference.getInt(name, defultint);

return value_int;

}

public void write(String name, int value) {

SharedPreferences.Editor editor = preference.edit();

editor.putInt(name, value);

editor.commit();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值