Android如何从彩色选择器中保存拾取的颜色?

时间:2015-03-19 13:21:27

标签: android color-picker

我有一个带有libs的颜色选择器。 现在,当我点击按钮时,它会打开一个可以使用的所有颜色。当我点击一种颜色时,我想要这个颜色,我在一个按钮上... ...(如何在一个按钮上保存所选颜色) ? 对不起,我的英语不好.... main_activity.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/hello_world" />

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:text="Button" />

</LinearLayout>

Menu_java.java(Mainactivity)

import android.app.*;
import android.os.*;
import com.fourmob.colorpicker.ColorPickerDialog;
import com.fourmob.colorpicker.ColorPickerSwatch.OnColorSelectedListener;
import android.graphics.*;
import android.widget.*;
import android.view.*;
import android.support.v4.app.*;


public class Menu_java extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.menu_layout);

        final ColorPickerDialog colorPickerDialog = new ColorPickerDialog();
        colorPickerDialog.initialize(R.string.dialog_title, new int[] { Color.CYAN, Color.LTGRAY, Color.BLACK, Color.BLUE, Color.GREEN, Color.MAGENTA, Color.RED, Color.GRAY, Color.YELLOW }, Color.YELLOW, 3, 2);
        colorPickerDialog.setOnColorSelectedListener(new OnColorSelectedListener() {

                @Override
                public void onColorSelected(int color) {
                    Toast.makeText(Menu_java.this, "selectedColor : " + color, Toast.LENGTH_SHORT).show();
                }
            });

        findViewById(R.id.button1).setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    colorPickerDialog.show(getSupportFragmentManager(), "colorpicker");
                }
            });
    }





        }

1 个答案:

答案 0 :(得分:0)

你可以将它保存在这样的共享偏好中:

 private SharedPreferences sharedprefs = getSharedPreferences(yourapplicationnamehere), MODE_PRIVATE);
sharedprefs.edit().putInt("colorint" , color).apply();

并按如下方式阅读:

int colorint = sharedprefs.getString("colorint" , 0);