android 历史记录跟搜索

 
 
<? xml version= "1.0" encoding= "utf-8" ?>
< RelativeLayout
xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: app = "http://schemas.android.com/apk/res-auto"  
xmlns: tools = "http://schemas.android.com/tools"  
android :layout_width= "match_parent"  
android :layout_height= "match_parent"  
tools :context= "com.example.adoptionofreplies.LiShiActivity">
  < LinearLayout  
android :orientation= "horizontal"  
android :layout_width= "match_parent"  
android :layout_height= "wrap_content"
android :id= "@+id/line" >
< ImageView  
android :layout_width= "40sp"  
android :layout_height= "match_parent"  
android :id= "@+id/hui"  
android :background= "@drawable/leftjiantou"  
/>

< EditText  
android :layout_width= "match_parent"
android :layout_height= "match_parent"  
android :layout_centerHorizontal= "true"  
android :padding= "7dp"  
android :layout_toLeftOf= "@id/img"
android :hint= "请输入想要的物品"  
android :gravity= "center"  
android :layout_toRightOf= "@id/hui"  
android :background= "@drawable/shape"
android :id= "@+id/edit"  
android :layout_weight= "1"  
/>
 < Button  
android :layout_width= "wrap_content"  
android :layout_height= "wrap_content"  
android :id= "@+id/img"  
android :text= "搜索"  
android :layout_alignParentRight= "true"
/>
 </ LinearLayout>
 < LinearLayout  
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :orientation= "vertical"  
android :id= "@+id/lina"  
android :layout_below= "@id/line"  
>
 < TextView  
android :layout_width= "match_parent"  
android :layout_height= "wrap_content"
android :text= "搜索"  
android :textSize= "20sp"
/>
 < ScrollView  
android :layout_width= "match_parent"
android :layout_height= "wrap_content">
 < com.fynn.fluidlayout.FluidLayout
android :id= "@+id/fluid_layout"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :padding= "5dp">
</ com.fynn.fluidlayout.FluidLayout>
</ ScrollView>
</ LinearLayout>
< LinearLayout  
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :orientation= "vertical"  
android :layout_marginTop= "20dp"
android :id= "@+id/lins"
android :layout_below= "@id/lina"  
>
 < TextView
android :layout_width= "match_parent"  
android :layout_height= "wrap_content"  
android :text= "历史记录"  
android :textSize= "30sp"  
/>
 < ListView  
android :layout_width= "match_parent"
android :layout_height= "match_parent"
android :id= "@+id/list_view"  
android :divider= "@null"  
 </ ListView>
</ LinearLayout>
 < Button
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "清空历史"  
android :id= "@+id/button"
android :layout_alignParentBottom= "true" />
</ RelativeLayout>

//主界面
 
  
package
com.example.adoptionofreplies; import
android.content.Context; import
android.content.Intent; import
android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import com.example.adoptionofreplies.utils.MessageEvent;
import com.fynn.fluidlayout.FluidLayout;
import org.greenrobot.eventbus.EventBus;
import java.util.ArrayList;
import java.util.List;

public class LiShiActivity extends AppCompatActivity {
private EditText edit;
private ListView lv;
private Button img;
private Button btn;
private List<String> list = new ArrayList<>();
private ImageView hui;
private TextView textView;
private String name;
private FluidLayout fluidLayout;
String[] arrs={ "手机", "iPad", "充电宝", "倩女幽魂", "单机斗地主", "天堂战记", "妖精的尾巴", "极限挑战", "我们相爱吧", "倚天屠龙记", "明星大侦探"
};
private TextView text1;
private MyAdapter adapter;
@Override protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

setContentView(R.layout. activity_li_shi);
edit = findViewById(R.id. edit);
lv = findViewById(R.id. list_view);
img = findViewById(R.id. img);
btn = findViewById(R.id. button);
hui = findViewById(R.id. hui);
fluidLayout = findViewById(R.id. fluid_layout);

  //搜索
getTag();

  //回调
hui.setOnClickListener( new View.OnClickListener() {
@Override  
public void onClick(View v) {
EventBus. getDefault().post( new MessageEvent( ""));
finish();
}
});

  //跳转传值
img.setOnClickListener( new View.OnClickListener() {
@Override  
public void onClick(View v) {
name = edit.getText().toString();
if( edit.length()== 0){
Toast. makeText(LiShiActivity. this, "输入不能为空",Toast. LENGTH_SHORT).show();
//弹出一个自动消失的提示框return;  
} else{            
          //添加name到适配器    
  list.add( name);
adapter = new MyAdapter(LiShiActivity. this, list);
lv.setAdapter( adapter);
//跳转传值
Intent intent = new Intent(LiShiActivity. this,HuDiaoActivity. class);
intent.putExtra( "name", name);
startActivity(intent);
}
}
});

//清空历史记录
btn.setOnClickListener( new View.OnClickListener() {
@Override  
public void onClick(View v) {
list.clear(); adapter.notifyDataSetChanged();
}
});
}

  //搜索
public void getTag() {
//循环数据  
for ( int i= 0;i< arrs. length;i++){
text1 = new TextView( this);
text1.setText( arrs[i]);
//文字大小  
text1.setTextSize( 13);
//搜索的字
final String s = text1.getText().toString();
//上下左右的距离  
FluidLayout.LayoutParams params = new FluidLayout.LayoutParams(ViewGroup.LayoutParams. WRAP_CONTENT,ViewGroup.LayoutParams. WRAP_CONTENT);
//上下左右的距离 
         params.setMargins( 12, 12, 32, 12);
fluidLayout.addView( text1,params);
text1.setOnClickListener( new View.OnClickListener() {
@Override  
public void onClick(View v) {
//搜索添加到历史记录  
list.add( s);
adapter = new MyAdapter(LiShiActivity. this, list);
lv.setAdapter( adapter);
adapter.notifyDataSetChanged();
}
});
}
}

  //适配器
class MyAdapter extends BaseAdapter{
private Context context;
private List<String> list;
public MyAdapter(Context context, List<String> list) {
this. context = context;
this. list = list; }
@Override  
public int getCount() {
return list.size();
}
@Override
public Object getItem( int position) {
return list.get(position);
}
@Override  
public long getItemId( int position) {
return position;
}
@Override
public View getView( int position, View convertView, ViewGroup parent) {
//添加到历史记录
textView = new TextView( context);
//字体大小
textView.setTextSize( 20);
textView.setText( list.get(position)); return textView;
}
}
}

猜你喜欢

转载自blog.csdn.net/l6666_6666/article/details/79966853