标签:ring linear 获取 text textview save 参数 xtend schema
数据的存储方式 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>
标签:ring linear 获取 text textview save 参数 xtend schema
原文地址:https://www.cnblogs.com/kevinOnes1/p/11290851.html