自定义view 搜索框

##view activity

public class MainActivity extends AppCompatActivity implements SearchView.SearchCallback{

    private SearchView searchView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        searchView = findViewById(R.id.searchView);
        searchView.setSearchCallback(this);
    }

    @Override
    public void back() {
        this.finish();
    }

    @Override
    public void btn() {
        Intent intent = new Intent(MainActivity.this,Main2Activity.class);
        startActivity(intent);
    }
}

##searchview

public class SearchView extends LinearLayout {
    private TextView btn;
    private ImageView back;
    private EditText ed_search;
    public SearchView(Context context) {
        this(context,null);
    }

    public SearchView(Context context,AttributeSet attrs) {
        this(context, attrs,0);
    }

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

    private void init(Context context, AttributeSet attrs) {
        //initAttrs(context,attrs);
        initView();
        initData();
    }

    private void initAttrs(Context context, AttributeSet attrs) {
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.SearchView);
        typedArray.getColor(R.styleable.SearchView_searchColor, Color.BLUE);
        if (typedArray!=null){
            typedArray.recycle();
        }
    }

    private void initData() {
    }

    private void initView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.search_layout,this,true);
        back = view.findViewById(R.id.back);
        ed_search = view.findViewById(R.id.ed_search);
        btn = view.findViewById(R.id.tv_search);
        back.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (searchCallback!=null){
                    searchCallback.back();
                }
            }
        });
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                searchCallback.btn();
            }
        });
    }
    private SearchCallback searchCallback;

    public void setSearchCallback(SearchCallback searchCallback) {
        this.searchCallback = searchCallback;
    }

    interface SearchCallback{
        void back();
        void btn();
    }
}

##attrs

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <declare-styleable name="SearchView" >
       <attr name="searchColor" format="color"></attr>
       <attr name="searchSize" format="dimension"></attr>
   </declare-styleable>
</resources>

##shape

<shape android:shape="rectangle"
    xmlns:android="http://schemas.android.com/apk/res/android" >
    <solid android:color="#999999"/>
    <corners android:radius="25dp"/>
    <size android:width="100dp" android:height="35dp"/>
</shape>

##search layout

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:layout_height="match_parent">
    <RelativeLayout
        android:padding="10dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
    <ImageView
        android:id="@+id/back"
        android:src="@drawable/ic_launcher_background"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <EditText
        android:id="@+id/ed_search"
        android:layout_marginLeft="5dp"
        android:layout_marginRight="5dp"
        android:layout_centerVertical="true"
        android:background="#00000000"
        android:layout_toRightOf="@id/back"
        android:layout_toLeftOf="@id/tv_search"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <TextView
        android:id="@+id/tv_search"
        android:text="搜索"
        android:textSize="30sp"
        android:layout_centerVertical="true"
        android:layout_alignParentRight="true"

        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</RelativeLayout>
</LinearLayout>

##search activity xml

<com.example.view_20190106.SearchView
        android:id="@+id/searchView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

    </com.example.view_20190106.SearchView>

猜你喜欢

转载自blog.csdn.net/weixin_44022413/article/details/85981963