Android 我是否应该使用AlertDialog来显示几个用于更改ImageButton背景的选项?
我正在努力实现这一结果: 单击ImageButton后,将显示一个AlertDialog(或另一个用于弹出的功能,背景变暗),允许将5个其他小图像设置为所述按钮的背景。单击所选图像后,AlertDialog或弹出窗口消失,新图像设置为ImageButton背景 到目前为止,我有这段代码,我知道的不多,但由于某些原因,我无法进一步了解我的错误:Android 我是否应该使用AlertDialog来显示几个用于更改ImageButton背景的选项?,android,imagebutton,Android,Imagebutton,我正在努力实现这一结果: 单击ImageButton后,将显示一个AlertDialog(或另一个用于弹出的功能,背景变暗),允许将5个其他小图像设置为所述按钮的背景。单击所选图像后,AlertDialog或弹出窗口消失,新图像设置为ImageButton背景 到目前为止,我有这段代码,我知道的不多,但由于某些原因,我无法进一步了解我的错误: package com.test.test; import android.app.AlertDialog; import android.conten
package com.test.test;
import android.app.AlertDialog;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
public class PageTwoFragment extends Fragment {
int i = 0;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(
R.layout.page2_layout, container, false);
final ImageButton pp_btn1 = (ImageButton) rootView.findViewById(R.id.m1_btn);
final ImageButton m1_ts_btn = (ImageButton) rootView.findViewById(R.id.m1_ts_btn);
final Context context = this;
pp_btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i +=1;
if (i % 2 == 0) {
pp_btn1.setImageResource(R.drawable.pause);
} else {
pp_btn1.setImageResource(R.drawable.play);
}
}
});
m1_ts_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("My Title");
}
});
return rootView;
}
}
错误1:“最终上下文=此;
”
这表示“类型不匹配:无法从PageTwoFragment转换为上下文”
错误2:“新建AlertDialog.Builder(此);
”
上面写着“构造函数AlertDialog.Builder(new View.OnClickListener(){})未定义”
有谁能解释一下我哪里出了问题,然后直接告诉我如何实现我所需要的
第一个错误出现在“final Context=this;”上,上面写着“Type”
不匹配:无法从PageTwoFragment转换为上下文“
片段不是上下文的子类。活动非常活跃。获取片段附加到的活动:
final Context context = getActivity();
第二个错误出现在“newalertdialog.Builder(this);”上,上面说
“构造函数AlertDialog.Builder(new View.OnClickListener(){})是
未定义”
在此处使用先前设置的上下文:
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);