【问题标题】:setResult not working in a java class implementing onClickListenersetResult 在实现 onClickListener 的 java 类中不起作用
【发布时间】:2016-06-15 06:06:58
【问题描述】:

在我的项目中,我有一些按钮,它们有自己的 View.OnClickListener。 我想要的是将所有这些 onClickListener “外部化”为实现接口的 java calss,并在此处处理所有单击事件。

但我发现了一个问题,我不知道如何解决它。

其中一个按钮在类中启动setResult(RESULT_OK, startChatIntent)finish(), but this方法,标记为:Cannot be resolved as method.

这是类:(我去掉了其他按钮功能,可以正常工作,使其更清晰)

public class startCircuitListener implements View.OnClickListener {

    private int mChatType;
    private String mGroupName;
    private String mNickName;
    private String mMessage;
    private ArrayList<String> mGroupEmails;
    private Context context;

    public startCircuitListener(int mChatType, String mGroupName, String mNickName, String mMessage, ArrayList<String> mGroupEmails, Context context) {
        this.mChatType = mChatType;
        this.mGroupName = mGroupName;
        this.mNickName = mNickName;
        this.mMessage = mMessage;
        this.mGroupEmails = mGroupEmails;
        this.context = context;
    }

    @Override
    public void onClick(View v) {


        Intent startChatIntent = new Intent();
        startChatIntent.putExtra("chatRoom", mGroupName);
        startChatIntent.putExtra("chatServer", HelperVariables.CONFERENCE_SERVER_NAME);
        startChatIntent.putExtra("nickname", mNickName);
        startChatIntent.putExtra("Individual_Group", 0);
        startChatIntent.putExtra("MessageToSend", mMessage);
        startChatIntent.putExtra("Invitations", mGroupEmails);
        setResult(RESULT_OK, startChatIntent);
        finish();

    }

}

setResultfinish() 怎样才能工作?

【问题讨论】:

  • setResult()finish()Activity 中声明。所以你需要在Activity 的实例上调用这些方法。如果您想展示更多代码,我们可以提供更准确的建议。 - 你的听众是普通的“外部”类,还是另一个类的非静态内部类,甚至可能是Activity
  • 是的,这个监听器是一个外部java类(基本上是一个新文件),我想在按钮中调用它:`bt_startCircuit.setOnClickListener(new startCircuitListener(param1,param2, ..参数N));

标签: java android onclicklistener implements


【解决方案1】:

您必须使用当前的 Activity 实例创建您的侦听器实例,并将其存储为 startCircuitListener 的成员。

然后拨打this.myActivity.setResult(RESULT_OK, startChatIntent);。 finish() 也是一样的;

【讨论】:

    猜你喜欢
    • 2021-04-21
    • 2021-08-03
    • 2023-03-30
    • 2016-08-03
    • 2021-04-07
    • 1970-01-01
    • 2011-11-04
    • 1970-01-01
    • 2017-01-04
    相关资源
    最近更新 更多