自定义标题栏接口回调实现点击事件

1.TitleView类

package com.example.rikaoti;

import android.content.Context;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

public class TitleView extends LinearLayout {

    private TextView tvBack;
    private TextView tvSubmit;

    public TitleView(Context context) {
        this(context, null);
    }

    public TitleView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        LayoutInflater.from(context).inflate(R.layout.title, this);
        tvBack = findViewById(R.id.tvBack);
        tvSubmit = findViewById(R.id.tvSubmit);
    }

    public void setBackClickListener(OnClickListener onClickListener) {
        tvBack.setOnClickListener(onClickListener);
    }

    public void setSubmitClickListener(OnClickListener onClickListener) {
        tvSubmit.setOnClickListener(onClickListener);
    }
   /* @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvBack:
                Toast.makeText(getContext(), "返回", Toast.LENGTH_SHORT).show();
                break;
            case R.id.tvSubmit:
                Toast.makeText(getContext(), "提交", Toast.LENGTH_SHORT).show();
                break;
        }
    }*/
}

2.MainActivity类

package com.example.rikaoti;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TitleView titleView = findViewById(R.id.title);
        titleView.setBackClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "返回", Toast.LENGTH_SHORT).show();
            }
        });
        titleView.setSubmitClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "提交", Toast.LENGTH_SHORT).show();
            }
        });
    }

}

3.title.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                android:background="#ff3660">

    <TextView
        android:id="@+id/tvBack"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:text="返回"
        android:textSize="20sp"
        android:textColor="#ffffff"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="标题"
        android:textColor="#ffffff"
        android:textSize="25sp"/>

    <TextView
        android:id="@+id/tvSubmit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:text="提交"
        android:textColor="#ffffff"
        android:textSize="20sp"/>
</RelativeLayout>

猜你喜欢

转载自blog.csdn.net/sui_yz/article/details/79933254