Android實現動態切換橫豎屏,保存橫豎屏數據(用Preference存儲) – Android移動開發技術文章_手機開發 Android移動開發教學課程

 

Look My Code:

 

 

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<BR>

 * class description:動態設置橫豎屏哦<BR>

 * PS:既然要實現橫豎屏的變更,所以設置的記錄就必須持久化存儲 你可以用數據庫,這裡用的是簡單一點的Preference <BR>

 * 

 * @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(); 

    } 

摘自 裴裴的Android學習和分享

發佈留言