自定义组合控件,自定义View,接口点击事件



先写自定义组合控件的布局LoginView 继承自LinearLayout

public class LoginViewextends LinearLayoutimplements View.OnClickListener{


    private Context _context;
    public LoginView(Context context) {
        super(context);
    }

    public LoginView(Context context, AttributeSet attrs) {
        super(context, attrs);
        _context = context;

        //将指定布局id填充到当前页面
        View view = LayoutInflater.from(context).inflate(R.layout.view_login, this, true);

        Button right_btn = view.findViewById(R.id.right_btn);

        right_btn.setOnClickListener(this);
    }

    public LoginView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void onClick(View view) {
        /*if(view.getId()==R.id.right_btn){
            Toast.makeText(_context,"right",Toast.LENGTH_SHORT).show();
        }*/
        //调用接口的方法
        loginViewClickListener.loginViewButtonClicked(view);
    }

    //定义接口的成员
    OnLoginViewClickListener loginViewClickListener;
    //接口的setter
    public void setOnLoginViewClickListenler(OnLoginViewClickListener loginViewClickListener){
         //setter中把这个接口的实现赋值给这个loginview的上面定义的接口
          this.loginViewClickListener = loginViewClickListener;
    }


    //接口
    public interface OnLoginViewClickListener{
        public void loginViewButtonClicked(View v);//传的参数
    }
}

写自定义组合控件LoginView 的对应的布局view_login

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <LinearLayout
        android:layout_gravity="center_vertical"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:background="#D4DBED"
        android:orientation="horizontal">

        <Button

            android:textSize="23sp"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:background="#D4DBED"
            android:text="" />

        <TextView
            android:textSize="23sp"
            android:gravity="center"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:background="#D4DBED"
            android:text="那些花儿" />

        <Button
            android:id="@+id/right_btn"
            android:textSize="23sp"
            android:layout_width="80dp"
            android:layout_height="wrap_content"
            android:background="#D4DBED"
            android:text="⑤" />
    </LinearLayout>
</LinearLayout>
activity_main里面的,将自定义组合控件引入进来

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    >

  <com.example.a171104zhouliu_moniti.LoginView
      android:id="@+id/loginView"
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"/>

<LinearLayout
    android:gravity="center"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="6"
   >

  <com.example.a171104zhouliu_moniti.CustomProgressView
      android:id="@+id/custom_ProgressView"
      android:layout_width="200dp"
      android:layout_height="200dp"
     />

  <Button
      android:id="@+id/saomiao"
      android:text="扫描二维码"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      />

</LinearLayout>
</LinearLayout>

MainActivity里面的代码,拿到自定义控件的id,,new出接口,点击按钮执行进度条
public class MainActivity extends AppCompatActivity {

    private LoginView loginView;
    private Button btnsaomiao;
    private CustomProgressView customProgressView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //拿到滚动条的视图
        customProgressView = (CustomProgressView) findViewById(R.id.custom_ProgressView);
        btnsaomiao = (Button) findViewById(R.id.saomiao);
        loginView = (LoginView) findViewById(R.id.loginView);
        //在这个实现上把接口new出来
        loginView.setOnLoginViewClickListenler(new LoginView.OnLoginViewClickListener() {
            @Override
            public void loginViewButtonClicked(View v) {
                 //判断当前点击的v是哪个
                if(v.getId()==R.id.right_btn){
                   // Toast.makeText(MainActivity.this, "Login", Toast.LENGTH_LONG).show();
                    //点击右边的按钮 跳转到 secondActivity
                    Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                    startActivity(intent);
                }
            }
        });


        //扫描按钮的点击事件
        btnsaomiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
             //点击按钮 调用滚动条的方法
             customProgressView.start();
            }
        });
    }
}


自定义圆和进度条CustomProgressView的代码
public class CustomProgressView extends View{
    //定义画笔
    private Paint paint;
    //进度的值
    private int progress = 0;//一开始是0
    //控制循环的变量
    private boolean running = true;
 Context context;
    public CustomProgressView(Context context) {
        super(context);
    }

    public CustomProgressView(Context context, AttributeSet attrs) {
        super(context, attrs);
      this.context = context;
        //创建画笔
        paint = new Paint();
        paint.setAntiAlias(true);//抗锯齿
        paint.setColor(Color.GREEN);
        paint.setStyle(Paint.Style.STROKE);//填充是空心的

 }

    //抽成一个方法,,
    public void start(){

        new Thread(new Runnable() {
            @Override
            public void run() {
                //循环
                while(running){

                    if(progress>=360){
                        //如果进度已经大于了360,就跳出
                        running = false;

                        Intent intent = new Intent(context, CaptureActivity.class);
                        context.startActivity(intent);
                        return;
                    }

                    progress+=10;//进度每次加10

                    //子线程刷新,
                    postInvalidate();

                    try {
                        //睡眠
                        Thread.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }
    public CustomProgressView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

        //获取当前view 的宽度,x,y确定了圆心的位置
        int x = getWidth()/2;
        int y = getHeight()/2;

        int radius = 100;//圆的半径


        //利用这个rectf来确定弧形的范围
        RectF rectF = new RectF(x-radius,y-radius,x+radius,y+radius);
        paint.setColor(Color.GRAY);
        paint.setStrokeWidth(20);
        canvas.drawCircle(x,y,radius,paint);
        //user center false不从中心点开始
        paint.setColor(Color.GREEN);
        paint.setStrokeWidth(20);
       canvas.drawArc(rectF,-90,progress,false,paint);

        //计算百分比的值,,整体强转成int
        int zhi = (int) ((float) progress/360*100);
        //计算百分比的值的位置
        float zhiWidth = paint.measureText(zhi+"%");

        //根据这个矩形 求 百分比值的 高度
        Rect rectZhi = new Rect();
        paint.getTextBounds(zhi+"%",0,(zhi+"%").length(),rectZhi);
        //字符串的高度:rectZhi.height()
        paint.setTextSize(15);
        paint.setStrokeWidth(1);

        //画文字
      canvas.drawText(zhi+"%",x-zhiWidth/2,x+zhiWidth/2,paint);

    }
}
点击组合控件上的按钮,跳转到secondAcitivity里面,,布局里面引入自定义阶梯矩阵的
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
  android:orientation="vertical"
   >

    <com.example.a171104zhouliu_moniti.LoginView
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
       ></com.example.a171104zhouliu_moniti.LoginView>

    <com.example.a171104zhouliu_moniti.CustomJieTiRect
        android:layout_width="match_parent"
        android:layout_weight="5"
        android:layout_height="0dp" />

</LinearLayout>

自定义阶梯矩阵的代码
public class CustomJieTiRect extends View{
    public CustomJieTiRect(Context context) {
        super(context);
    }

    public CustomJieTiRect(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public CustomJieTiRect(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);


        Paint paint1 = new Paint();
        paint1.setTextSize(10);
        paint1.setColor(Color.BLACK);
         Rect rect = new Rect(0,0,250,80);
        canvas.drawRect(rect,paint1);

        Paint paint2 = new Paint();
        paint2.setTextSize(10);
        paint2.setColor(Color.RED);
        Rect rect1 = new Rect(250,80,500,160);
        canvas.drawRect(rect1,paint2);

        Paint paint3 = new Paint();
        paint3.setTextSize(10);
        paint3.setColor(Color.BLUE);
        Rect rect2 = new Rect(500,160,1000,250);
        canvas.drawRect(rect2,paint3);
      /*  canvas.drawRect(0,0,150,80,paint1);
        canvas.drawRect(150,80,300,160,paint2);
        canvas.drawRect(300,160,500,250,paint3);*/

             /*Paint paint = new Paint();
               paint.setColor(Color.BLUE);
               paint.setAntiAlias(true);
               canvas.drawRect(0,0,getWidth()/3,70,paint);
              paint.setColor(Color.GRAY);
             canvas.drawRect(getWidth()/3,70,getWidth()/2+70,140,paint);
              paint.setColor(Color.RED);
                canvas.drawRect(getWidth()/2+70,140,getWidth(),210,paint);*/


 }}
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Android自定义组合是指通过将现有的多个组合起来,形成一个新的自定义件,以实现特定的功能或界面效果。通过组合现有的件,我们可以更灵活地满足项目需求,并减少重复编写代码的工作量。 在Android中,我们可以使用布局文件XML来定义定义件的组合。首先,我们需要创建一个新的布局文件,其中包含多个现有的件。然后,我们可以通过在Java代码中引用这个布局文件,并对其中的件进行操作和设置属性。 以下是一个简单的示例,演示如何创建一个自定义组合: 1. 创建一个新的布局文件,例如"custom_view.xml",在该文件中添加需要组合的多个件,如TextView、Button等。例如: ```xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/textView" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Hello" /> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Click Me" /> </LinearLayout> ``` 2. 在Java代码中引用该布局文件,并进行相应的操作。例如,在一个Activity中,我们可以通过setContentView方法将该布局文件设置为当前Activity的布局。 ```java public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.custom_view); // 对自定义件进行操作 TextView textView = findViewById(R.id.textView); Button button = findViewById(R.id.button); // 设置监听器等其他操作... } } ``` 通过上述步骤,我们就可以将多个现有的组合成一个新的自定义件,实现特定的功能或界面效果。当然,在实际应用中,可能还需要对组合件进行进一步的自定义和功能扩展。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值