我对Android开发相当新,并希望在片段中使用Android-Color_picker“AmbilWarna”。我收到了错误:
The constructor AmbilWarnaDialog(HomeFragment, int, new OnAmbilWarnaListener(){}) is undefined.
这是因为我使用Fragment而不是Fragment活动我使用的教程使用了Activity。
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();
}
答案 0 :(得分: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
。它将与Context一起返回。
答案 1 :(得分:0)
如果你想要一个颜色选择器的片段解决方案,我已经使用 android-color-picker 的分支,其中使用 DialogFragment 并在配置更改时重新创建。这是链接:https://github.com/lomza/android-color-picker