zoukankan      html  css  js  c++  java
  • 安卓学习-数据存储与IO-SharedPreferences

    安卓学习-数据存储与IO-SharedPreferences

    运行完后,可以在DDMS,看到这个xml文件

    MainActivity.java

    public class MainActivity extends Activity implements OnClickListener{
    
        EditText editText1;
        TextView textView3;
        SharedPreferences pre;
        Editor editor;
    
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            
            editText1=(EditText)findViewById(R.id.editText1);
            textView3=(TextView)findViewById(R.id.textView3);
            
            pre=getSharedPreferences("myPre222", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
            editor=pre.edit();
            
            Button btn1=(Button)findViewById(R.id.button1);
            Button btn2=(Button)findViewById(R.id.button2);
            btn1.setOnClickListener(this);
            btn2.setOnClickListener(this);
        }
    
        public void onClick(View v) {
            if(v==findViewById(R.id.button1)){
                editor.putString("name", editText1.getText()+"");
                editor.commit();
            }else if(v==findViewById(R.id.button2)){
                String name=pre.getString("name", "");
                textView3.setText(name);
            }
        }
    }
    View Code

    activity_main.xml

    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="${relativePackage}.${activityClass}" >
    
        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/editText1"
            android:layout_alignBottom="@+id/editText1"
            android:layout_alignParentLeft="true"
            android:text="姓名"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <EditText
            android:id="@+id/editText1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentTop="true"
            android:layout_marginLeft="15dp"
            android:layout_marginTop="15dp"
            android:layout_toRightOf="@+id/textView1"
            android:ems="10" >
    
            <requestFocus />
        </EditText>
    
        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/editText1"
            android:layout_marginTop="22dp"
            android:text="写入" />
    
        <Button
            android:id="@+id/button2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/button1"
            android:layout_marginTop="36dp"
            android:text="读取" />
    
        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/button2"
            android:layout_alignBottom="@+id/button2"
            android:layout_marginLeft="11dp"
            android:layout_toRightOf="@+id/button2"
            android:text="值:"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
        <TextView
            android:id="@+id/textView3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignBaseline="@+id/textView2"
            android:layout_alignBottom="@+id/textView2"
            android:layout_centerHorizontal="true"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    
    </RelativeLayout>
    View Code

     别的应用程序来读取这个xml

    try {
                        Context context=createPackageContext("com.example.fffff", Context.CONTEXT_IGNORE_SECURITY);
                        SharedPreferences pre=context.getSharedPreferences("myPre222", Context.MODE_WORLD_READABLE+Context.MODE_WORLD_WRITEABLE);
                        String name=pre.getString("name", "");
                        Toast.makeText(MainActivity.this, name, Toast.LENGTH_SHORT).show();
                    } catch (NameNotFoundException e) {
                        // TODO 自动生成的 catch 块
                        e.printStackTrace();
                    }
    View Code

     MODE_WORLD_READABLE和MODE_WORLD_WRITEABLE已经在Android 4.2(API level 17)废弃了,因为这样危险,安全性不高。

  • 相关阅读:
    AngularJS Directive 隔离 Scope 数据交互
    Web(Jsp+ Servlet)开发中如何解决中文乱码问题
    MySQL中进行模糊搜索的一些问题
    RequireJS 模块的定义与加载
    Mysql 正则表达式 判断字段值不包含数字
    使用命令行将Excel数据表导入Mysql中的方法小结
    js 去除字符串左右两端的空格
    js 计算两个时间差
    在MySQL中创建实现自增的序列(Sequence)的教程
    ajax post传值
  • 原文地址:https://www.cnblogs.com/weijj/p/4200975.html
Copyright © 2011-2022 走看看