【问题标题】:Saving color int from ColorPicker Android?从 ColorPicker Android 保存颜色 int?
【发布时间】:2015-05-20 19:37:41
【问题描述】:

我在 Android 中使用 ColorPicker。当我从ColorPicker 设置所需的颜色时,它可以工作。但是当我退出并重新启动我的activity 时,我的颜色又恢复为默认值。我已经解决这个问题超过 2 周了,并尝试了 SharedPreferences 的每一件事!这是我的代码:

MainActivity.java:

package com.example.project;

import yuku.ambilwarna.AmbilWarnaDialog;
import yuku.ambilwarna.AmbilWarnaDialog.OnAmbilWarnaListener;
import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;

public class Preferences extends Activity {

    String[] pref_list = { "Background Color", "Background Wallpaper" };
    RelativeLayout background;
    public static final String MY_PREFS_NAME = "MyPrefs";
    Editor editor;
    SharedPreferences sharedpreference;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

        setContentView(R.layout.preferences);

        background = (RelativeLayout) findViewById(R.id.p_rl);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                R.layout.lv_activity, pref_list);

        ListView listView = (ListView) findViewById(R.id.pref_list);
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                if (position == 0) {

                    colorpicker();
                } else {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(
                            Intent.createChooser(intent, "Select Picture"), 1);
                }
            }
        });
    }

    public void colorpicker() {

        AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, 0xff0000ff,
                new OnAmbilWarnaListener() {

                    @Override
                    public void onCancel(AmbilWarnaDialog dialog) {
                    }

                    @Override
                    public void onOk(AmbilWarnaDialog dialog, int color) {
                        SharedPreferences sharedpreference = getApplicationContext()
                                .getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
                        Editor editor = sharedpreference.edit();
                        editor.putInt("Color code", color);
                        editor.commit();
                        Preferences.this.findViewById(R.id.p_rl)
                                .setBackgroundColor(color);
                        Preferences.this.findViewById(R.id.pref_list)
                                .setBackgroundColor(color);

                    }

                });
        dialog.show();
    }

    public void run(View view) {
        SharedPreferences sharedpreference = getSharedPreferences(
                MY_PREFS_NAME, MODE_PRIVATE);
        sharedpreference.getInt("Color code", 0);

        editor.commit();

    }
}

但这不是简单的工作!我的代码有什么问题?

【问题讨论】:

  • 没人愿意帮忙吗?

标签: android sharedpreferences color-picker


【解决方案1】:

您需要在 onCreate 或 onResume 上获取保存在 sharedPreference 上的数据。 因此,一旦应用启动,您就可以应用您保存的任何颜色。

【讨论】:

  • 请问你能告诉我怎么做吗?我会非常感谢你!
  • 将 sharedpreference 设为全局变量 将此代码移至 onCreate sharedpreference = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE); 然后 sharedpreference.getInt("Color code", 0);
  • int t = sharedpreference.getInt("Color code", 0) Preferences.this.findViewById(R.id.p_rl) .setBackgroundColor(colorCode); Preferences.this.findViewById(R.id.pref_list).setBackgroundColor(colorCode);
  • 但我无法使我的 int 颜色全局化,这是个大问题!
  • 你不必让它全球化。您已经将其保存在您的共享首选项中。 sharedpreference 数据将一直存在,直到您清除应用程序数据或将其卸载。下面的代码将获得您保存在共享首选项中的颜色代码int colorCode = sharedpreference.getInt("Color code", 0) Preferences.this.findViewById(R.id.p_rl) .setBackgroundColor(colorCode); Preferences.this.findViewById(R.id.pref_list).setBackgroundColor(colorCode); 这将在 onCreate()
【解决方案2】:
  //add param in that function
  public void colorpicker(int color) { 

        AmbilWarnaDialog dialog = new AmbilWarnaDialog(this, color,
                new OnAmbilWarnaListener() { 
        //your code
        }
  }


  //call this function in that way:
  colorpicker(getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).getInt("Color code", 0xff0000ff));

所以只需使用保存的数据。目前你不这样做。

【讨论】:

  • 我应该什么时候调用这个函数?
  • 不知道你什么时候需要。什么时候需要显示这个对话框?顺便说一句,你看到这是你的代码,只是稍微修改了吗?
  • 在 listview 项目上单击它会显示一个对话框,但是当我将 int color 添加到 colorpicker() 时,我无法调用该对话框!
  • 只需复制我的代码。一切都已经为你做好了。你没看到函数签名也被修改了吗?
  • 意味着我有一个列表视图,当用户选择其中一个选项时,会弹出 ColorPicker 对话框,但是由于我已将 int color 添加到 colorpicker() 我无法调用该方法if (position == 0) { colorpicker(); }
【解决方案3】:

像这样改变你的 onItemClick 监听器

listView.setOnItemClickListener(new OnItemClickListener() {
        @Override
    public void onItemClick(AdapterView<?> parent, View view,
            int position, long id) {
SharedPreferences sharedPreferences = getApplicationContext()
                        .getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);

    sharedPreferences.edit().putInt("color", position).commit();
        if (position == 0) {

            //colorpicker();
        } else {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(
                    Intent.createChooser(intent, "Select Picture"), 1);
        }
    }
});

并将其添加到 onCreate

SharedPreferences sharedPreferences = getApplicationContext().getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE);
int selectedColor = sharedpreference.getInt("color", 0);
listView.setItemChecked(selectedColor, true);

【讨论】:

  • 强制关闭。请你给我详细的答案,我已经被这个问题困扰了好几个星期了!!!
猜你喜欢
  • 2011-01-12
  • 2017-10-25
  • 2016-08-02
  • 1970-01-01
  • 2011-05-09
  • 1970-01-01
  • 1970-01-01
  • 2011-10-19
  • 2015-10-28
相关资源
最近更新 更多