1

我对android开发相当陌生,想在片段中使用Android-Color_picker“AmbilWarna”。我收到错误消息:

The constructor AmbilWarnaDialog(HomeFragment, int, new OnAmbilWarnaListener(){}) is undefined.

这是因为我使用的是 Fragment 而不是 Fragment Activity 我使用的教程使用的是 Activity。

我正在使用以下教程: http ://wptrafficanalyzer.in/blog/android-color-picker-application-using-ambilwarna-color-picker-library/

public class HomeFragment extends SherlockFragment implements TabListener {


private View homeView;

@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
        Bundle savedInstanceState) {

    homeView = inflater.inflate(R.layout.homefragment, container, false);

    Button sColorBtn = (Button) homeView.findViewById(R.id.button2);
    OnClickListener clickListener = new OnClickListener() {

        @Override
        public void onClick(View v) {
            colorpicker();
        }
    };

    // Setting click event listener for the button
    sColorBtn.setOnClickListener(clickListener);
    return sColorBtn;
}

public void colorpicker() {
    //     initialColor is the initially-selected color to be shown in the rectangle on the left of the arrow.
    //     for example, 0xff000000 is black, 0xff0000ff is blue. Please be aware of the initial 0xff which is the alpha.

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

        // Executes, when user click Cancel button
        @Override
        public void onCancel(AmbilWarnaDialog dialog){
        }

        // Executes, when user click OK button
        @Override
        public void onOk(AmbilWarnaDialog dialog, int color) {
            Toast.makeText(getBaseContext(), "Selected Color : " + color, Toast.LENGTH_LONG).show();
        }
    });
    dialog.show();
}
4

2 回答 2

2

用这个:

AmbilWarnaDialog dialog = new AmbilWarnaDialog(getActivity().getApplicationContext(), 0xff0000ff, new OnAmbilWarnaListener() {

    // Executes, when user click Cancel button
    @Override
    public void onCancel(AmbilWarnaDialog dialog){
    }

    // Executes, when user click OK button
    @Override
    public void onOk(AmbilWarnaDialog dialog, int color) {
        Toast.makeText(getBaseContext(), "Selected Color : " + color, Toast.LENGTH_LONG).show();
    }
});

所以你必须使用getActivity().getApplicationContext()而不是this. 它将与上下文一起返回。

于 2013-01-16T21:36:47.373 回答
0

如果您想要颜色选择器的片段解决方案,我已经制作了一个android-color- picker 的分支,其中使用了DialogFragment并在配置更改时重新创建。这是链接:https ://github.com/lomza/android-color-picker

于 2013-09-30T16:10:52.683 回答