简体   繁体   English

Android有多个听众吗?

[英]Can Android have multiple listeners?

Say I have 5 buttons. 说我有5个按钮。 For each button I want to be able to fire a listener. 对于每个按钮,我希望能够触发一个监听器。 Once the listener is fired I want fire an async task in my sdk, and then have the sdk return the status of the async task. 一旦侦听器被触发,我想在我的sdk中触发异步任务,然后让sdk返回异步任务的状态。

1) Do I write a separate listener/ button? 1)我是否写了一个单独的监听器/按钮? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners? 我读到某处我在android中只能有一个注册的监听器,如果这是真的我如何处理许多监听器?

2) How do I return postExecute call result from SDK to the api level? 2)如何将SDK的postExecute调用结果返回到api级别?

1) Do I write a separate listener/ button? 1)我是否写了一个单独的监听器/按钮? I read somewhere I can have only one registered listener in android, if thats true how do I handle many listeners? 我读到某处我在android中只能有一个注册的监听器,如果这是真的我如何处理许多监听器?

A View can only have one listener of each type, ie a Button cannot have two OnClickListeners . 一个视图只能有一个每种类型的侦听器,即一个Button不能有两个OnClickListeners Don't confuse this with the fact that one listener can be attached to multiple Views, ie ButtonA and ButtonB can have the same OnClickListener 不要将此与一个侦听器可以附加到多个视图的事实混淆,即ButtonA和ButtonB可以具有相同的OnClickListener

2) How do I return postExecute call result from SDK to the api level? 2)如何将SDK的postExecute调用结果返回到api级别?

Your terminology isn't right, but you'll figure it out as you go. 你的术语不对,但你去的时候就会搞清楚。 Typically onPostExecute() will call another method or work directly with a View: 通常onPostExecute()将调用另一个方法或直接使用View:

@Override
protected void onPostExecute(String result) {
    doSomething(result);
    textView.setText(result);
}

Easiest is if you setup one listener in your activity, and then handle multiple buttons. 最简单的方法是在活动中设置一个侦听器,然后处理多个按钮。 You can do this with the OnKeyListener class. 您可以使用OnKeyListener类执行此操作 You then do a switch on which key was hit, setup cases for the buttons you wish to act on, and start your AsyncTask . 然后,您可以切换哪个键被击中,为您希望操作的按钮设置案例,并启动AsyncTask

I'm not quite sure what you mean by "return postExecute". 我不太确定你的意思是“return postExecute”。 But if you look at AsyncTask you can see how to use the proper parameters to return a result into onPostExecute. 但是,如果你看一下AsyncTask,你可以看到如何使用正确的参数将结果返回到onPostExecute。 When you instantiate your subclass of AsyncTask you can easily pass in the activity or context you wish to perform a call back on. 当您实例化AsyncTask的子类时,您可以轻松地传入您希望执行回调的活动或上下文。

You can have multiple listeners and depending on what you want to do with them, you can have one for each button. 您可以拥有多个侦听器,并根据您要对其执行的操作,每个按钮可以有一个。 As far as the postExecute() , it runs on the UI thread so you can show your result from there or do whatever you want with it. postExecute() ,它在UI线程上运行,因此您可以从那里显示结果或用它做任何你想做的事情。 For more details, you will need to provide some of the code you have tried and explain exactly where you are having trouble. 有关详细信息,您需要提供一些您尝试过的代码,并准确解释您遇到问题的位置。 If you haven't already, go through the 如果你还没有,请通过

Android Docs about getting started 有关入门的Android文档

This link describes AsynTask 此链接描述了AsynTask

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM