[英]Context to show Toast from onClickListener class
I have a MainActivity that handles the UIs and buttons, but I have a separate class that handles the onClickListener
portion of the code. 我有一个MainActivity来处理UI和按钮,但我有一个单独的类来处理代码的
onClickListener
部分。 My Main class is defined as: public class MainActivity extends Activity
and my buttons in the class are defined like: 我的Main类定义为:
public class MainActivity extends Activity
,类中的按钮定义如下:
MyButtonListener l = new MyButtonListener();
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(l);
The MyButtonListener
class is defined as public class MyButtonListener extends MainActivity implements View.OnClickListener
. MyButtonListener
类定义为public class MyButtonListener extends MainActivity implements View.OnClickListener
。 The problem comes in, is when I try to make a toast in MyButtonListener
. 问题来自于,当我尝试在
MyButtonListener
举杯时。 I do not know what context to put for the toast. 我不知道吐司的背景。 I have tried: MainActivity.this, MyButtonListener.this, getContextApplication().
我尝试过:MainActivity.this,MyButtonListener.this,getContextApplication()。 None of them seemed to work.
他们似乎都没有工作。 This is how I am trying to show my toast:
这就是我试图展示吐司的方式:
Toast.makeText(MyButtonListener.this, message, Toast.LENGTH_LONG).show();
Does anyone what context to put in order to show the toast from the MyButtonListener
class? 有没有人为了从
MyButtonListener
类中显示toast而放置什么上下文?
Create Global variable 创建全局变量
Context context;
in MainActivity and set 在MainActivity和Set中
context = this;
in onCreate Method of main activity in onCreate主要活动方法
now pass the context in MyButtonListener Constructor like this 现在像这样在MyButtonListener构造函数中传递上下文
MyButtonListener l = new MyButtonListener(context);
b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(l);
and in MyButtonListener add constructor 并在MyButtonListener中添加构造函数
private Context context;
public MyButtonListener(Context context) {
this.context = context;
}
and use that context in toast 并在吐司中使用该上下文
add a Context
to your MyButtonListener
's constructor and use it to show the Toast
. 将一个
Context
添加到MyButtonListener
的构造函数中,并使用它来显示Toast
。
public MyButtonListener(Context c);
from MainActivity
: 来自
MainActivity
:
MyButtonListener l = new MyButtonListener(this);
Use View.getContext() from the parameters. 从参数中使用View.getContext()。
Eg: 例如:
...
void onClick(View v){
Context ctx = v.getContext();
}
...
This saves you from passing a context manually. 这样可以避免手动传递上下文。
It is not the context you are getting but a different context of a different reference, instead you can pass the reference
of the main activity to your constructor of the MyButtonListener and use the context that was passed on it. 它不是您获得的上下文,而是不同引用的不同上下文,而是您可以
pass the reference
主活动pass the reference
给MyButtonListener的构造函数 ,并使用在其上传递的上下文。
What you are doing is actually inheriting the mainactivity but not its reference by the time it is created so using the MyButtonListene.this
wont bring the toast on front . 你正在做的事实上是继承mainactivity而不是它的创建时间,所以使用
MyButtonListene.this
不会在前面带来吐司。 so extending mainactivity is completely useless/pointless. 所以扩展主要活动是完全没用的/没有意义的。
I do not see any reason to have MyButtonListener extend MainActivity. 我没有看到任何理由让MyButtonListener扩展MainActivity。
Do something like this: 做这样的事情:
public class MyButtonListener implements View.OnClickListener {
private Context context;
public MyButtonListener(Context context) {
this.context = context;
}
...
}
And pass the context from MainActivity: 并从MainActivity传递上下文:
MyButtonListener l = new MyButtonListener(this.getApplicationContext());
MyButtonListener l = new MyButtonListener(getApplicationContext());
and create parameterize constructor in
MyButtonListener
and use that Context
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.