如何绘制Android的一条线两个或多个单选按钮之间 [英] How to draw a line in android between two or more RadioButtons

查看:880
本文介绍了如何绘制Android的一条线两个或多个单选按钮之间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想两个或两个以上的观点类似单选按钮之间画一条线一个被选中的时候?

I want to draw a line between two or more views like radioButtons when the one is checked?

推荐答案

我想$ C $低于CS可以提供​​一些想法图纸

i think below codes can give some ideas about drawing

import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RelativeLayout;

public class MainActivity extends Activity {

      DrawView drawView;
      RadioButton r1,r2,r3,r4;    
      RelativeLayout rl1;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            r1 =(RadioButton)findViewById(R.id.radioButton1);
            r2 =(RadioButton)findViewById(R.id.radioButton2);
            r3 =(RadioButton)findViewById(R.id.radioButton3);
            r4 =(RadioButton)findViewById(R.id.radioButton4);

            rl1 =(RelativeLayout)findViewById(R.id.rl1);

            r1.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    r2.setChecked(false);
                    r3.setChecked(false);
                    r4.setChecked(false);
                    drawView = new DrawView(MainActivity.this,r4,r1);
                    drawView.setBackgroundColor(Color.WHITE);
                    rl1.addView(drawView);                  
                }
            });

            r2.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    r1.setChecked(false);
                    r3.setChecked(false);
                    r4.setChecked(false);
                    drawView = new DrawView(MainActivity.this,r1,r2);
                    drawView.setBackgroundColor(Color.WHITE);
                    rl1.addView(drawView);
                }
            });

            r3.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    r2.setChecked(false);
                    r1.setChecked(false);
                    r4.setChecked(false);
                    drawView = new DrawView(MainActivity.this,r2,r3);
                    drawView.setBackgroundColor(Color.WHITE);
                    rl1.addView(drawView);
                }
            });

            r4.setOnCheckedChangeListener(new OnCheckedChangeListener() {               
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {    
                    r2.setChecked(false);
                    r3.setChecked(false);
                    r1.setChecked(false);
                    drawView = new DrawView(MainActivity.this,r3,r4);
                    drawView.setBackgroundColor(Color.WHITE);
                    rl1.addView(drawView);
                }
            });

        }

}

drawView函数

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;

public class DrawView extends View {
    Paint paint = new Paint();
    View startView;
    View endView;    

    public DrawView(Context context,View startView,View endView) {
        super(context);
        paint.setColor(Color.YELLOW);        
        this.startView = startView;
        this.endView = endView;
    }

    @SuppressLint("NewApi")
    public void onDraw(Canvas canvas) {
            canvas.drawLine(startView.getX()+25, startView.getY()+50, endView.getX()+25, endView.getY(), paint);
    }

}

activity_main.xml     

    <RelativeLayout
        android:id="@+id/rl1"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="#ffffaa" >

    </RelativeLayout>

    <RadioButton
        android:id="@+id/radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="20dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/radioButton4"
        android:layout_alignParentTop="true"
        android:layout_marginTop="84dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_below="@+id/radioButton4"
        android:layout_marginRight="78dp"
        android:layout_marginTop="32dp"
        android:text="RadioButton" />

    <RadioButton
        android:id="@+id/radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/radioButton4"
        android:layout_alignParentRight="true"
        android:layout_marginBottom="23dp"
        android:text="RadioButton" />

</RelativeLayout>

这篇关于如何绘制Android的一条线两个或多个单选按钮之间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆