2017-01-07 2 views
3

のJavaAndroid 6.0がチェックされていませんレイアウトリソースからレイアウトリソースを切り替えます。アンドロイド7.0作業罰金

DeviceSwitch.setLayoutResource(R.layout.settings); 
DeviceSwitch.setKey(CategoryKey); 
DeviceSwitch.setDefaultValue(true); 
DeviceSwitch.setEnabled(true); 
DeviceSwitch.setSelectable(true); 
DevicesShowScreen.addPreference(DeviceSwitch); 

このコードを実行した場合 - > DeviceSwitch.setLayoutResource(R.layout.settings)。 Android 6.0でスイッチがチェックされていません

この文字列 - > DeviceSwitch.setCheked(true);レイアウトから7.0スイッチの嗜好をチェックアンドロイドにアンドロイド6.0に

動作しませ 通常

レイアウト

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:minHeight="?attr/listPreferredItemHeightSmall" 
    android:gravity="center_vertical" 
    android:clipToPadding="false"> 

    <RelativeLayout 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_weight="1" 
     android:paddingTop="16dp" 
     android:paddingBottom="16dp" 
     android:layout_marginStart="15dp"> 

     <TextView android:id="@android:id/title" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:singleLine="true" 
      android:textAppearance="?attr/textAppearanceListItem" 
      android:ellipsize="marquee" /> 

     <TextView android:id="@android:id/summary" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_below="@android:id/title" 
      android:layout_alignStart="@android:id/title" 
      android:maxLines="10" 
      android:ellipsize="end" /> 

    </RelativeLayout> 

    <Switch 
     android:layout_width="wrap_content" 
     android:layout_height="match_parent" 
     android:focusable="false" 
     android:clickable="false" 
     android:gravity="center" 
     android:layout_marginEnd="15dp" 
     android:id="@android:id/switch_widget" 
     android:checked="false" /> 

</LinearLayout> 

答えて

0

SDK 23の代わりにcom.android.internal.R.id.switchWidget識別子を使用していますので、それはですcom.android.internal.R.id.switch_widget

回避方法は非常に簡単です。ただ、SwitchPreferenceクラスを継承し、いくつかのコードを複製:

public class MySwitchPreference extends SwitchPreference { 
    private final Listener mListener = new Listener(); 

    private class Listener implements CompoundButton.OnCheckedChangeListener { 
     @Override 
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { 
      if (!callChangeListener(isChecked)) { 
       // Listener didn't like it, change it back. 
       // CompoundButton will make sure we don't recurse. 
       buttonView.setChecked(!isChecked); 
       return; 
      } 

      MySwitchPreference.this.setChecked(isChecked); 
     } 
    } 

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
     super(context, attrs, defStyleAttr, defStyleRes); 
    } 

    public MySwitchPreference(Context context, AttributeSet attrs, int defStyleAttr) { 
     super(context, attrs, defStyleAttr); 
    } 

    public MySwitchPreference(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    public MySwitchPreference(Context context) { 
     super(context); 
    } 

    @Override 
    protected void onBindView(View view) { 
     super.onBindView(view); 

     View checkableView = view.findViewById(R.id.switch_widget); 
     if (checkableView != null && checkableView instanceof Checkable) { 
      if (checkableView instanceof Switch) { 
       final Switch switchView = (Switch) checkableView; 
       switchView.setOnCheckedChangeListener(null); 
      } 

      ((Checkable) checkableView).setChecked(isChecked()); 

      if (checkableView instanceof Switch) { 
       final Switch switchView = (Switch) checkableView; 
       switchView.setTextOn(getSwitchTextOn()); 
       switchView.setTextOff(getSwitchTextOff()); 
       switchView.setOnCheckedChangeListener(mListener); 
      } 
     } 
    } 
} 

そしてもちろんの

があなたのウィジェットスイッチャーレイアウトで android:id="@+id/switch_widget"android:id="@android:id/switch_widget"を交換することを忘れないでください。

関連する問題