Android實現動態切換橫豎屏,儲存橫豎屏資料(用Preference儲存)

來源:互聯網
上載者:User

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 implementsOnLongClickListener {// 設定預設為橫屏SCREEN_ORIENTATION_LANDSCAPE,豎屏SCREEN_ORIENTATION_PORTRAITprivate int ORIENTATION = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE;private TextView textview;private MyPreference mpf;@Overridepublic 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);// 實現長按事件監聽}@Overridepublic boolean onLongClick(View v) {// 如果當前為橫屏if (ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {// 將豎屏儲存進MyPreferencempf.write("model", ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);// 顯示一個toast提示Toast toast = Toast.makeText(this, "橫屏已經切換成豎屏,請重新進入應用查看",Toast.LENGTH_SHORT);toast.show();// 如果當前為豎屏} else if (ORIENTATION == ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {// 將橫屏儲存進MyPreferencempf.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();}}
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.