android开关按钮怎么实现
6个回答
展开全部
在Android中,不管什么样子的视图都是继承自View类,因此我们必须要自定义一个View类,下面看一下代码实现:
com\mandr\demo\ TestSlipButtonActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestSlipButtonActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton mSlipButton=(SlipButton)findViewById(R.id.slipButton);
mSlipButton.setOnChangedListener(new OnChangedListener() {
@Override
public void OnChanged(boolean checkState) {
if(checkState){
Log.d("TestSlipButtonActivity", "checkState = "+checkState);
}else{
Log.d("TestSlipButtonActivity", "checkState = "+checkState);
}
}
});
}
}
//View的核心代码
public class ToogleButton extends View {
private static final String Tag="ToogleButton";
private int width;
private int height;
private Paint mPaint;
public ToogleButton(Context context) {
super(context);
init();
}
public ToogleButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ToogleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
Log.d(Tag, "init()");
mPaint = new Paint();
mPaint.setColor(Color.GRAY);
mPaint.setAlpha(255);
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
//draw rect
RectF rectF = new RectF();
width = 80;
height = 30;
rectF.set(0, 0, width, height);
canvas.drawRoundRect(rectF, 7, 7, mPaint);
//draw half
mPaint.setColor(Color.BLUE);
RectF tButton = new RectF();
tButton.set(0, 0, width/2, height);
canvas.drawRoundRect(tButton, 7, 7, mPaint);
//draw text
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(23);
mPaint.setTextAlign(Align.CENTER);
mPaint.setTypeface(Typeface.DEFAULT);
canvas.drawText("开", 15, 25, mPaint);
canvas.drawText("关", 57, 25, mPaint);
//draw half
mPaint.setColor(Color.WHITE);
RectF button = new RectF();
button.set(0, 0, width/2, height);
canvas.drawRoundRect(button, 7, 7, mPaint);
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(Tag, "widthMeasureSpec ="+widthMeasureSpec);
Log.d(Tag, "heightMeasureSpec = "+heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
Log.d(Tag, "width="+width);
Log.d(Tag, "height="+height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN){
int eventX = (int)event.getX();
int eventY = (int)event.getY();
Log.d(Tag, "x = "+eventX+", y ="+eventY);
if(eventX <(width /2)){
Log.d(Tag, "开");
}else{
Log.d(Tag, "关");
}
}
return true;
}
}
测试程序比较简单,就是把自定义的View组件当作一般的系统View组件使用就可以了,用法一样,在此我给出一个范例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.mandr.demo.SlipButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/slipButton"
/>
</LinearLayout>
到此一个精美的按钮组件就开发完成了,希望对你以后的开发有用。附加了view的全部代码。
com\mandr\demo\ TestSlipButtonActivity.java
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
public class TestSlipButtonActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SlipButton mSlipButton=(SlipButton)findViewById(R.id.slipButton);
mSlipButton.setOnChangedListener(new OnChangedListener() {
@Override
public void OnChanged(boolean checkState) {
if(checkState){
Log.d("TestSlipButtonActivity", "checkState = "+checkState);
}else{
Log.d("TestSlipButtonActivity", "checkState = "+checkState);
}
}
});
}
}
//View的核心代码
public class ToogleButton extends View {
private static final String Tag="ToogleButton";
private int width;
private int height;
private Paint mPaint;
public ToogleButton(Context context) {
super(context);
init();
}
public ToogleButton(Context context, AttributeSet attrs) {
super(context, attrs);
init();
}
public ToogleButton(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
private void init(){
Log.d(Tag, "init()");
mPaint = new Paint();
mPaint.setColor(Color.GRAY);
mPaint.setAlpha(255);
mPaint.setAntiAlias(true);
}
@Override
protected void onDraw(Canvas canvas) {
//draw rect
RectF rectF = new RectF();
width = 80;
height = 30;
rectF.set(0, 0, width, height);
canvas.drawRoundRect(rectF, 7, 7, mPaint);
//draw half
mPaint.setColor(Color.BLUE);
RectF tButton = new RectF();
tButton.set(0, 0, width/2, height);
canvas.drawRoundRect(tButton, 7, 7, mPaint);
//draw text
mPaint.setColor(Color.BLACK);
mPaint.setTextSize(23);
mPaint.setTextAlign(Align.CENTER);
mPaint.setTypeface(Typeface.DEFAULT);
canvas.drawText("开", 15, 25, mPaint);
canvas.drawText("关", 57, 25, mPaint);
//draw half
mPaint.setColor(Color.WHITE);
RectF button = new RectF();
button.set(0, 0, width/2, height);
canvas.drawRoundRect(button, 7, 7, mPaint);
super.onDraw(canvas);
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
Log.d(Tag, "widthMeasureSpec ="+widthMeasureSpec);
Log.d(Tag, "heightMeasureSpec = "+heightMeasureSpec);
width = MeasureSpec.getSize(widthMeasureSpec);
height = MeasureSpec.getSize(heightMeasureSpec);
Log.d(Tag, "width="+width);
Log.d(Tag, "height="+height);
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
super.onTouchEvent(event);
if(event.getAction() == MotionEvent.ACTION_DOWN){
int eventX = (int)event.getX();
int eventY = (int)event.getY();
Log.d(Tag, "x = "+eventX+", y ="+eventY);
if(eventX <(width /2)){
Log.d(Tag, "开");
}else{
Log.d(Tag, "关");
}
}
return true;
}
}
测试程序比较简单,就是把自定义的View组件当作一般的系统View组件使用就可以了,用法一样,在此我给出一个范例:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<com.mandr.demo.SlipButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/slipButton"
/>
</LinearLayout>
到此一个精美的按钮组件就开发完成了,希望对你以后的开发有用。附加了view的全部代码。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2014-01-16
展开全部
可以直接用button和两张图片(开和关),用图片做背景,在点击事件中切换图片就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
刷MIUI吧 这是MIUI的样式。。。。别的ROM也许也有
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
图片加上自己的一些处理就行了,可以网上找找源码,有做好了的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
两个图片来回切换就行
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询