zoukankan      html  css  js  c++  java
  • 存储

    DataActivity.java

    package com.hanqi.test2;
    
    import android.content.SharedPreferences;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.EditText;
    import android.widget.Toast;
    
    public class DataActivity1 extends AppCompatActivity {
        EditText et_1;
        EditText et_2;
        SharedPreferences sp;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_data1);
            et_1=(EditText)findViewById(R.id.et_1);
            et_2=(EditText)findViewById(R.id.et_2);
            sp=getSharedPreferences("mydata",MODE_PRIVATE);
        }
        public void bt_1onclick(View v){
            String key= et_1.getText().toString();
            String value= et_2.getText().toString();
            if (key.length()==0 || value.length()==0){
                Toast.makeText(DataActivity1.this, "key,value不能为空", Toast.LENGTH_SHORT).show();
            }
            SharedPreferences.Editor editor= sp.edit();
            editor.putString(key,value);
            boolean b = editor.commit();
            if (b){
                Toast.makeText(DataActivity1.this, "yes", Toast.LENGTH_SHORT).show();
            }else {
                Toast.makeText(DataActivity1.this, "no", Toast.LENGTH_SHORT).show();
            }
    
        }
        public void bt_2onclick(View v){
            String key = et_1.getText().toString();
            et_2.setText(sp.getString(key,"没有发现key"));
    
    
        }
    }

    layout_data1.xml

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout 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="com.hanqi.test2.DataActivity1"
        android:orientation="vertical">
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_1"
            android:hint="key"/>
        <EditText
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/et_2"
            android:hint="value"/>
        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="保存"
                android:onClick="bt_1onclick"/>
            <Button
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_weight="1"
                android:text="读取"
                android:onClick="bt_2onclick"/>
        </LinearLayout>
    
    </LinearLayout>
  • 相关阅读:
    [转]几个开源的.net界面控件
    电脑上设置对眼睛友好的绿豆沙色
    [转] C# 绘制报表,使用Graphics.DrawString 方法
    Excel 绘制图表,如何显示横轴的数据范围
    [转] C#中绘制矢量图形
    Chapter 3 Protecting the Data(3):创建和使用数据库角色
    续x奇数倍(n+2*x)暴力算法是冠军的算法结合数量
    新秀学习SSH(十四)——Spring集装箱AOP其原理——动态代理
    中国是大数据的人工智能的发源地
    采用shell脚本统计代码的行数
  • 原文地址:https://www.cnblogs.com/WY404683569/p/5368804.html
Copyright © 2011-2022 走看看