从onClick侦听器调用方法 [英] calling a method from an onClick listener
问题描述
可能重复:
访问Java中内部类的包含类
Possible Duplicate:
Accessing the containing class of an inner class in Java
我知道以前肯定已经回答过这个问题,但是我已经连续两个晚上做了这个工作,我要么听不懂,要么就搞砸了. 我试图用按钮调用方法.我的方法仅是复制和粘贴,因此不会打开其他活动.
I know this has certainly been answered before, but I have been working on this for two straight nights and I either don't understand or I have messed something up badly. I am trying to call a method with a button. My method is only going to copy and paste so it's not opening another activity.
package com.example.copypastetest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class PDFtester extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdftester);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new OnClickListener(){;
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//public void work (View view){
this is my issue??-->Intent intent = new Intent(this, copyAsset.class);
startActivity(intent);}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_pdftester, menu);
return true;}
};
感谢您的关注,我承认我完全是个黑客.我到目前为止做的唯一原因是这个网站,第一个问题,我相信很多人,但希望有人能提供帮助.我不是要当吸血鬼,但如果有可以阅读的页面,我能全力以赴,那么我真的是在这里寻找正确的答案.
thank you for looking, I admit I am a total hack. The only reason I made it this far is this website, first question, im sure of many but hopefully someone can help. I am not trying to be a vampire but I really am looking for the correct answer here if there is a page i can read to understand what i did wrong i am all ears.
谢谢.
推荐答案
使用以下代码:
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
//public void work (View view){
Intent intent = new Intent(PDFtester.this, copyAsset.class);
startActivity(intent);}
}
注意:-诸如OnClickListener
,OnTouchListener
等的接口不使用this
来获取Context
尝试使用YourActivity.this
或getApplicationContext()
Note :- Interfaces like OnClickListener
,OnTouchListener
etc don't use this
for getting Context
try to use YourActivity.this
or getApplicationContext()
这篇关于从onClick侦听器调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!