zoukankan      html  css  js  c++  java
  • Android-------------------数据存储

    JAVA:

    package com.example.myapplication;
     
    import androidx.appcompat.app.AppCompatActivity;
     
    import android.content.SharedPreferences;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.TextView;
    import android.widget.Toast;
     
    public class SharedActivity extends AppCompatActivity {
        private EditText shared1;
        private Button shared2,shared3;
        private TextView shared4;
        private SharedPreferences mSharedPreferences;
        private SharedPreferences.Editor mEditor;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_shared);
            shared1=findViewById(R.id.shared1);
            shared2=findViewById(R.id.shared2);
            shared3=findViewById(R.id.shared3);
            shared4=findViewById(R.id.shared4);
     
            mSharedPreferences=getSharedPreferences("data",MODE_PRIVATE);
            mEditor=mSharedPreferences.edit();
     
            shared2.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   mEditor.putString("name",shared1.getText().toString());
                   mEditor.commit();
                    Toast.makeText(SharedActivity.this,"保存完毕",Toast.LENGTH_SHORT).show();
                }
            });
            shared3.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   shared4.setText(mSharedPreferences.getString("name",""));
                }
            });
        }
    

      

    XML:

     
    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:padding="10dp"
       >
        <EditText
            android:layout_marginTop="50dp"
            android:id="@+id/shared1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="请输入要保存的名字:"
            android:textSize="20sp"/>
        <Button
            android:layout_marginTop="20dp"
            android:id="@+id/shared2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="保存"/>
        <Button
            android:id="@+id/shared3"
            android:layout_marginTop="20dp"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"
            android:text="查看"/>
        <TextView
            android:layout_marginTop="40dp"
            android:id="@+id/shared4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:textSize="20dp"/>
     
    </LinearLayout>
    

      

  • 相关阅读:
    腾讯之困,QQ与微信各有各的烦恼
    Attribute(一)——提前定义特性
    假设有来生
    codeforces248(div1) B Nanami&#39;s Digital Board
    驳斥苹果“诊断后门论”,声援扎德尔斯基
    python批量下载色影无忌和蜂鸟的图片 爬虫小应用
    建筑建模学习笔记2——3DMax房屋框架建模
    【大话QT之十六】使用ctkPluginFramework插件系统构建项目实战
    javaObject类
    java基本类型和String之间的转换
  • 原文地址:https://www.cnblogs.com/xuwei123456/p/13972584.html
Copyright © 2011-2022 走看看