單選開關(guān)(點擊選擇與否切換)

單選開關(guān)

TextView兩圖片實現(xiàn)點擊切換開關(guān)效果:

    //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"; //取值

    /**
     * 單選開關(guān)
     *
     * @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兩圖片實現(xiàn)點擊切換開關(guān)效果:

    //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);
            }
        });
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

推薦閱讀更多精彩內(nèi)容

  • 內(nèi)容抽屜菜單ListViewWebViewSwitchButton按鈕點贊按鈕進度條TabLayout圖標下拉刷新...
    皇小弟閱讀 46,907評論 22 665
  • 發(fā)現(xiàn) 關(guān)注 消息 iOS 第三方庫、插件、知名博客總結(jié) 作者大灰狼的小綿羊哥哥關(guān)注 2017.06.26 09:4...
    肇東周閱讀 12,259評論 4 61
  • @RequestBody作用:@RequestBody注解用于讀取http請求的內(nèi)容(字符串),通過springm...
    Stringer閱讀 528評論 0 0
  • 作為一名大四黨,談起大學想說的關(guān)鍵詞如下 讀書,旅行,兼職,戀愛。雖早有不聽老人言吃虧在眼前,但我們還是在吃虧的道...
    是芒果味兒的空氣呀閱讀 120評論 0 0
  • 1、感恩當下這個冥想!每次聽冥想也許我沒注意怎么聽,也不知道它什么時候就滲入到我的骨子里,讓我開始不一樣!我成功并...
    明景靈燕閱讀 312評論 0 0