2017-11-22 12 views
-1

私はアンドロイドプロジェクトで作業しています。私は、ボタンの他​​の部分でスライドモードになっているときに別の一時停止ボタンが表示されるように、スイッチボタンを使用することが可能かどうか尋ねましたか?ボタンの切り替え方法

答えて

1

あなたはこのようなものを探していることを望みます。これを達成するための

enter image description here

enter image description here

enter image description here

は、カスタム描画可能を作成する必要があります。

switch_button_custom.xmlは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<selector xmlns:android="http://schemas.android.com/apk/res/android"> 
    <item android:drawable="@drawable/pause_active" android:state_enabled="false" /> 
    <item android:drawable="@drawable/play_active" android:state_checked="true" /> 
</selector> 

あなたplay_active.png

enter image description here

あなたpause_active.png

enter image description here

あなたの活動のXMLは次のようになります。

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="center" 
    android:orientation="vertical"> 

     <Switch 
     android:id="@+id/switchCustom" 
     android:layout_width="96dp" 
     android:layout_height="48dp" 
     android:layout_gravity="center" 
     android:background="@drawable/switch_button_custom" 
     android:thumb="@android:color/transparent" /> 

</LinearLayout> 

最後に、あなたのアクティビティは次のようになります。

@Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_home); 

     switchCustom = findViewById(R.id.switchCustom); 

     switchCustom.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
      @Override 
      public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) { 
       if (isChecked) { 
        Toast.makeText(HomeActivity.this, 
          "Play", Toast.LENGTH_SHORT).show(); 
       } else { 
        Toast.makeText(HomeActivity.this, 
          "Pause", Toast.LENGTH_SHORT).show(); 
       } 
      } 
     }); 
    } 

私はこれが達成したいと思っています。

関連する問題