Android 开关问题-只有一个选项可见

Android 开关问题-只有一个选项可见,android,switch-statement,Android,Switch Statement,我想在android应用程序中使用switch。我试过了,但主要问题是 如果我选择了ON,它将不会显示off文本 如果我选择了“关闭”,它将不会显示在文本上 关闭文本不显示,但我们可以通过单击开关的黑色区域来选择关闭 代码 <Switch android:id="@+id/mySwitch" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParen

我想在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"/>

论国家

非状态

如何同时显示两个文本? 我是否可以更改开关的文本以同时显示开/关状态

任何帮助都将不胜感激


提前感谢。

这是我的解决方案,它创建了一个自定义小部件来模拟开关控制。我使用的是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:RelativeLayout{
私人视野;
私人住宅被检查;
private int-imageResourceIdOn=Resource.Drawable.switch\u-on;
private int-imageResourceIdOff=Resource.Drawable.switch\u off;
公共SwitchImageView(上下文):基础(上下文){
Init();
}
public SwitchImageView(上下文,IAttributeSet属性):基本(上下文,属性){
Init();
}
公共SwitchImageView(上下文上下文,IAttributeSet属性,int-defStyle):基本(上下文,属性,defStyle){
Init();
}
私有void Init(){
var layoutInflater=(layoutInflater)ApplicationContext.Activity.GetSystemService(Context.LayoutInflaterService);
layoutInflater.充气(RPR.Mobile.Resource.Layout.SwitchImageView,this,true);
单击+=(对象发送者,事件参数e)=>{
选中=!选中;
};
}
公共int ImageResourceIdOn{
得到{
返回imageResourceIdOn;
}
设置{
imageResourceIdOn=值;
如果(已检查){
此.SetBackgroundResource(值);
}
}
}
公共int ImageResourceIdOff{
得到{
返回imageResourceIdOff;
}
设置{
imageResourceIdOff=值;
如果(!已检查){
此.SetBackgroundResource(值);
}
}
}
公共字符串TextOn{
得到{
返回this.findviewbyd(Resource.Id.switch\u on\u text).text;
}
设置{
this.findviewbyd(Resource.Id.switch\u on\u text).text=值;
}
}
公共字符串TextOff{
得到{
返回this.findviewbyd(Resource.Id.switch\u off\u text).text;
}
设置{
this.findviewbyd(Resource.Id.switch\u off\u text).text=值;
}
}
公共布尔检查{
得到{
返回检查;
}
设置{
isChecked=值;
this.SetBackgroundResource(值?ImageResourceIdOn:ImageResourceIdOff);
this.findviewbyd(Resource.Id.switch_on_text).SetTextColor(value?Color.White:Color.Black);
this.findviewbyd(Resource.Id.switch\u off\u text).SetTextColor(value?Color.Black:Color.White);
if(CheckedChange!=null){
CheckedChange(此,值);
}
}
}
公共事件处理程序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>

有一个由两个标准按钮和一个线性布局组成。有很多xml文件需要导入,但它在所有版本上都可以完美地工作,并且非常易于使用。检查下面的Github页面

使用
  • 将res/drawable下的XML文件复制到项目的res/drawable文件夹中
  • 将LinearLayout从layout.xml复制到布局文件
  • 将值从values/colors.xml和values/dimens复制到您自己的文件中
  • 使用以下代码初始化开关
  • 
    SekizbitSwitch mySwitch=新的SekizbitSwitch(findViewById(R.id.sekizbit_开关));
    mySwitch.setOnChangeListener(新的SekizbitSwitch.OnSelectedChangeListener()){
    @凌驾
    选定更改时的公共无效(SekizbitSwitch发送方){
    if(sender.getCheckedIndex()==0)
    {
    System.out.println(“选择左键”);
    }
    else if(sender.getCheckedIndex()==1)
    {
    System.out.println(“选择右键”);
    }
    }
    });
    

    请澄清您的问题,如果开关的状态是关闭的,那么post ongoogle、facebook等旁边的文本是否。。。说“关”吗?你能用版式设计代码更新问题吗?你可以用一个带有“开-关”文本的自定义绘图,并将其设置为切换按钮的背景。@ilovepjs:不,这是完全不同的控件。@Jayabal:更新的问题。