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;
}
}
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在Android应用中实现搜索功能并显示历史记录,你可以按照以下步骤进行: 1. 创建一个搜索界面,包括一个搜索框和一个搜索按钮。 2. 使用Shared Preferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。 3. 在搜索界面中添加一个“搜索历史记录”按钮,当用户点击该按钮时,从SharedPreferences中读取搜索历史记录显示在一个列表中。 4. 当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。 以下是一个简单的示例代码的实现: ```java public class SearchActivity extends AppCompatActivity { private EditText mSearchEditText; private Button mSearchButton; private Button mHistoryButton; private SharedPreferences mSharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); mSharedPreferences = getPreferences(MODE_PRIVATE); mSearchEditText = findViewById(R.id.search_edit_text); mSearchButton = findViewById(R.id.search_button); mHistoryButton = findViewById(R.id.history_button); mSearchButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { String keyword = mSearchEditText.getText().toString(); addKeywordToHistory(keyword); // 执行搜索操作 } }); mHistoryButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { showHistoryList(); } }); } private void addKeywordToHistory(String keyword) { SharedPreferences.Editor editor = mSharedPreferences.edit(); Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); history.add(keyword); editor.putStringSet("history", history); editor.apply(); } private void showHistoryList() { Set<String> history = mSharedPreferences.getStringSet("history", new HashSet<String>()); final String[] historyArray = history.toArray(new String[history.size()]); AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("历史记录"); builder.setItems(historyArray, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialogInterface, int i) { mSearchEditText.setText(historyArray[i]); // 执行搜索操作 } }); builder.show(); } } ``` 在这个示例代码中,我们使用SharedPreferences来存储搜索历史记录。当用户点击搜索按钮时,将搜索关键字添加到SharedPreferences中。当用户点击历史记录按钮时,从SharedPreferences中读取搜索历史记录显示在一个列表中。当用户点击历史记录列表中的一个记录时,将该记录填充到搜索框中,并执行搜索操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值