单选开关(点击选择与否切换)

单选开关

TextView两图片实现点击切换开关效果:

    //xml
    <TextView
                android:id="@+id/iv_reason"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="@drawable/corner_border_feedback_n"
                android:gravity="center"
                android:text="推送時間造成困擾" />

    initSwitcher(iv_reason, "on"); //初始
    String value = (iv_reason.getTag() != null) && ((String) iv_reason.getTag()).equals("off") ? "2" : "1"; //取值

    /**
     * 單選開關
     *
     * @param tv
     * @param tag
     */
    private void initSwitcher(final TextView tv, String tag) {

        //config
        final String[] tags = {"on", "off"};
        final int[] colors = {0xfff77518, 0xff999999};
        final int[] resIds = {R.drawable.corner_border_feedback_p, R.drawable.corner_border_feedback_n};

        tag = tag.equals(tags[0]) ? tags[0] : tags[1]; // check data
        int resId = tag.equals(tags[0]) ? resIds[0] : resIds[1];
        int color = tag.equals(tags[0]) ? colors[0] : colors[1];

        tv.setBackgroundResource(resId);
        tv.setTextColor(color);
        tv.setTag(tag);
        tv.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String tagto = (view.getTag() != null) && ((String) view.getTag()).equals(tags[1]) ? tags[0] : tags[1];
                if (tagto.equals(tags[0])) {
                    tv.setBackgroundResource(resIds[0]);
                    tv.setTextColor(colors[0]);
                    //TODO:互斥
                } else {
                    tv.setBackgroundResource(resIds[1]);
                    tv.setTextColor(colors[1]);
                }
                tv.setTag(tagto);
            }
        });
    }

ImageView、Button两图片实现点击切换开关效果:

    //xml
    <ImageView
                android:id="@+id/iv_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:contentDescription="@null"
                android:scaleType="fitXY"
                android:src="@drawable/ic_feedback_submit_n"/>

    <Button
                android:id="@+id/btn_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_horizontal"
                android:background="@drawable/ic_feedback_submit_n"
                android:gravity="center"/>

        // ImageView
        final String[] tags = {"on", "off"};
        final ImageView iv_send = (ImageView) v.findViewById(R.id.iv_send);
        iv_send.setTag(tags[0]); //init
        iv_send.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String tagto = (view.getTag() != null) && ((String) view.getTag()).equals(tags[1]) ? tags[0] : tags[1];
                iv_send.setImageResource(tagto.equals(tags[0]) ? R.drawable.ic_feedback_submit_n: R.drawable.ic_feedback_submit_p);
                iv_send.setTag(tagto);
            }
        });

        // Button
        final Button btn_send = (Button) v.findViewById(R.id.btn_send);
        btn_send.setTag(tags[0]); //init
        btn_send.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                String tagto = (view.getTag() != null) && ((String) view.getTag()).equals(tags[1]) ? tags[0] : tags[1];
                btn_send.setBackgroundResource(tagto.equals(tags[0]) ? R.drawable.ic_feedback_submit_n: R.drawable.ic_feedback_submit_p);
                btn_send.setTag(tagto);
            }
        });
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 内容抽屉菜单ListViewWebViewSwitchButton按钮点赞按钮进度条TabLayout图标下拉刷新...
    皇小弟阅读 46,884评论 22 665
  • 发现 关注 消息 iOS 第三方库、插件、知名博客总结 作者大灰狼的小绵羊哥哥关注 2017.06.26 09:4...
    肇东周阅读 12,229评论 4 61
  • @RequestBody作用:@RequestBody注解用于读取http请求的内容(字符串),通过springm...
    Stringer阅读 528评论 0 0
  • 作为一名大四党,谈起大学想说的关键词如下 读书,旅行,兼职,恋爱。虽早有不听老人言吃亏在眼前,但我们还是在吃亏的道...
    是芒果味儿的空气呀阅读 119评论 0 0
  • 1、感恩当下这个冥想!每次听冥想也许我没注意怎么听,也不知道它什么时候就渗入到我的骨子里,让我开始不一样!我成功并...
    明景灵燕阅读 306评论 0 0