创建类和使用它们纳入项目 [英] Creating classes and using them into project
问题描述
我有很多按钮,我的应用程序中,并为每一个按钮,我需要创建onClickListener,这将incerase的code的路线。
I have a lot of button inside my application and for every button i need to create onClickListener, this will incerase line of code's.
所以我创建onClickListener以下一类:
So i create a class for onClickListener below:
import android.app.Activity;
import android.content.Intent;
import android.view.View;
public class OnClickListener implements android.view.View.OnClickListener{
Activity current;
Class<?> goTo;
public OnClickListener(Activity arg0, Class arg1){
this.current = arg0;
this.goTo = arg1;
}
@Override
public void onClick(View v) {
Intent intent = new Intent(current, goTo);
}
}
现在我要到下一个活动时,点击按钮的,但是当我把这个行 startActivity(意向);
编译器会显示错误的方法startActivity(意向)是未定义的类型OnClickListener
我不知道我错了,我错过了什么。请帮我解决这个问题。
I dont know where i am going wrong and what i am missing. Please help me to solve this problem
推荐答案
您可以用另一种方式做太多,让你的类实现 OnClickListener
接口,然后实施的onClick(...)
方法,在该方法中,在视图的ID开关是这样的:
You can do in another way too, let your class implement the OnClickListener
interface and then implement onClick(...)
method, an in that method, switch on view's id like this:
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.btn1:
...
case R.id.btn2:
...
}
}
这篇关于创建类和使用它们纳入项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!