[英]Using the same 'OnClickListener' in different Activities
I was defining and using a custom OnClickListener (ie MyOnClickListener
) inside the file of the Fragment I was using at the moment. 我正在定义和使用一个自定义的OnClickListener (即MyOnClickListener
),此片段在我当前正在使用的Fragment文件中。
Keeping writing code I realized that I needed the same listener also in another Fragment that happened to be in another Activity as well. 继续编写代码,我意识到在另一个恰好在另一个Activity中的 Fragment中, 我也需要相同的侦听器 。
I created therefore the file MyOnClickListener.java
copying all the code I was using in the first Fragment for it earlier, but now I get the following errors: 因此,我创建了文件MyOnClickListener.java
复制之前在第一个Fragment中使用的所有代码,但是现在出现以下错误:
Cannot resolve method 'getActivity()' 无法解析方法“ getActivity()”
Cannot resolve method 'getResources()' 无法解析方法“ getResources()”
Note: I read on stackoverflow that one solution may be just writing MainActivity.this
in place of getActivity()
, but in my situation I need to use the same in two different activities . 注意:我在stackoverflow上读到,一种解决方案可能只是编写MainActivity.this
来代替getActivity()
,但是在我的情况下,我需要在两个不同的活动中使用相同的方法。 What should I do? 我该怎么办?
EDIT: Here's the code of MyOnClickListener
It just has to display a matrix of icons: 编辑:这是MyOnClickListener
的代码, MyOnClickListener
需要显示一个图标矩阵:
class MyOnClickListener implements View.OnClickListener {
private LabeledButton labeledButton;
MyOnClickListener(LabeledButton labeledButton) {
super();
this.labeledButton = labeledButton;
}
@Override
public void onClick(View view) {
Button iconButton = (Button) view;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); // ERROR HERE
LayoutInflater layoutInflater = getActivity().getLayoutInflater(); // ERROR HERE
final View viewLayout = layoutInflater.inflate(R.layout.dialog_matrix_icons, null);
builder.setView(viewLayout);
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Do nothing
}
});
final AlertDialog alertDialog = builder.create();
// Set-up listeners of icon button
Button imageButtons[][] = new Button[3][3];
int i;
int j;
for (i = 0; i < 3; i++) {
for (j = 0; j < 3; j++) {
Resources res = getResources(); // ERROR HERE
TypedArray icons = res.obtainTypedArray(R.array.listIcon);
int idDrawable = icons.getResourceId(i + 3 * j, 1); // ERROR HERE
icons.recycle();
LinearLayout grid = (LinearLayout) viewLayout;
LinearLayout row = (LinearLayout) grid.getChildAt(i);
imageButtons[i][j] = (Button) row.getChildAt(j); // Retrieve the right image in the grid
imageButtons[i][j].setBackgroundResource(idDrawable);
String nameIcon = getResources().getResourceEntryName(idDrawable); // ERROR HERE
ImageOnClickListener imageOnClickListener = new ImageOnClickListener(iconButton, alertDialog, idDrawable, nameIcon, labeledButton);
imageButtons[i][j].setOnClickListener(imageOnClickListener);
}
}
alertDialog.show();
}
} }
Replace this with your constructor 用您的构造函数替换
MyOnClickListener(LabeledButton labeledButton,Context context) {
super();
this.labeledButton = labeledButton;
this.context = context;
}
Make sure you create context variable inside your MyOnClickListenerClass if not you will find error at the constructor line.Then you can replace all the getActivity()
to context
. 如果没有,请确保在MyOnClickListenerClass内创建上下文变量,否则将在构造函数行发现错误。然后可以将所有getActivity()
替换为context
。
When you initiallize your MyOnClickListener
make sure to pass the context parameter 初始化MyOnClickListener
时,请确保传递context参数
For your inflater use this 为您的充气机使用
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)
I read on stackoverflow that one solution may be just writing MainActivity.this in place of getActivity(), but in my situation I need to use the same in two different activities. 我在stackoverflow上读到,一种解决方案可能只是编写MainActivity.this来代替getActivity(),但是在我的情况下,我需要在两个不同的活动中使用相同的方法。 What should I do? 我该怎么办?
If you put the OnClickListener in its own compilation unity, you can't use whawt you read on Stackoverflow . 如果将OnClickListener放在自己的编译单元中,则不能使用在Stackoverflow上阅读过的东西 。 MainActivity.this
, refers to a concrete and current instance of MainActivity
(please, look it up what the keyword this
means in java). MainActivity.this
,指的是一个具体的和当前实例MainActivity
(拜托,看看它是什么关键字this
手段在Java)。 If you need a Context, you can the parameter you get in the onClick
callaback, View view
, to retrieve it. 如果需要上下文,可以在onClick
回调View view
中获取的参数进行检索。 Read more here 在这里阅读更多
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.