首页 \ 问答 \ 是否可以创建一个将同时处理ToggleButton和Switch的OnClickListener(Is it possible to create an OnClickListener that will handle both ToggleButton and Switch)

是否可以创建一个将同时处理ToggleButton和Switch的OnClickListener(Is it possible to create an OnClickListener that will handle both ToggleButton and Switch)

我正在开发一款需要与Android 4.0之前的设备兼容的应用。

在4.0之前的布局中,我必须使用ToggleButton并在使用Switch 4.0 am之后。 就用户而言,两者的功能相同。 单击它们时,它们应具有相同的行为。

我正在尝试编写一个适用于它们的OnClickListener ,这样我就不会复制很多代码了。 Switch / ToggleButtonsFragment中使用,因此只需将布局中的onClickListener调用放置即可。

为了尝试它,对于SwitchToggleButton ,我将onClickListener设置为pushSelector,它是:

private View.OnClickListener pushSelector = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((ToggleButton) v).isChecked() || ((Switch) v).isChecked()) {
                // code
            } else {
                // code
            }
        }
    };

不出所料,这不起作用,但我想我会试一试。

是否可以有一个可以同时处理ToggleButtonSwitchToggleButton ,还是需要为每个onClickListener设置一个onClickListener


I am working on an app that needs to be compatible with pre Android 4.0 devices.

In the pre 4.0 layout I have to use a ToggleButton and in the post 4.0 am using a Switch. Both function the same as far as the user is concerned. When they are clicked, they should have the same behavior.

I'm trying to write an OnClickListener that will work for both of them so that I'm not duplicating a lot of code. The Switch/ToggleButtons are used inside a Fragment, so just placing the call to the onClickListener in the layout won't work.

In an effort to try it, for both the Switch and ToggleButton, I set the onClickListener to pushSelector, which is:

private View.OnClickListener pushSelector = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (((ToggleButton) v).isChecked() || ((Switch) v).isChecked()) {
                // code
            } else {
                // code
            }
        }
    };

Unsurprisingly, this didn't work, but I thought I'd give it a go.

Is it possible to have a single onClickListener that can handle both a ToggleButton and Switch, or will I need to set up an onClickListener for each one?


原文:https://stackoverflow.com/questions/32247337
更新时间:2023-02-21 22:02

最满意答案

您应该将SignalA api用于客户端应用程序,对于服务器,您可以使用WebSocket,如果您在服务器中使用Microsoft .Net平台,那么您可以使用ASP.NET SignalR

https://github.com/erizet/SignalA

http://signalr.net/

https://github.com/SignalR/java-samples

用于支持SignalR的Java服务器api

https://github.com/Atmosphere/atmosphere


You should use SignalA api for client application and for server you can use WebSocket , if you are using Microsoft .Net platform in server, then you can use ASP.NET SignalR

https://github.com/erizet/SignalA

http://signalr.net/

https://github.com/SignalR/java-samples

Java server api for supporting SignalR

https://github.com/Atmosphere/atmosphere

相关问答

更多
  • 您可以使用ScringoCommentButton( http://www.scringo.com/docs/android-guides/popular/setup-chat-rooms-and-forums/#Adhoc ),只允许您想要的用户进入该房间。 您只能向允许的用户显示此按钮。 顺便说一句,您甚至可以使用Scringo安卓库项目中的XML:scringo_comment_button.xml自定义按钮的UI You can use the ScringoCommentButton (http: ...
  • 未注册的Smack数据包提供商。 您得到一个ClassCastException因为Smack无法为MUC创建正确的Packet类实例。 我建议您使用较新版本的aSmack并按照README中的说明进行操作,该说明应该为您初始化并注册Providers。 The Packet Providers of Smack where not registered. You get a ClassCastException because Smack is unable to create the correct P ...
  • 通过使用bot API可以实现。 默认情况下,机器人无法接收所有消息。 但您可以更改此设置。 这是关于此的文档: https : //core.telegram.org/bots#privacy-mode 创建机器人。 改变Bot的隐私模式。 将bot添加到组中 设置webhook以重新发送消息或使用getUpdates方法。 将消息存储到数据库中。 It's possible by using bot API. By default bots can't recive all messages. but ...
  • 我已经使用PubNub一段时间了,它真棒。 您可以参考Android和iOS中的示例。 它具有完整的PCL支持,因此您无需执行特定的Android / iOS代码来交换消息。 我还阅读了他们在PCL中进行的大量测试 ,并从中获得了很好的信息。 您有实时框架 ,以交换实时消息。 他们也有很好的支持 。 对于SQL,您有SQLite-net 。 I've been using PubNub for a while, and its awesome. You can consult examples in And ...
  • 我很惊讶,线路chat.SendMessage("/add " & "contact-to-add")首先起作用,因为我在使用它时会出错。 因此,我使用子程序通过UserCollection对象添加新成员。 如果要添加新成员,请添加到UserCollection,然后将其传递给AddMembers()子例程。 至于聊天名称本身,你不需要它。 如果您在现场创建聊天,则将其分配给聊天变量。 Imports SKYPE4COMLib Public Class Form1 Dim skype As Skype ...
  • 尝试这个: Messages groupchat1 userid message:hello time: (timeinMills) type: text userid1 message: hello again! time (timeinMills) type: text groupchat1将作为聊天组的名称,它将包含聊天内部的用户并相互发送消息。 然后,您可以使用FirebaseRecycl ...
  • 你需要在房间里指定你想要的昵称。 这是您发送给JID的资源。 例如,要将房间作为“Joe”加入,请将您的存在节发送到testroom@conference.localhost/Joe而不是testroom@conference.localhost 。 您的连接节也不应包含'from'属性,这是由服务器为您添加的 。 包括一个不会破坏任何东西,但它是毫无意义的(无论如何你的错误是错误的,它必须是一个完整的JID)。 因此,加入房间为'乔'的正确节: javax.imageio.IIOException: Can't create output stream!的解决方案

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)