Android——sharepreference的实例

    xiaoxiao2021-03-25  71

    这里做一个很有趣的项目:

    通过自己设置自己app中TextView的背景颜色, 下次打开该app时候TextView背景颜色

    就是上次选的颜色。

    通过sharepreference配置文件就可以实现这样的功能:

    先上效果图:

    下次打开时候:

    未选择下面颜色时,也是默认绿色。

    我们来打开配置文件看看:

    废话不多说,直接上代码:

    mainactivity:

    package com.example.textsharepre; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.graphics.Color; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.CheckBox; import android.widget.CompoundButton; import android.widget.CompoundButton.OnCheckedChangeListener; import android.widget.TextView; public class MainActivity extends Activity implements OnCheckedChangeListener { private CheckBox check1, check2, check3; private TextView text; private String colorselected; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initViews(); Settcolor(); registListener(); } private void Settcolor() { // TODO Auto-generated method stub SharedPreferences sp = getSharedPreferences("color", Context.MODE_PRIVATE); colorselected = sp.getString("colorselect", "NO Found"); text.setBackgroundColor(sp.getInt(colorselected, -1)); } private void registListener() { // TODO Auto-generated method stub check1.setOnCheckedChangeListener(this); check2.setOnCheckedChangeListener(this); check3.setOnCheckedChangeListener(this); } private void initViews() { // TODO Auto-generated method stub check1 = (CheckBox) findViewById(R.id.check1); check2 = (CheckBox) findViewById(R.id.check2); check3 = (CheckBox) findViewById(R.id.check3); text = (TextView) findViewById(R.id.text); } @Override public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { // TODO Auto-generated method stub SharedPreferences sp = getSharedPreferences("color", Context.MODE_PRIVATE); SharedPreferences.Editor editor = sp.edit(); switch (buttonView.getId()) { case R.id.check1: if (isChecked) text.setBackgroundColor(Color.RED); editor.putInt("red", Color.RED); colorselected = "red"; editor.putString("colorselect", colorselected); editor.commit(); break; case R.id.check2: if (isChecked) text.setBackgroundColor(Color.GREEN); editor.putInt("green", Color.GREEN); colorselected = "green"; editor.putString("colorselect", colorselected); editor.commit(); break; case R.id.check3: if (isChecked) text.setBackgroundColor(Color.BLUE); editor.putInt("blue", Color.BLUE); colorselected = "blue"; editor.putString("colorselect", colorselected); editor.commit(); break; default: break; } } }

    通过每次点击颜色按钮都会存入对应的颜色,只要每次占存的时候获取颜色的key值,这

    里用colorselected获取key,就可以在下次 打开的时候通过获取的key设置颜色。

    activity_main xml:

    <RelativeLayout 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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.example.textsharepre.MainActivity" > <TextView android:id="@+id/text" android:layout_width="130dp" android:layout_height="130dp" android:layout_centerInParent="true" android:text="@string/hello_world" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" android:layout_centerHorizontal="true" android:orientation="horizontal" > <CheckBox android:id="@+id/check1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" android:text="红" /> <CheckBox android:id="@+id/check2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" android:layout_toRightOf="@id/check1" android:text="绿" /> <CheckBox android:id="@+id/check3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@id/text" android:layout_toRightOf="@id/check2" android:text="蓝" /> </LinearLayout> </RelativeLayout>
    转载请注明原文地址: https://ju.6miu.com/read-40320.html

    最新回复(0)