【问题标题】:How to draw a line in android between two or more RadioButtons如何在两个或多个 RadioButtons 之间在 android 中画一条线
【发布时间】:2012-12-09 05:26:36
【问题描述】:

当一个被选中时,我想在两个或多个视图(如 radioButtons)之间画一条线?

【问题讨论】:

    标签: android line draw


    【解决方案1】:

    我认为下面的代码可以提供一些关于绘图的想法

    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);
                    }
                });
    
            }
    
    }
    

    绘图视图

    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>
    

    【讨论】:

    • 感谢您的代码,它非常有帮助。但是当我运行代码并按下单选按钮时,应用程序崩溃,因为 onDraw 方法:public void onDraw(Canvas canvas) { canvas.drawLine(startView .getX()+25, startView.getY()+50, endView.getX()+25, endView.getY(), 油漆); }
    • 问题是我试图在较旧的模拟器 Android 2.2 中运行该应用程序。但是当我现在尝试在新的模拟器 Android 4 中运行它时,它运行良好。再次感谢
    • 好的,很高兴听到解决了您的问题:) 顺便说一句,请不要忘记通过点击图标来支持我的答案
    • 感谢您的关注,我可以在两个图像视图之间画线而不是这些单选按钮吗?希望你能尽快回复。谢谢你
    • 很好的解释。感谢发帖。
    【解决方案2】:

    我认为您可以在类中创建两个单选按钮实例,然后使用画布在它们之间进行绘制。我想你可以得到它们的坐标。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-07-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多