android偏好设置快捷键,Android共享偏好设置

Android提供了许多存储应用程序数据的方法。其中一种方法称为共享首选项。共享首选项允许您以键,值对的形式保存和检索数据。

为了使用共享首选项,您必须调用getSharedPreferences()方法,该方法返回指向包含首选项值的文件的SharedPreference实例。

SharedPreferences sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

第一个参数是键,第二个参数是MODE。除私人外,还有其他可用的模式,如下所示 -

序号

模式和描述

1

MODE_APPEND

这将使用现有的首选项附加新的首选项

2

MODE_ENABLE_WRITE_AHEAD_LOGGING

数据库打开标志。设置后,默认情况下将启用预写日志记录

3

MODE_MULTI_PROCESS

即使已经加载了共享首选项实例,此方法也将检查首选项的修改

4

MODE_PRIVATE

通过设置此模式,只能使用调用应用程序访问该文件

5

MODE_WORLD_READABLE

此模式允许其他应用程序读取首选项

6

MODE_WORLD_WRITEABLE

此模式允许其他应用程序编写首选项

您可以使用SharedPreferences.Editor类在共享首选项中保存一些内容。您将调用SharedPreference实例的edit方法,并将在编辑器对象中接收它。它的语法是

Editor editor = sharedpreferences.edit();

editor.putString("key", "value");

editor.commit();

除了putString方法之外,编辑器类中还有一些方法可以处理共享首选项中的数据。它们列出如下 -

Sr. NO

Mode & description

1

apply()

这是一种抽象的方法。它会将您的更改从编辑器提交回您正在调用的sharedPreference对象

2

clear()

它将从编辑器中删除所有值

3

remove(String key)

它将删除其键已作为参数传递的值

4

putLong(String key, long value)

它将在首选项编辑器中保存一个长值r

5

putInt(String key, int value)

它将在首选项编辑器中保存整数值

6

putFloat(String key, float value)

它将在首选项编辑器中保存浮点值

此示例演示了共享首选项的使用。它显示一个带有一些文本字段的屏幕,其值在应用程序关闭时保存,并在再次打开时返回。

要试验此示例,您需要在根据以下步骤开发应用程序后在实际设备上运行此操作 -

序号

描述

1

您将使用Android studio在com.example.sairamkrishna.myapplication包下创建Android应用程序。

2

修改src / MainActivity.java文件以添加进度代码以显示旋转进度对话框。

3

修改res / layout / activity_main.xml文件以添加相应的XML代码。

4

运行应用程序并选择正在运行的Android设备并在其上安装应用程序并验证结果。

以下是修改后的 MainActivity.java 的内容 。

package com.example.sairamkrishna.myapplication;

import android.content.Context;

import android.content.SharedPreferences;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

EditText ed1,ed2,ed3;

Button b1;

public static final String MyPREFERENCES = "MyPrefs" ;

public static final String Name = "nameKey";

public static final String Phone = "phoneKey";

public static final String Email = "emailKey";

SharedPreferences sharedpreferences;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

ed1=(EditText)findViewById(R.id.editText);

ed2=(EditText)findViewById(R.id.editText2);

ed3=(EditText)findViewById(R.id.editText3);

b1=(Button)findViewById(R.id.button);

sharedpreferences = getSharedPreferences(MyPREFERENCES, Context.MODE_PRIVATE);

b1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

String n = ed1.getText().toString();

String ph = ed2.getText().toString();

String e = ed3.getText().toString();

SharedPreferences.Editor editor = sharedpreferences.edit();

editor.putString(Name, n);

editor.putString(Phone, ph);

editor.putString(Email, e);

editor.commit();

Toast.makeText(MainActivity.this,"Thanks",Toast.LENGTH_LONG).show();

}

});

}

}

以下是已修改的主活动文件 res / layout / activiy_main.xml 的内容。

xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"

android:paddingRight="@dimen/activity_horizontal_margin"

android:paddingTop="@dimen/activity_vertical_margin"

android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Shared Preference "

android:id="@+id/textView"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:textSize="35dp" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Tutorials Point"

android:id="@+id/textView2"

android:layout_below="@+id/textView"

android:layout_centerHorizontal="true"

android:textSize="35dp"

android:textColor="#ff16ff01" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText"

android:layout_below="@+id/textView2"

android:layout_marginTop="67dp"

android:hint="Name"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText2"

android:layout_below="@+id/editText"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true"

android:hint="Pass" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:id="@+id/editText3"

android:layout_below="@+id/editText2"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_alignParentRight="true"

android:layout_alignParentEnd="true"

android:hint="Email" />

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="Save"

android:id="@+id/button"

android:layout_below="@+id/editText3"

android:layout_centerHorizontal="true"

android:layout_marginTop="50dp" />

以下是文件 res / values / strings.xml 的修改内容的内容

My Application

以下是内容默认文件 AndroidManifest.xml。

package="com.example.sairamkrishna.myapplication" >

android:allowBackup="true"

android:icon="@mipmap/ic_launcher"

android:label="@string/app_name"

android:theme="@style/AppTheme" >

android:name=".MainActivity"

android:label="@string/app_name" >

让我们尝试运行您的应用程序。我假设您已将实际的Android移动设备与计算机相关联。要从Android工作室运行应用程序,请打开项目的某个活动文件,然后单击

c7a2d120f35c003ac55fa8c77c44e2c4.png工具栏中的“运行”

图标。在开始申请之前,Android studio将显示以下窗口,以选择您要运行Android应用程序的选项。

a9d3ca742fb1058be6210eea85cf6cce.png

选择您的移动设备作为选项,然后检查您的移动设备,该设备将显示以下屏幕 -

现在只需在该字段中输入一些文字。就像我输入一些随机名称和其他信息,然后单击保存按钮。

bfe641256db0fa250122dc8b5962d4a5.png

现在,当您按下保存按钮时,文本将保存在共享首选项中。现在按后退按钮退出应用程序。现在再次打开它,您将看到您在应用程序中写回的所有文本。

bc06dfb3dcd791b7f147fbba1c526207.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值