Android 搜索内容保存历史记录

news/2025/3/29 19:36:58/

Android 搜索内容保存历史记录

一、界面布局

主界面布局 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".MainActivity"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:padding="5dp"android:layout_marginTop="10dp"><EditTextandroid:id="@+id/e1"android:layout_weight="5"android:layout_width="0dp"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@android:color/transparent"android:maxLength="20"android:textSize="13sp"android:textColor="#000"android:hint="输入关键词进行搜索"/><Buttonandroid:id="@+id/b1"android:layout_weight="1"android:layout_width="0dp"android:layout_height="wrap_content"android:text="确定"android:background="@null"/></LinearLayout><Viewandroid:layout_width="match_parent"android:layout_height="1dp"android:layout_marginTop="5dp"android:background="#74D5D5D5"/><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:orientation="horizontal"><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="历史搜索"android:textColor="#000"android:padding="20dp"android:textSize="16sp"/><TextViewandroid:id="@+id/t1"android:layout_width="match_parent"android:layout_height="wrap_content"android:gravity="end"android:layout_marginRight="20dp"android:text="清除搜索记录"/></LinearLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/r1"android:layout_width="match_parent"android:layout_height="match_parent"android:padding="20dp"/></LinearLayout>

次界面布局 activity_main2.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"android:orientation="vertical"tools:context=".MainActivity2"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="50dp"android:orientation="horizontal"android:background="#FF6200EE"><ImageViewandroid:id="@+id/fh"android:layout_width="20dp"android:layout_height="20dp"android:layout_gravity="center"android:layout_marginLeft="20dp"android:src="@mipmap/photos3"/><TextViewandroid:layout_width="match_parent"android:layout_height="match_parent"android:layout_marginRight="20dp"android:gravity="center"android:textSize="20sp"android:textColor="#fff"android:text="商品列表"/></LinearLayout><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginTop="10dp"><LinearLayoutandroid:layout_width="350dp"android:layout_height="wrap_content"android:layout_marginLeft="20dp"android:background="#2CCACACA"><ImageViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@mipmap/photos4"/><TextViewandroid:id="@+id/e1"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_gravity="center"android:background="@android:color/transparent"android:maxLength="20"android:textSize="13sp"/></LinearLayout><ImageViewandroid:id="@+id/i3"android:layout_width="wrap_content"android:layout_height="wrap_content"android:src="@mipmap/photos11"/></LinearLayout><LinearLayoutandroid:id="@+id/linearLayout"android:layout_width="match_parent"android:layout_height="50.0dip"android:background="#FFFFFF"><!--layout_weight这个属性为权重,让两个textview平分这个linearLayout--><TextViewandroid:id="@+id/t1"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1.0"android:gravity="center"android:text="综合"android:textSize="15dp"android:textColor="#F36778D5" /><TextViewandroid:id="@+id/t2"android:layout_width="match_parent"android:layout_height="match_parent"android:layout_weight="1.0"android:gravity="center"android:text="销量"android:textSize="15dp"android:textColor="#000000" /><LinearLayoutandroid:layout_weight="1.0"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/t3"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="价格"android:textSize="15dp"android:textColor="#000000" /><ImageViewandroid:id="@+id/i1"android:layout_width="20dp"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@mipmap/photos8"/></LinearLayout><LinearLayoutandroid:layout_weight="1.0"android:layout_width="match_parent"android:layout_height="match_parent"android:gravity="center"android:orientation="horizontal"><TextViewandroid:id="@+id/t4"android:layout_width="wrap_content"android:layout_height="match_parent"android:gravity="center"android:text="筛选"android:textSize="15dp"android:textColor="#000000" /><ImageViewandroid:id="@+id/i2"android:layout_width="20dp"android:layout_height="wrap_content"android:layout_gravity="center"android:src="@mipmap/photos9"/></LinearLayout></LinearLayout><androidx.recyclerview.widget.RecyclerViewandroid:id="@+id/r1"android:layout_width="match_parent"android:layout_height="match_parent"></androidx.recyclerview.widget.RecyclerView>
</LinearLayout>

列表界面布局 list.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_marginTop="20dp"><TextViewandroid:id="@+id/t1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:textSize="15dp"android:singleLine="true"android:layout_marginRight="5dp"android:layout_marginLeft="5dp"android:text="椅子"android:textColor="#000"/>
</LinearLayout>

界面展示
在这里插入图片描述
二、功能实现
新建一个Adapter.class

package com.likeit.note7;import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;public class Adapter extends RecyclerView.Adapter<Adapter.ViewHolder> {private String[]a;private int length;private String result;Adapter(String[] a, String result) {this.a=a;this.result=result;}@NonNull@Overridepublic Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list, parent, false);ViewHolder viewHolder = new ViewHolder(view);return viewHolder;}@Overridepublic void onBindViewHolder(Adapter.ViewHolder holder, int position) {if(result.equals("1")){holder.t1.setText("");}else{holder.t1.setText(a[position]);}}@Overridepublic int getItemCount() {try {length=a.length;}catch (Exception e){e.printStackTrace();length=0;}return length;}public class ViewHolder extends RecyclerView.ViewHolder {private TextView t1;public ViewHolder(@NonNull View itemView) {super(itemView);t1 = itemView.findViewById(R.id.t1);//绑定id}}
}

MainActivity.class

package com.likeit.note7;import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.util.Timer;
import java.util.TimerTask;public class MainActivity extends AppCompatActivity {private EditText e1;private Button b1;private TextView t1;private RecyclerView r1;private String result;private String[] str1;private GridLayoutManager gridLayoutManager;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);e1=(EditText)findViewById(R.id.e1);b1=(Button)findViewById(R.id.b1);t1=(TextView)findViewById(R.id.t1);r1=(RecyclerView)findViewById(R.id.r1);gridLayoutManager=new GridLayoutManager(MainActivity.this,5);r1.setLayoutManager(gridLayoutManager);//读取文件StringBuffer stringBuffer=new StringBuffer();try {InputStream inputStream=openFileInput("myfile");BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream));String cout=null;while((cout=bufferedReader.readLine())!=null){stringBuffer.append(cout);}//将获取到的文件内容写入数组str1=stringBuffer.toString().substring(0,stringBuffer.length()-1).split(",");Adapter adapter=new Adapter(str1,"0");r1.setAdapter(adapter);bufferedReader.close();}catch (Exception e){e.printStackTrace();}b1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {result = e1.getText().toString().trim();//判断搜索内容是否重复,重复则不写入try {for(int i=0;i<=str1.length;i++){if(result.equals(str1[i])){Intent intent=new Intent(MainActivity.this,MainActivity2.class);intent.putExtra("ss",result);startActivity(intent);return;}}}catch (Exception e){e.printStackTrace();}//将内容写入文件try {OutputStream outputStream = openFileOutput("myfile", MODE_APPEND);//判断搜索内容是否为空,为空则不写入if(result.equals("")){Intent intent=new Intent(MainActivity.this,MainActivity2.class);intent.putExtra("ss",result);startActivity(intent);return;}String str = result + ",";//用句号分隔开outputStream.write(str.getBytes());outputStream.flush();outputStream.close();Intent intent=new Intent(MainActivity.this,MainActivity2.class);intent.putExtra("ss",result);startActivity(intent);} catch (Exception e) {e.printStackTrace();}}});//删除文件t1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {boolean b=deleteFile("myfile");if(b==true){Toast.makeText(getApplicationContext(), "清除搜索记录成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(getApplicationContext(), "当前没有历史记录", Toast.LENGTH_SHORT).show();}Adapter adapter=new Adapter(str1,"1");r1.setAdapter(adapter);}});}
}

MainActivity2.class

package com.likeit.note7;import androidx.appcompat.app.AppCompatActivity;import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;public class MainActivity2 extends AppCompatActivity {
private TextView e1;
private String ss;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main2);e1=(TextView)findViewById(R.id.e1);Intent intent = getIntent();ss=intent.getStringExtra("ss");e1.setText(ss);e1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {Intent intent=new Intent(MainActivity2.this,MainActivity.class);startActivity(intent);}});}
}

三、源代码
点击下载


http://www.ppmy.cn/news/310764.html

相关文章

Android Studio入门之文本内容、大小、颜色的讲解及实战(附源码 超详细必看)

运行有问题或需要源码请点赞关注收藏后评论区留言或私信博主 一、设置文本的内容 1:在XML文件中通过属性android:text设置文本 <TextViewandroid:layout_width"wrap_content"android:layout_height"wrap_content"android:text"Hello World!"…

Compose (9/N) - 主题 Theme

一、Material Design 直接把任何Composable函数用 MaterialTheme{ } 包裹起来&#xff0c;就可以使用相关属性了。也可以单独将某个属性拿出来使用。 1.1 颜色 Color primary 主色&#xff0c;屏幕和元素都用这个颜色。 primaryVariant 用于区分主色&#xff0c;比如app bar和…

android 登录注册动画,Android开发(14)——动画实战:炫酷登录

本节内容 1.第三方库实现虚化 2.添加输入框和按钮 3.按钮状态 4.键盘隐藏 5.监听焦点改变的事件 6.手臂旋转动画 7.手掌和手臂动画 Demo简介 1.做一个炫酷登录的界面。 image.png 当我们输入密码的时候&#xff0c;猫头鹰会捂住眼睛。点击其他地方后&#xff0c;它的手臂又会张…

Android开发帮助技巧(适用于入门)二

Android配置配置清单&#xff0c;Gradle构建文件&#xff0c;外部化资源部分。 &#xff08;一)配置清单AndroidManifest.xml use-permission和uses-feature和application属于同等级节点。 android:allowBackup 是否允许应用参与备份和恢复基础架构。如果将此属性设为 fals…

【php + MySQL + Android】Android登陆界面设计(1)

前言 自己要做一个Android App&#xff0c;涉及到服务器端的用户数据管理&#xff0c;而网络上的的资源往往是只讲一个方面。所以自己每完成一段工作&#xff0c;会将开发过程记录下来&#xff0c;并供大家参考&#xff0c;相关文章放在专栏 【php MySQL Android】。 如果…

Jetpack Compose 初体验(上),retrofit原理面试

声明 Typography 对象&#xff0c;然后给 Text 添加 style 属性&#xff0c;来控制文字的样式。 Preview(showBackground true) Composable fun VerticalText() { val typography MaterialTheme.typography Column( modifier Modifier.padding(16.dp) ) { Image( painter …

scnu校赛去年题

求两个数的公约数有多少个 #include<iostream> #include<bits/stdc.h> using namespace std; int gcd(int a,int b){return b?gcd(b,a%b):a; } int main(){int T,a,b;cin>>T;while(T--){int ans1;cin>>a>>b;int ggcd(a,b);if(g1){cout<<…

STM32F10xx串口通信

一、通信方式相关 1.1 并行通信 1.2 串行通信 串行通信的通信方式&#xff1a; 常见的串行通信接口&#xff1a; STM32的串口通信接口 USART&#xff1a;通用异步收发器UART&#xff1a;通用同步异步收发器STM32F10x大容量系列芯片&#xff0c;包含3个USART&#xff08;支持异…