切换问题 - 只有一个选项可见

时间:2013-10-14 08:38:06

标签: android switch-statement

我想在Android应用程序中使用switch。我尝试过,但主要问题是

  • 如果我选择了ON,则不会显示文字。
  • 如果我选择了OFF,它将不会显示在文本上。

不显示关闭文本,但我们可以通过单击开关的黑色区域来选择关闭。

代码

<Switch android:id="@+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="48dp"
android:layout_marginTop="26dp"
android:height="50dp"
android:text="ON OFF"
android:textSize="20sp"
android:switchMinWidth="50sp"
android:switchPadding="50sp"/>

On State

enter image description here

关闭状态

enter image description here

我如何同时显示两个文字? 我可以更改状态ON / OFF的开关文本吗?

任何帮助将不胜感激。

提前致谢。

3 个答案:

答案 0 :(得分:4)

完成此link。这是一个很好的图书馆,您可以通过代码了解如何自己或使用图书馆。

答案 1 :(得分:0)

这是我的解决方案,它创建了一个自定义小部件来模仿Switch控件。我使用的是Xamarin,但这段代码很容易翻译成Java。

SwitchImageView.cs:

public class SwitchImageView : RelativeLayout {
        private View view;

        private bool isChecked;
        private int imageResourceIdOn = Resource.Drawable.switch_on;
        private int imageResourceIdOff = Resource.Drawable.switch_off;

        public SwitchImageView(Context context): base(context) {
            Init ();
        }

        public SwitchImageView(Context context, IAttributeSet attrs) : base(context, attrs) {
            Init ();
        }

        public SwitchImageView(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle) {
            Init ();
        }

        private void Init () {
            var layoutInflater = (LayoutInflater)ApplicationContext.Activity.GetSystemService (Context.LayoutInflaterService);
            layoutInflater.Inflate (RPR.Mobile.Resource.Layout.SwitchImageView, this, true);

            this.Click += (object sender, EventArgs e) => {
                Checked = !Checked;
            };
        }

        public int ImageResourceIdOn { 
            get {
                return imageResourceIdOn;
            }
            set {
                imageResourceIdOn = value;
                if (isChecked) {
                    this.SetBackgroundResource (value);
                }
            }
        }

        public int ImageResourceIdOff { 
            get {
                return imageResourceIdOff;
            }
            set {
                imageResourceIdOff = value;
                if (!isChecked) {
                    this.SetBackgroundResource (value);
                }
            }
        }

        public string TextOn { 
            get {
                return this.FindViewById <TextView> (Resource.Id.switch_on_text).Text;
            }
            set {
                this.FindViewById <TextView> (Resource.Id.switch_on_text).Text = value;
            }
        }

        public string TextOff { 
            get {
                return this.FindViewById <TextView> (Resource.Id.switch_off_text).Text;
            }
            set {
                this.FindViewById <TextView> (Resource.Id.switch_off_text).Text = value;
            }
        }

        public bool Checked { 
            get {
                return isChecked;
            }
            set {
                isChecked = value;
                this.SetBackgroundResource (value ? ImageResourceIdOn : ImageResourceIdOff);
                this.FindViewById <TextView> (Resource.Id.switch_on_text).SetTextColor (value ? Color.White : Color.Black);
                this.FindViewById <TextView> (Resource.Id.switch_off_text).SetTextColor (value ? Color.Black : Color.White);
                if (CheckedChange != null) {
                    CheckedChange (this, value);
                }
            }
        }

        public event EventHandler<bool> CheckedChange;
    }

SwitchImageView.axml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/switch_container"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <View
        android:id="@+id/strut"
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:visibility="invisible"
        android:layout_centerInParent="true" />
    <TextView
        android:id="@+id/switch_off_text"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/black"
        android:text="On"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:paddingLeft="10dp"
        android:paddingTop="3dp" />
    <TextView
        android:id="@+id/switch_on_text"
        android:layout_toRightOf="@+id/strut"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:textColor="@color/white"
        android:text="Off"
        android:textAppearance="?android:attr/textAppearanceSmall"
        android:paddingRight="5dp"
        android:paddingLeft="10dp"
        android:paddingTop="3dp" />
</RelativeLayout>

答案 2 :(得分:0)

有一个由2个标准按钮和一个LinearLayout制作。有大量的xml文件要导入,但它在所有版本上都很完美,而且非常易于使用。检查以下Github页面

Preview

Custom Switch With 2 Buttons

使用

  1. 将res / drawable下的XML文件复制到项目的res / drawable文件夹中。
  2. 将LinearLayout从layout.xml复制到您的布局文件。
  3. 将值/ colors.xml和values / dimens中的值复制到您自己的文件中。
  4. 使用以下代码
  5. 初始化交换机

    SekizbitSwitch mySwitch = new SekizbitSwitch(findViewById(R.id.sekizbit_switch)); mySwitch.setOnChangeListener(new SekizbitSwitch.OnSelectedChangeListener() { @Override public void OnSelectedChange(SekizbitSwitch sender) { if(sender.getCheckedIndex() ==0 ) { System.out.println("Left Button Selected"); } else if(sender.getCheckedIndex() ==1 ) { System.out.println("Right Button Selected"); } } });

相关问题