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

    1.当点击 数据存储 进入 数据存储界面 :

     2.  在请输入要保存的名字 输入名字 点击保存 如下:

       点击查看:

    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>
  • 相关阅读:
    题解 P3717 【[AHOI2017初中组]cover】
    【题解】 [POI2012]FES-Festival (差分约束)
    【题解】 [HNOI2005]狡猾的商人(差分约束)
    【题解】 [SCOI2011]糖果 (差分约束)
    【题解】 POJ 1201 Intervals(差分约束)
    【题解】 Codeforces 919F A Game With Numbers(拓扑排序+博弈论+哈希)
    【题解】 [ZJOI2012]灾难 (拓扑排序+LCA)
    【题解】 [HAOI2016]食物链 (拓扑排序)
    【题解】 Luogu P1402 酒店之王 (二分图匹配)
    【题解】 [NOI2009]变换序列 (二分图匹配)
  • 原文地址:https://www.cnblogs.com/x20425535/p/14086426.html
Copyright © 2011-2022 走看看