京东搜索框

Activity


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout

    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:Myattr="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.lixinyang.zhoukao1_test.MainActivity">

    <com.lixinyang.zhoukao1_test.Views.MyTitle
        android:layout_width="wrap_content"
        android:layout_height="100px"></com.lixinyang.zhoukao1_test.Views.MyTitle>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="1px"
        android:background="@color/black" />

    <com.lixinyang.zhoukao1_test.Views.MyText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Myattr:textString="热搜"
        Myattr:textSize="20dp"/>

    <com.lixinyang.zhoukao1_test.Views.MyGroupView
        android:layout_width="match_parent"
        android:layout_height="200px">

        <Button
            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="羊毛衫 新品" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="碟子" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="苹果8" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="髌骨带" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="胸罩" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="瑜伽球" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="超级实惠三只老鼠" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="冬季运动保暖裤" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="毛衣" />

        <Button

            android:layout_width="wrap_content"
            android:layout_height="70px"
            android:text="加长版风衣" />
    </com.lixinyang.zhoukao1_test.Views.MyGroupView>
    <com.lixinyang.zhoukao1_test.Views.MyText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        Myattr:textString="历史搜索"
        Myattr:textSize="20dp"/>
        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/lv"></ListView>
    <com.lixinyang.zhoukao1_test.Views.MyFooter
        android:layout_width="wrap_content"
        android:layout_height="100px"></com.lixinyang.zhoukao1_test.Views.MyFooter>
</LinearLayout>


MainActivity
public class MainActivity extends AppCompatActivity {

    private static ListView lv;
    private static List<Users> select;
    private static MyAdapter adapter;
    private static Dao dao;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        lv = (ListView) findViewById(R.id.lv);
        dao = new Dao(MainActivity.this);
        select = dao.select();
        adapter = new MyAdapter();
        lv.setAdapter(adapter);
    }
    class MyAdapter extends BaseAdapter{

        @Override
        public int getCount() {
            return select.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            if(view==null){
                view=View.inflate(MainActivity.this,R.layout.activity_item,null);
            }
            TextView tv_item1 = view.findViewById(R.id.tv_item1);
            TextView tv_item2 = view.findViewById(R.id.tv_item2);
            TextView tv_item3 = view.findViewById(R.id.tv_item3);
            TextView tv_item4 = view.findViewById(R.id.tv_item4);
            tv_item1.setText(select.get(i).getA());
            tv_item2.setText(select.get(i).getAa());
            tv_item3.setText(select.get(i).getAaa());
            tv_item4.setText(select.get(i).getAaaa());
            return view;
        }
    }
    public static void aa(){
        select = dao.select();
        lv.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }
}
public class Dao {
    //获取帮助类对象--->通过帮助类对象得到操作数据库对象(mh.getWritableDatabase())---->  SQLiteDatabase
    Mysql mh;

    public Dao(Context ctx) {
        super();
        mh=new Mysql(ctx);  //获取帮助类实例对象
    }

  /*  //执行添加操作
    public void add(){
        //获取SQLiteDatabase对象
        SQLiteDatabase database = mh.getWritableDatabase();
        //执行添加的操作
        database.execSQL("insert into t_users values (?,'zhaojunchao','15110033445')"); //无返回值

        //关闭数据库对象的方法
        database.close();
    }*/

  /*  //执行删除操作
    public void delete(){
        //获取SQLiteDatabase对象
        SQLiteDatabase database = mh.getWritableDatabase();
        //执行添加的操作  执行sql语句
        database.execSQL("delete from t_users where _id=1");  //无返回值

        //关闭数据库对象的方法
        database.close();
    }

    public List<Users> select(){
        // 创建存放所有行对象集合
        List<Users>  us=new ArrayList<Users>();

        //获取SQLiteDatabase对象
        SQLiteDatabase database = mh.getWritableDatabase();

        //执行查询的sql语句--->获取到结果集对象  Cursor
        Cursor query = database.rawQuery("select * from t_users", null);   //null是为sequel语句中的占位符赋值

        //是否有下一行
        while(query.moveToNext()){
            //获取当前行中每个列的值
            int _id = query.getInt(0);
            String uname= query.getString(1);
            String tel= query.getString(2);
            Users u=new Users(_id, uname, tel);
            us.add(u);
        }

        //关闭数据库对象的方法
        database.close();

        return us;
    }*/
  public List<Users> select() {
      // 创建存放所有行对象集合
      List<Users> us = new ArrayList<Users>();

      //获取SQLiteDatabase对象
      SQLiteDatabase database = mh.getWritableDatabase();

      //执行查询的sql语句--->获取到结果集对象  Cursor
      Cursor query = database.rawQuery("select * from t_shop", null);   //null是为sequel语句中的占位符赋值

      //是否有下一行
      while (query.moveToNext()) {
          //获取当前行中每个列的值
          String a = query.getString(0);
          String aa = query.getString(1);
          String aaa = query.getString(2);
          String aaaa = query.getString(3);
          Users u = new Users(a, aa, aaa, aaaa);
          us.add(u);
      }
      database.close();
      return us;
  }
  public void ins(Users users){
      SQLiteDatabase database = mh.getWritableDatabase();
      database.execSQL("insert into t_shop values('"+users.getA()+"','"+users.getAa()+"','"+users.getAaa()+"','"+users.getAaaa()+"')");
      database.close();
  }
  public void clear(){
      SQLiteDatabase database = mh.getWritableDatabase();
      database.execSQL("delete from t_shop");
  }
}

public class Mysql extends SQLiteOpenHelper {
    public Mysql(Context context) {
        super(context, "lixinyang", null, 1);
    }

    @Override
    public void onCreate(SQLiteDatabase sqLiteDatabase) {
        sqLiteDatabase.execSQL("create table t_shop (a varchar(20), aa varchar(11), aaa varchar(20) , aaaa varchar(20))");
        sqLiteDatabase.execSQL("insert into t_shop values('羊毛衫男','新品','恒轩祥','高领')");
        sqLiteDatabase.execSQL("insert into t_shop values('衬衣','新品','男长袖','保暖')");
    }

    @Override
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {

    }
}

public class Users {
    private String a;
    private String aa;
    private String aaa;
    private String aaaa;

    public Users(String a,String aa,String aaa,String aaaa) {
        this.a = a;
        this.aa = aa;
        this.aaa = aaa;
        this.aaaa = aaaa;
    }

    public String getA() {
        return a;
    }

    public void setA(String a) {
        this.a = a;
    }

    public String getAa() {
        return aa;
    }

    public void setAa(String aa) {
        this.aa = aa;
    }

    public String getAaa() {
        return aaa;
    }

    public void setAaa(String aaa) {
        this.aaa = aaa;
    }

    public String getAaaa() {
        return aaaa;
    }

    public void setAaaa(String aaaa) {
        this.aaaa = aaaa;
    }
}


public class MyFooter extends RelativeLayout {
    public MyFooter(Context context) {
        this(context,null);
    }

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

    public MyFooter(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.activity_footer, this,true);
        Button btn = view.findViewById(R.id.btn_clear);
        btn.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Dao dao = new Dao(getContext());
                dao.clear();
                MainActivity.aa();
            }
        });
    }
}
public class MyGroupView extends ViewGroup {
    Button btn;
    View view;
    AttributeSet attributeSet;
    public MyGroupView(Context context) {
        this(context,null);
    }

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

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


    @Override
    public ViewGroup.LayoutParams generateLayoutParams(AttributeSet attrs)
    {
        return new MarginLayoutParams(getContext(), attrs);
    }

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

    }

    @Override
    protected void onLayout(boolean b, int i, int i1, int i2, int i3) {
/**
 * 一般是定义为int top;一个top实际上是数组的下标
 left : 指定矩形框左上角的x坐标
 top: 指定矩形框左上角的y坐标
 right: 指定矩形框右下角的x坐标
 bottom:指定矩形框右下角的y坐标
 */
        int width = getWidth();
        int height = getHeight();
        int tw = 0;
        int th = 0;
        for (int ii = 0; ii < getChildCount(); ii++) {
            View child = getChildAt(ii);
            view = child;
            btn = (Button) view;
            btn.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View view) {
                    Dao dao = new Dao(getContext());
                    Button bt = (Button) view;
                    String a = bt.getText().toString();
                    Users users = new Users(a,"新品","特别厚","挨冻");
                    dao.ins(users);
                    MainActivity.aa();
                    MyTitle.ss(a);
                }
            });

            if (tw + child.getWidth() < width) {

            } else {
                tw = 0;
                th += child.getMeasuredHeight();   //超过屏幕的宽度,自动换行
            }

            child.layout(tw, th, tw + child.getMeasuredWidth(), th + child.getMeasuredHeight());
            tw += child.getMeasuredWidth();
        }
    }

}
public class MyText extends View {
    private float size;
    private String content;
    private Paint paint;
    private Rect rect;
    public MyText(Context context) {
        this(context,null);
    }

    public MyText(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public MyText(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        //获取到你控件的属性并形成数组
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.mystyle, defStyleAttr, 0);
        //取出里面的属性
        content = ta.getString(R.styleable.mystyle_textString);
        size = ta.getDimension(R.styleable.mystyle_textSize, 40);
        //初始化画笔
        paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        paint.setTextSize(size);
        rect = new Rect();
        //第一个参数放置你的文本
        // 第二个参数是你字符串开始的下标
        //第三个参数是你要字符串的长度
        //第四个参数返回给调用者
        paint.getTextBounds(content,0,content.length(), rect);
    }
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawText(content,0, 30,paint);
    }
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        //获取测量的模式和测量的尺寸
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        int widthSize = MeasureSpec.getSize(widthMeasureSpec);
        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        int heightSize = MeasureSpec.getSize(heightMeasureSpec);
        int width;
        int height;
        //判断测量模式并将你的控件大小改变
        if(widthMode==MeasureSpec.EXACTLY){
            width=widthSize;
        }else{
            int width1 = rect.width();
            width=width1+getPaddingLeft()+getPaddingRight()+10;
        }
        if(heightMode==MeasureSpec.EXACTLY){
            height=heightSize;
        }else{
            int height1 = rect.height();
            height = height1+getPaddingTop()+getPaddingBottom()+10;
        }
        //将屏幕宽高度从新赋值
        setMeasuredDimension(width,height);
    }

}

public class MyTitle extends RelativeLayout implements View.OnClickListener {

    private static EditText editText;
    private ImageView imageView;
    private TextView textView;

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

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

    public MyTitle(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        View view = LayoutInflater.from(context).inflate(R.layout.activity_title, this, true);
        editText = view.findViewById(R.id.et);
        imageView = view.findViewById(R.id.iv);
        textView = view.findViewById(R.id.tv);
        imageView.setOnClickListener(this);
        textView.setOnClickListener(this);
    }

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

    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
            switch(event.getAction()){
                    case MotionEvent.ACTION_DOWN:

                    break;
                }
        return true;
    }

    @Override
    public void onClick(View view) {
            switch(view.getId()){
                    case R.id.iv:
                        Toast.makeText(getContext(),"我点了返回",Toast.LENGTH_SHORT).show();
                    break;
                    case R.id.tv:
                        Dao dao = new Dao(getContext());
                        String a = editText.getText().toString();
                        Users users = new Users(a,"新品","特别厚","挨冻");
                        dao.ins(users);
                        MainActivity.aa();
                    break;

                }
    }
    public static void ss(String str){
        editText.setText(str);
    }
}
activity_footer.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="match_parent">
    <Button
        android:layout_width="500px"
        android:layout_height="70px"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:text="清空历史记录"
        android:background="@drawable/buttonborder"
        android:id="@+id/btn_clear"
        />
    <ImageView

        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/delete"
        android:layout_centerVertical="true"
        android:layout_marginLeft="250px"/>
</RelativeLayout>
activity_item.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="match_parent">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/tv_item1"/>
    <LinearLayout
        android:layout_width="200px"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_alignParentRight="true">
    <TextView
        android:background="@drawable/buttonborder"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/tv_item2"/>
    <TextView
        android:background="@drawable/buttonborder"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/tv_item3"/>
    <TextView
        android:background="@drawable/buttonborder"
        android:layout_width="0px"
        android:layout_weight="1"
        android:layout_height="wrap_content"
        android:id="@+id/tv_item4"/>
    </LinearLayout>

</RelativeLayout>

activity_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="match_parent">
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/bb"
        android:id="@+id/iv"
        android:layout_alignParentLeft="true"
        android:layout_centerVertical="true"
        android:layout_marginLeft="30px"/>
    <EditText
        android:layout_width="500px"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:background="@drawable/radioimage"
        android:layout_centerVertical="true"
        android:id="@+id/et"
        android:gravity="center"/>
    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_marginLeft="80dp"
        android:src="@drawable/dd" />
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="搜索"
        android:layout_alignParentRight="true"
        android:layout_centerVertical="true"
        android:layout_marginRight="30px"
        android:id="@+id/tv"/>
</RelativeLayout>

atts

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <attr name="textString" format="string"></attr>
    <attr name="textSize" format="dimension"></attr>
    <declare-styleable name="mystyle">
        <attr name="textString"></attr>
        <attr name="textSize"></attr>
    </declare-styleable>
</resources>


阅读更多
想对作者说点什么?

博主推荐

换一批

没有更多推荐了,返回首页