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)废弃了,因为这样危险,安全性不高。

  • 相关阅读:
    ubuntu 安装 redis desktop manager
    ubuntu 升级内核
    Ubuntu 内核升级,导致无法正常启动
    spring mvc 上传文件,但是接收到文件后发现文件变大,且文件打不开(multipartfile)
    angular5 open modal
    POJ 1426 Find the Multiple(二维DP)
    POJ 3093 Margritas
    POJ 3260 The Fewest Coins
    POJ 1837 Balance(二维DP)
    POJ 1337 A Lazy Worker
  • 原文地址:https://www.cnblogs.com/weijj/p/4200975.html
Copyright © 2011-2022 走看看