[英]Android ICS widgets (switch) and older versions
Is it possible to have a switch widget for ICS versions, but a checkbox for pre ICS?是否可以为 ICS 版本提供一个开关小部件,但为预 ICS 提供一个复选框? If so, how?如果是这样,如何?
I'm not worried about other components, only switch.我不担心其他组件,只担心开关。
MY SOLUTION我的解决方案
Seeing as switch and checkbox both inherit from CompoundButton, I just did this看到 switch 和 checkbox 都继承自 CompoundButton,我只是这样做了
((CompoundButton)findViewById(R.id.swTax)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
calculateValues();
}
});
Create a separate layout XML file for ICS versions by placing it in a separate layout folder, eg layout-v14
.为 ICS 版本创建一个单独的布局 XML 文件,方法是将其放置在单独的布局文件夹中,例如layout-v14
。 To prevent a lot of duplicate XML, use one main layout file and include the widget.为防止大量重复 XML,请使用一个主布局文件并包含小部件。 The resulting file structure would look something like this:生成的文件结构如下所示:
In mylayout.xml
you would have something like:在mylayout.xml
你会有这样的东西:
<include layout="@layout/widget" />
In the Activity
for this layout you will have to check the version as well before setting up any interaction with either the CheckBox
or Switch
widget:在此布局的Activity
中,您还必须在设置与CheckBox
或Switch
小部件的任何交互之前检查版本:
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
}
I've tried every solution that i found but non of them were fit my needs, so i created my own widget wich is used ObjectAnimator from nineOld compatibility lib and works pretty fine on any android API.我已经尝试了我找到的所有解决方案,但没有一个适合我的需要,所以我创建了我自己的小部件,它使用了 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);
}
} }
In XML 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"
/>
In the Activity/Fragment you only have to findViewById and set clickListener to it, and in onClick callback handle it:在 Activity/Fragment 中,您只需要找到 ViewById 并将其设置为 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);
}
}
I use a iOS like Segmented control (extension of radio button) with on/off instead of a switch then you can use the same code for old and new SDK.我使用 iOS 之类的带有开/关而不是开关的分段控件(单选按钮的扩展),然后您可以对新旧 SDK 使用相同的代码。
There is a nice sample project with all the code here:这里有一个很好的示例项目,其中包含所有代码:
https://github.com/makeramen/android-segmentedradiobutton https://github.com/makeramen/android-segmentedradiobutton
It has both text and graphic samples.它有文本和图形样本。
It's happened!出事了!
http://developer.android.com/tools/support-library/index.html http://developer.android.com/tools/support-library/index.html
Changes for v7 appcompat library: Added SwitchCompat, a backport of the Switch widget that was added in Android 4.0 (API level 14). v7 appcompat 库的更改:添加了 SwitchCompat,它是 Android 4.0(API 级别 14)中添加的 Switch 小部件的向后端口。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.