Android ICS小部件(切换)和旧版本

时间:2012-04-04 12:46:49

标签: android android-widget

是否可以为ICS版本设置一个开关小部件,但是有一个ICS前的复选框?如果是这样,怎么样?

我并不担心其他组件,只有切换。

我的解决方案

看到切换和复选框都从CompoundButton继承,我只是做了这个

((CompoundButton)findViewById(R.id.swTax)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            calculateValues();
        }
    });

4 个答案:

答案 0 :(得分:6)

为ICS版本创建一个单独的布局XML文件,方法是将其放在一个单独的布局文件夹中,例如: layout-v14。要防止大量重复的XML,请使用一个主布局文件并包含小部件。生成的文件结构如下所示:

  • 布局
    • mylayout.xml
    • widget.xml
  • 布局-V14
    • widget.xml

mylayout.xml中你会有类似的东西:

<include layout="@layout/widget" />

在此版面的Activity中,您必须先检查版本,然后才能与CheckBoxSwitch小部件进行任何互动:

int version = android.os.Build.VERSION.SDK_INT;
if (version >= 14) {
    // get your Switch view and set up listeners etc
}
else {
    // get your CheckBox view and set up listeners etc
}

答案 1 :(得分:1)

我已经尝试了我找到的所有解决方案,但是没有它们符合我的需求,所以我创建了自己的小部件,它使用了来自nineOld兼容性库的ObjectAnimator,并且可以在任何Android API上运行得很好。

import android.widget.RelativeLayout;
import com.myapp.utilities.AppUtils;
import com.nineoldandroids.animation.Animator;
import com.nineoldandroids.animation.AnimatorListenerAdapter;
import com.nineoldandroids.animation.ObjectAnimator;

public class SwitchButton extends RelativeLayout {

public static final int TEXT_SIZE = 11;

public float HANDLE_SHIFT = -40f;
public float TEXT_RIGHT_SHIFT = 40f;
public static int BUTTON_ID = 0x00009999;
public static int TEXT_ID = 0x00008888;


private Button handleButton;
private RoboTextView textView;
private boolean switchEnabled;
private String yesStr;
private String noStr;
private int TEXT_LEFT_PADDING = 13;

private ObjectAnimator animateHandleLeftShift;
private ObjectAnimator animateHandleRightShift;
private int HANDLE_BUTTON_HEIGHT = 22;
private int HANDLE_BUTTON_WIDTH = 42;
private ObjectAnimator animateTextLeftShift;
private ObjectAnimator animateTextRightShift;


public SwitchButton(Context context) {
    super(context);
    onCreate(context);
}


private void onCreate(Context context) {

    float density = context.getResources().getDisplayMetrics().density;

    TEXT_LEFT_PADDING *= density;

    HANDLE_BUTTON_HEIGHT *= density;
    HANDLE_BUTTON_WIDTH *= density;

    HANDLE_SHIFT *= density;
    TEXT_RIGHT_SHIFT *= density;

    yesStr = getContext().getString(R.string.yes).toUpperCase();
    noStr = getContext().getString(R.string.no).toUpperCase();

    {// Button
        handleButton = new Button(getContext());
        RelativeLayout.LayoutParams buttonParams = new LayoutParams(HANDLE_BUTTON_WIDTH, HANDLE_BUTTON_HEIGHT);
        buttonParams.addRule(RelativeLayout.CENTER_VERTICAL);
        buttonParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

        handleButton.setBackgroundResource(R.drawable.button_switch_handle_selector);
        handleButton.setId(BUTTON_ID);

        addView(handleButton, buttonParams);
    }


    {// Text
        textView = new RoboTextView(getContext());
        LayoutParams textParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        textParams.addRule(RelativeLayout.CENTER_VERTICAL);

        textView.setText(yesStr);
        textView.setTextColor(getContext().getResources().getColor(R.color.new_normal_gray));
        textView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, TEXT_SIZE);
        textView.setPadding(TEXT_LEFT_PADDING, 0, 0, 0);
        textView.setFont(RoboTextView.ROBOTO_BOLD_FONT);
        textView.setId(TEXT_ID);
        float shadowRadius = 0.5f ;
        float shadowDx = 0;
        float shadowDy = 1;
        textView.setShadowLayer(shadowRadius, shadowDx, shadowDy, Color.BLACK);

        addView(textView, textParams);
    }
    initFlipAnimation();

}

@Override
public void setOnClickListener(OnClickListener l) {
    handleButton.setOnClickListener(l);
    textView.setOnClickListener(l);
}

public void toggle(View view){
    if (AppUtils.HONEYCOMB_PLUS_API && view.getId() == TEXT_ID) { // ignore text clicks
        return;
    }

    switchEnabled = !switchEnabled;

    if (switchEnabled) {
        // animate handle to the left
        animateHandleLeftShift.start();
        animateTextLeftShift.start();

        textView.setText(noStr);
    } else {
        animateHandleRightShift.start();
        animateTextRightShift.start();

        textView.setText(yesStr);
    }
}

private android.view.animation.Interpolator accelerator = new LinearInterpolator();
private static final int DURATION = 70;

private void initFlipAnimation() {


    animateHandleLeftShift = ObjectAnimator.ofFloat(handleButton, "translationX", 0f, HANDLE_SHIFT);
    animateHandleLeftShift.setDuration(DURATION);
    animateHandleLeftShift.setInterpolator(accelerator);

    animateHandleRightShift = ObjectAnimator.ofFloat(handleButton, "translationX", HANDLE_SHIFT, 0f);
    animateHandleRightShift.setDuration(DURATION);
    animateHandleRightShift.setInterpolator(accelerator);

    animateHandleLeftShift.addListener(new AnimatorListenerAdapter() {
        @Override
        public void onAnimationEnd(Animator anim) {
            // TODO
        }
    });


    animateTextLeftShift = ObjectAnimator.ofFloat(textView, "translationX", 0f, TEXT_RIGHT_SHIFT);
    animateTextLeftShift.setDuration(DURATION);
    animateTextLeftShift.setInterpolator(accelerator);

    animateTextRightShift = ObjectAnimator.ofFloat(textView, "translationX", TEXT_RIGHT_SHIFT, 0f);
    animateTextRightShift.setDuration(DURATION);
    animateTextRightShift.setInterpolator(accelerator);
}

}

在XML

<com.chess.SwitchButton
    android:id="@+id/ratedGameSwitch"
    android:layout_width="@dimen/button_switch_width"
    android:layout_height="@dimen/button_switch_height"
    android:background="@drawable/button_switch_back"
    />

在Activity / Fragment中你只需要findViewById并将clickListener设置为它,并在onClick回调中处理它:

switchButton = (SwitchButton) optionsView.findViewById(R.id.ratedGameSwitch);
switchButton.setOnClickListener(this);


@Override
public void onClick(View view) {
    if (view.getId() == SwitchButton.BUTTON_ID  || view.getId() == SwitchButton.TEXT_ID){
        switchButton.toggle(view);
    }
}

答案 2 :(得分:0)

我使用像分段控件(单选按钮的扩展名)这样的iOS来打开/关闭而不是开关,然后你就可以对新旧SDK使用相同的代码了。

这里有一个很好的示例项目包含所有代码:

https://github.com/makeramen/android-segmentedradiobutton

它有文字和图形样本。

答案 3 :(得分:0)

它发生了!

http://developer.android.com/tools/support-library/index.html

v7 appcompat库的更改:         添加了SwitchCompat,这是在Android 4.0(API级别14)中添加的Switch小部件的后端。