7

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"/>

オン状態

ここに画像の説明を入力

オフ状態

ここに画像の説明を入力

両方のテキストを同時に表示するにはどうすればよいですか? オン/オフの両方の状態のスイッチのテキストを変更できますか?

任意のヘルプをいただければ幸いです。

前もって感謝します。

4

2 に答える 2

0

2 つの標準ボタンと LinearLayout で作成されたものがあります。インポートする xml ファイルがたくさんありますが、すべてのバージョンで完璧に機能し、非常に使いやすいです。次のGithubページを確認してください

プレビュー

2つのボタンを備えたカスタムスイッチ

利用方法

  1. res/drawable の下にある XML ファイルをプロジェクトの res/drawable フォルダーにコピーします。
  2. LinearLayout を layout.xml からレイアウト ファイルにコピーします。
  3. values/colors.xml および values/dimens から独自のファイルに値をコピーします。
  4. 次のコードでスイッチを初期化します

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"); } } });

于 2016-04-07T19:57:21.993 に答える
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>
于 2014-07-01T20:37:27.810 に答える