zoukankan      html  css  js  c++  java
  • SharedPreferences的简单实例

    package com.delightPress.chap61.pref;

    import com.delightPress.chap61.R;

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;

    public class PrefManual extends Activity {
        
        public static final String PREF_FILENAME = "aLibro.pref";
        
        /**
         * 从界面中取得用户首选
         * 并且存储进 PREF_FILENAME 之中
         */
        public class PreferenceOnClickListenter implements OnClickListener{

            @Override
            public void onClick(View v) {
                //按照文件名取得首选设置
                SharedPreferences settings = getSharedPreferences(PREF_FILENAME, 0);
                //取得首选编辑器
                SharedPreferences.Editor editor = settings.edit();
                //取得用户输入的公司抬头和统编
                String title = ((EditText) PrefManual.this.findViewById(R.id.pref_title)).getText().toString();
                String serial = ((EditText) PrefManual.this.findViewById(R.id.pref_serial)).getText().toString();
                editor.putString("title", title);
                editor.putString("serial", serial);
                
                //将数据写入首选设置
                editor.commit();
                finish();
            }
            
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            setContentView(R.layout.pref_manual);
            Button submitButton = (Button) findViewById(R.id.pref_submit);
            submitButton.setOnClickListener(new PreferenceOnClickListenter());
            
            //set the orginal value for EditView
            SharedPreferences settings = getSharedPreferences(PREF_FILENAME, 0);
            if( settings.contains("title") ){
                ((EditText) PrefManual.this.findViewById(R.id.pref_title)).setText(settings.getString("title", "defaultTitle"));
            }
            if( settings.contains("serial") ){
                ((EditText) PrefManual.this.findViewById(R.id.pref_serial)).setText(settings.getString("serial", "88888888"));
            }
        }

    }

    finish后返回,即时修改,同样也要在onResume中处理

    package com.delightPress.chap61.tabActivity;

    import com.delightPress.chap61.R;
    import com.delightPress.chap61.pref.PrefManual;

    import android.app.Activity;
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class Totalcome extends Activity {
        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.total);
            Button theButton = (Button) findViewById(R.id.quit_app);
            theButton.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    System.exit(0);
                }
                
            });
            
            //从用户首选项中读取首选设置
            SharedPreferences settings = getSharedPreferences(PrefManual.PREF_FILENAME, 0);
            TextView titleView = (TextView) findViewById(R.id.cpmpany_title);
            String companyTitle = String.format(getString(R.string.pref_title_string), settings.getString("title", "XXX Ltd."));
            titleView.setText(companyTitle);
            TextView serialView = (TextView) findViewById(R.id.company_serial);
            String companySerial = String.format(getString(R.string.pref_serial_string), settings.getString("serial", "12345678"));
            serialView.setText(companySerial);
        }

        @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();
            //从用户首选项中读取首选设置
            SharedPreferences settings = getSharedPreferences(PrefManual.PREF_FILENAME, 0);
            TextView titleView = (TextView) findViewById(R.id.cpmpany_title);
            String companyTitle = String.format(getString(R.string.pref_title_string), settings.getString("title", "XXX Ltd."));
            titleView.setText(companyTitle);
            TextView serialView = (TextView) findViewById(R.id.company_serial);
            String companySerial = String.format(getString(R.string.pref_serial_string), settings.getString("serial", "12345678"));
            serialView.setText(companySerial);
        }
        
        
    }

  • 相关阅读:
    Out of hay
    P3028 [USACO10OCT]汽水机Soda Machine
    P3619 魔法
    P2847 [USACO16DEC]Moocast(gold)奶牛广播-金
    P2830 写程序
    c#DateTime与unix时间戳互相转换
    C# UdpClient使用
    udp单播,广播,多播实现(ReceiveFromAsync,SendToAsync)
    udp广播,单播,多播
    C#实现异步阻塞TCP(Send,Receive,Accept,Connect)
  • 原文地址:https://www.cnblogs.com/xingmeng/p/2453706.html
Copyright © 2011-2022 走看看