仿京东首页搜索框

//自定义布局


    

    

        

            
            
        

        

            

            
            
        

        

            
            
        
    


//自定义类
public class SearchActivity extends RelativeLayout{

    private Context mycon;
    private static EditText ed;
    Suocha s;
    public SearchActivity(Context context) {
        this(context,null);
    }

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

    public SearchActivity(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //把上下文写成全局变量
        mycon=context;
        //初始化布局
        LayoutInflater inflater = LayoutInflater.from(context);
        View view = inflater.inflate(R.layout.aa, this, true);
        TextView cha = view.findViewById(R.id.cha);
        ed = view.findViewById(R.id.ed);
        cha.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                String s1 = ed.getText().toString();
                s.dianji(s1);
            }
        });
    }



    //写一个接口
    public interface Suocha{
        void dianji(String sou);
    }

    //写一个方法给外部访问
    public void fang(Suocha s){
        this.s=s;
    }

    public static void setEd(String e){
          ed.setText(e);
    }
}

//shape   

home_title_bar_search_corner_bg


    android:shape="rectangle">

   
   
   



你可能感兴趣的:(仿京东首页搜索框)