搜索框

流失布局,搜索框

<?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="match_parent"
    android:orientation="horizontal">

    <ImageView
        android:id="@+id/imageView_linear_left"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:src="@drawable/fanhui" />

    <EditText
        android:id="@+id/edit_linear"
        android:layout_width="0dp"
        android:layout_height="60dp"
        android:layout_weight="1" />

    <ImageView
        android:id="@+id/imageView_linear_right"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/tianjia" />

    <ImageView
        android:id="@+id/imageView_linear_chaxun"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:background="@drawable/pink_one" />

</LinearLayout>
package com.bawei.lmx.day13_2.cliear;

import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.bawei.lmx.day13_2.R;

public class CliearLayout extends LinearLayout {

    private ImageView imageView_linear_left;
    private EditText edit_linear;
    Call call;
    private ImageView imageView_linear_right;

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

    public CliearLayout(Context context, AttributeSet attrs) {
        this(context, attrs, 2);
    }

    public CliearLayout(final Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View inflate = inflate(context, R.layout.linearlayout, this);
        imageView_linear_left = inflate.findViewById(R.id.imageView_linear_left);
        imageView_linear_right = inflate.findViewById(R.id.imageView_linear_right);
        edit_linear = inflate.findViewById(R.id.edit_linear);
        imageView_linear_right.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (call != null) {
                    String trim = edit_linear.getText().toString().trim();
                    call.result(trim);
                } else {
                    Toast.makeText(context, "输入内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        measureChildren(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        int width = r - l;
        int x = 0;
        int y = 0;
        int row = 1;
        int childCount = getChildCount();
        for (int i = 0; i < childCount; i++) {
            View childAt = getChildAt(i);
            x += childAt.getMeasuredWidth();
            if (x > width) {
                x = childAt.getMeasuredWidth();
                row++;
            }
            y = row * childAt.getMeasuredHeight();
            childAt.layout(x - childAt.getMeasuredWidth(), y - childAt.getMeasuredHeight(), x, y);
        }
    }

    public void setCall(Call call) {
        this.call = call;
    }

    public interface Call {
        void result(String message);
    }
}


    <com.bawei.lmx.day13_2.cliear.CliearLayout
        android:id="@+id/cliearx"
        android:layout_width="match_parent"
        android:layout_height="200dp"></com.bawei.lmx.day13_2.cliear.CliearLayout>
package com.bawei.lmx.day13_2;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.bawei.lmx.day13_2.cliear.CliearLayout;

public class ChaxunActivity extends AppCompatActivity {

    private CliearLayout cliearx;
    private ImageView fanhui;
    private static final String TAG = "ChaxunActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chaxun);
        fanhui = findViewById(R.id.imageView_linear_left);
        fanhui.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        cliearx = findViewById(R.id.cliearx);

        cliearx.setCall(new CliearLayout.Call() {
            @Override
            public void result(final String message) {
                if (!TextUtils.isEmpty(message)) {
                    TextView textView = new TextView(ChaxunActivity.this);
                    textView.setText(message);
                    textView.setTextSize(22);
                    textView.setTextColor(R.drawable.pink_one);
                    cliearx.addView(textView);
                    Log.i(TAG, "result: " + message);
                    textView.setPadding(20, 20, 20, 20);
                    Toast.makeText(ChaxunActivity.this, message, Toast.LENGTH_SHORT).show();
                    cliearx.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            Intent intent = getIntent();
                            intent.putExtra("value", message);
                            setResult(2, intent);
                            finish();
                        }
                    });
                } else {
                    Toast.makeText(ChaxunActivity.this, "输入内容不能为空", Toast.LENGTH_SHORT).show();
                }
            }
        });
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值