Android 创建自定义OnClick侦听器
我有一个按钮列表,我的OCL需要知道我按下了哪个索引。 计划是这样的:Android 创建自定义OnClick侦听器,android,onclicklistener,Android,Onclicklistener,我有一个按钮列表,我的OCL需要知道我按下了哪个索引。 计划是这样的: MyOnClickListener onClickListener = new MyOnClickListener() { @Override public void onClick(View v) { Intent returnIntent = new Intent(); returnIntent.putExtra("deleteAtInd
MyOnClickListener onClickListener = new MyOnClickListener() {
@Override
public void onClick(View v) {
Intent returnIntent = new Intent();
returnIntent.putExtra("deleteAtIndex",idx);
setResult(RESULT_OK, returnIntent);
finish();
}
};
for (int i =0;i<buttonList.size();i++) {
buttonList.get(i).setText("Remove");
buttonList.get(i).setOnClickListener(onClickListener);
}
}
但是,我不确定在OCL的构造函数中以及重写的
onClick
函数中需要做什么。将setOnClickListener
设置为按钮:
buttonList.get(i).setOnClickListener(new MyOnClickListener(i));
编辑:
我需要完成myOCL中的一项活动,我将如何完成
要在非活动类中单击按钮完成活动,您需要将活动上下文传递给自定义OnClickListener,如下所示:
buttonList.get(i).setOnClickListener(new MyOnClickListener(i, Your_Current_Activity.this));
并将自定义OnClickListener类的构造函数更改为:
int index;
Context context;
public MyOnClickListener(int index,Context context)
{
this.index = index;
this.context=context;
}
@Override
public void onClick(View arg0) {
// now finish Activity as\
context.finish();
// OR
// ((Activity)context).finish();
}
您将在视图中获得按钮值,以便找到索引所在的位置。更改for循环,每次创建新对象,并在构造函数中传递索引
for (int i =0;i<buttonList.size();i++) {
buttonList.get(i).setText("Remove");
buttonList.get(i).setOnClickListener(new MyOnClickListener(i));
}
for(int i=0;i我不确定这是否适用于您的案例,但我希望为可单击元素创建一个自定义侦听器,以防止它们被单击两次(如果用户快速点击)
我的解决方案是创建一个侦听器类,我将自定义Runnable传递给该类,然后在第一次单击按钮时处理该Runnable,但显然您可以传入任何自定义行为,这只是如何使用它的一个非常简单的示例
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
public class MyCustomOnClickListener implements View.OnClickListener {
public static final String TAG = MyCustomOnClickListener.class.getSimpleName();
private Runnable doOnClick;
private Context mContext;
private int isClicked = 0;
public MyCustomOnClickListener(Context c, Runnable doOnClick) throws Exception{
if(!(c instanceof Context) || !(doOnClick instanceof Runnable))
throw new Exception("MyCustomOnClickListener needs to be invoked with Context and Runnable params");
this.doOnClick = doOnClick;
this.mContext = c;
}
@Override
public void onClick(View view) {
Log.v(TAG,"onClick() - detected");
if(isClicked++ > 0)return; //this prevents multiple clicks
Log.d(TAG, String.format("onClick() - being processed. View.Tag = \"%s\"", view.getTag().toString()));
new Handler(mContext.getMainLooper()).post(doOnClick);
}
}
然后使用它(假设您正在进行this
上下文的活动)
是否要将任何数据从for循环传递给MyOnClickListener?每次更改for循环并创建新对象,并在cunstructor中传递索引。哦,我明白了。如果我需要在myOCL中完成一个活动,我该如何做?公共类MyOnClickListener扩展活动实现OnClickListener{
`setResult(Activity.RESULT_OK,returnIntent);finish()`@AdrianJandl:将活动上下文也传递给按钮构造函数或无法从上下文调用“Activity”函数?Context.finish();
给我一个编译器错误。请查看我原始帖子中的编辑内容好吗?我在返回索引时仍然有问题。显然,我当前返回NULL。
for (int i =0;i<buttonList.size();i++) {
buttonList.get(i).setText("Remove");
buttonList.get(i).setOnClickListener(new MyOnClickListener(i));
}
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import android.view.View;
public class MyCustomOnClickListener implements View.OnClickListener {
public static final String TAG = MyCustomOnClickListener.class.getSimpleName();
private Runnable doOnClick;
private Context mContext;
private int isClicked = 0;
public MyCustomOnClickListener(Context c, Runnable doOnClick) throws Exception{
if(!(c instanceof Context) || !(doOnClick instanceof Runnable))
throw new Exception("MyCustomOnClickListener needs to be invoked with Context and Runnable params");
this.doOnClick = doOnClick;
this.mContext = c;
}
@Override
public void onClick(View view) {
Log.v(TAG,"onClick() - detected");
if(isClicked++ > 0)return; //this prevents multiple clicks
Log.d(TAG, String.format("onClick() - being processed. View.Tag = \"%s\"", view.getTag().toString()));
new Handler(mContext.getMainLooper()).post(doOnClick);
}
}
try{
Button aButton = findViewById(R.id.someButton);
aButton.setOnClickListener(new MyCustomOnClickListener(/*context*/this, new Runnable(){
public void run(){
//here you would put whatever you would have otherwise put in the onClick handler. If you need the View that's being clicked you can replace the Runnable with a custom Runnable that passes the view along
}
}));
}catch(...){
}