Android 学习 03

Posted kevinones1

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Android 学习 03相关的知识,希望对你有一定的参考价值。

数据的存储方式  SharedPreferences

serivce层

 

package com.aaa.service;

import android.content.Context;
import android.content.SharedPreferences;

import java.util.HashMap;
import java.util.Map;

public class PreferencesService 
    private Context context;

    public PreferencesService(Context context) 
        this.context = context;
    

    /**
     *  保存偏好设置
     * @param name 名称
     * @param age 年龄
     */
    public void save(String name, String age)
        Integer age1 = Integer.valueOf(age);
        SharedPreferences share = context.getSharedPreferences("myset",Context.MODE_PRIVATE);
        SharedPreferences.Editor editor= share.edit();
        editor.putString("name",name);
        editor.putInt("age",age1);
        editor.commit();
    

    /**
     * 获取各项配置参数
     * @return  map 一个集合包含键 和参数值
     */
    public Map<String, String> getPreferences()
        Map<String ,String> map = new HashMap<String,String>();
        SharedPreferences share = context.getSharedPreferences("myset",Context.MODE_PRIVATE);
       String name =  share.getString("name","");
       int age = share.getInt("age",0);
       map.put("name",name);
       map.put("age",String.valueOf(age));
       return map;
    

 

  activity层

package com.aaa.sharepreferences;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.aaa.service.PreferencesService;

import java.util.Map;

public class MainActivity extends AppCompatActivity 

    private EditText editText1 ;
    private EditText editText2 ;
    private PreferencesService service;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editText1  = (EditText) this.findViewById(R.id.name);
        editText2 = (EditText) this.findViewById(R.id.age);
        service = new PreferencesService(this);
        Map<String,String> map = service.getPreferences();
        editText2.setText(map.get("age"));      // 回显参数 age
        editText1.setText(map.get("name"));     // 回显参数name
    
    public void save(View view)
        String name = editText1.getText().toString();
        String age = editText2.getText().toString();
        service.save(name, age);
        Toast.makeText(getApplicationContext(),R.string.tip,Toast.LENGTH_LONG).show();
    

  res/layout/activity_main.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:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/name"
        />
    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="@string/name"
        android:autofillHints="@string/name"
        tools:targetApi="o" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/age" />
    <EditText
        android:id="@+id/age"
        android:hint="@string/age"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="number"
        />
    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:onClick="save"
        />
</LinearLayout>

  res/values/string.xml

<resources>
    <string name="app_name">软件参数设置</string>
    <string name="name">输入姓名</string>
    <string name="age">输入年龄</string>
    <string name="tip">保存成功</string>
    <string name="save">保存设置</string>
</resources>

  

 

以上是关于Android 学习 03的主要内容,如果未能解决你的问题,请参考以下文章

Android 学习 03

ANDROID_MARS学习笔记_S03_007_GoogleMap1

ANDROID_MARS学习笔记_S03_009_GOOGLEMAP3

学习进度2020.02.03

Android Handler消息机制03-Message源码学习

Android Handler消息机制03-Message源码学习