用安卓写搜索

少年恃险若平地,独倚长剑凌清秋。这篇文章主要讲述用安卓写搜索相关的知识,希望能为你提供帮助。
P.S.下图与最后的实现结果类似但不完全一样,这只是用processon画的UI图。

用安卓写搜索

文章图片
 
这个地方不仅仅是搜索框,下面还有一个热搜的列表框,可以进行筛选,里面可以自己自定义一个string数组或者ArrayList放在里面,不需要访问数据库或者接受服务端。
     
   
用安卓写搜索

文章图片

这里从搜索后展现所有学生的信息,可以点击list的item访问学生信息。
 
ActivitySearch:
package com.example.euphemiaxiao.activitysearch; import android.content.Context; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.text.TextUtils; import android.view.View; import android.view.inputmethod.InputMethodManager; import android.widget.AbsListView; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.SearchView; import org.json.JSONException; import org.json.JSONObject; import java.io.IOException; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; //import android.support.v7.widget.SearchView; public class ActivitySearch extends AppCompatActivity { //声明searchview和listview private SearchView assv; private ListView aslv; private ArrayAdapter< String> adapter; //这个是热搜,是固定数据,数据库中有但和数据库无关 private final String[] actStrings={"小橘灯","明信片设计大赛","游戏晚会","雷锋月"}; /*private static final long serialVersionUID =1L; //设定url路径,声明url String urlPath="http:\\\\"; URL url=new URL(urlPath); final JSONObject ClientKey = new JSONObject(); final JSONObject Authorization = new JSONObject(); */public ActivitySearch() throws MalformedURLException { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_search); aslv=(ListView)findViewById(R.id.aslv); adapter=new ArrayAdapter< String> (this,android.R.layout.simple_list_item_1,actStrings); aslv.setAdapter(adapter); //设置aslv可以被过滤 aslv.setTextFilterEnabled(true); //监听listview的滑动状态改变 aslv.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { InputMethodManager imm=(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); if(imm!=null) { //如果输入法是显示状态,那就隐藏输入法 imm.hideSoftInputFromInputMethod(aslv.getWindowToken(),0); } }@Override public void onScroll(AbsListView absListView, int i, int i1, int i2) {} }); //监听点击listview中单个item aslv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> adapterView, View view, int i, long l) { Bundle bundle = new Bundle(); //将该item内数据获取 bundle.putString("AcInfoData",actStrings[i]); Intent intent = new Intent(); intent.putExtras(bundle); //将获取数据传给ActivityInfo界面并进行跳转 intent.setClass(ActivitySearch.this,ActivityInfo.class); startActivity(intent); /* try { //使用httpPost的方式将ClientKey封装成JSON数据形式传递给服务器 //{"Activity":{"ActivityName":actStrings[i]}} //首先使用JsonObject封装{"ActivityName":actStrings[i]} ClientKey.put("ActivityName",actStrings[i]); //再使用JsonObject封装{"Activity":{"ActivityName":actStrings[i]}} Authorization.put("Activity",ClientKey); //将JSON数据转换成String类型使用输出流向服务器写 String content = String.valueOf(Authorization); //将封装好的数据传递过去,利用HttpURLConnection接口访问url HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); //设置允许输出 conn.setDoOutput(true); conn.setRequestMethod("POST"); //设置contentType conn.setRequestProperty("Content-Type","application/json"); OutputStream os = conn.getOutputStream(); os.write(content.getBytes()); os.close(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }*/ }); assv=(SearchView)findViewById(R.id.assv); //设置该searchview默认不自动缩小为图标 assv.setIconifiedByDefault(false); //searchview初始是可以点击输入的状态 assv.onActionViewExpanded(); //是否获得焦点 assv.setFocusable(false); //使失去焦点 assv.clearFocus(); //设置显示搜索按钮 assv.setSubmitButtonEnabled(true); //设置默认显示文字 assv.setQueryHint("输入活动名称"); assv.setOnQueryTextListener(new SearchView.OnQueryTextListener() { // private String TAG = getClass().getSimpleName(); //输入完成后,提交时触发的方法,一般情况是点击输入法中的搜索按钮才能触发。表示现在正式提交了 @Override public boolean onQueryTextSubmit(String query) { if(assv!=null) { //得到输入管理对象 InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); if (imm != null) { //这将让键盘在所有的情况下都被隐藏,但一般我们在点击搜索按钮后,输入法会乖乖自动隐藏 //输入法如果是显示状态,那么就隐藏输入法 imm.hideSoftInputFromInputMethod(assv.getWindowToken(), 0); } //不获取焦点 assv.clearFocus(); }//将输入数据传入ActivityInfo中 Intent intent = new Intent(ActivitySearch.this,ActivityInfo.class); intent.putExtra("AcInfoData",query); startActivity(intent); /*try { ClientKey.put("ActivityName", query); Authorization.put("Activity",ClientKey); String content = String.valueOf(Authorization); HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); //设置允许输出 conn.setDoOutput(true); conn.setRequestMethod("POST"); //设置contentType conn.setRequestProperty("Content-Type","application/json"); OutputStream os = conn.getOutputStream(); os.write(content.getBytes()); os.close(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/return true; }//在输入时触发的方法,当字符真正显示到searchView中触发 @Override public boolean onQueryTextChange(String newText) { if(TextUtils.isEmpty(newText)) { //清除listview的过滤 aslv.clearTextFilter(); } else { //使用用户输入的内容对listview的列表项 aslv.setFilterText(newText); } return true; } }); } }

 
  ActivityInfo:
package com.example.euphemiaxiao.activitysearch; import android.content.Intent; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListView; import android.widget.TextView; import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.ArrayList; public class ActivityInfo extends AppCompatActivity { //声明textview和listview private TextView aitv; private ListView ailv; //private final String[] sstrings={"2014211880 张三","2014211881 李四", //"2014211883 赵五","2014211884 王六"}; private ArrayAdapter< String> adapter; private ArrayList< String> studentInfo = new ArrayList< String> (); private ArrayList< String> studentID = new ArrayList< String> (); /*//声明url String urlPath="http:\\\\"; URL url=new URL(urlPath); */public ActivityInfo() throws MalformedURLException { } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_info); aitv=(TextView)findViewById(R.id.aitv); //从ActivitySearch中获取输入或点击的活动名称 final Intent intent = getIntent(); String AcInfoData = https://www.songbingjia.com/android/intent.getStringExtra("AcInfoData"); aitv.setText("活动:"+AcInfoData); /*studentInfo.add("2014211880 张三"); studentInfo.add("2014211881 李四"); studentInfo.add("2014211883 赵五"); studentInfo.add("2014211884 王六"); */ailv=(ListView)findViewById(R.id.ailv); /*try { //用HttpURLConnection接口访问url HttpURLConnection conn = (HttpURLConnection)url.openConnection(); //服务器返回的响应码 int code = conn.getResponseCode(); //当响应码为200时,表示服务器接收并处理了请求,将返回处理结果 if (code==200) { BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8")); String retData = https://www.songbingjia.com/android/null; String responseData =""; while((retData=https://www.songbingjia.com/android/in.readLine())!=null) { responseData+=retData; } //创建JSONObject对象,responseData是从服务器获得的Json数据 JSONObject jsonObject = new JSONObject(responseData); //获得JSONObject对象的值,该值是一个JSON数组,“student”是这个数据的键值 JSONArray actArray = jsonObject.getJSONArray("student"); for (int i = 0; i < actArray.length(); i++) { //获得JSON数组中的每一个JSONObject对象 JSONObject actObject = actArray.getJSONObject(i); String stuID=actObject.getString("studentID"); studentID.add(stuID); String stuName = actObject.getString("name"); //studentName.add(stuName); studentInfo.add(stuID+" "+stuName); } } } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/adapter = new ArrayAdapter< String> (this,android.R.layout.simple_list_item_1,studentInfo); ailv.setAdapter(adapter); ailv.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView< ?> adapterView, View view, int i, long l) { //将ArrayList转换成String数组 int size=studentInfo.size(); String[] studentInfoStrs=(String[])studentInfo.toArray(new String[size]); String str = studentInfoStrs[i]; String[] st = str.split(" "); Bundle bundle = new Bundle(); //获取点击的item的内容 bundle.putString("AcStudentInfo",st[0]); //将获取的内容传给下一个界面 Intent intent1=new Intent(); intent1.putExtras(bundle); //这里将学号传给下一个界面,别忘了改一下下一个界面的名称 intent1.setClass(ActivityInfo.this,test.class); startActivity(intent1); /* JSONObject ClientKey = new JSONObject(); JSONObject Authorization = new JSONObject(); try { //将ClientKey封装成JSON数据的形式传递给服务器 //使用JsonObject封装{"studentID":学号} ClientKey.put("studentID",st[0]); //使用JsonObject封装{"Student":{"studentID":studentIDStr[i]}} Authorization.put("Student",ClientKey); //把JSON数据转换成String类型实用输出流向服务器写 String content = String.valueOf(Authorization); //使用HttpURLConnection接口访问url HttpURLConnection conn = (HttpURLConnection)url.openConnection(); conn.setConnectTimeout(5000); //设置允许输出 conn.setDoOutput(true); conn.setRequestMethod("POST"); //设置contentType conn.setRequestProperty("Content-Type","application/json"); OutputStream os = conn.getOutputStream(); os.write(content.getBytes()); os.close(); } catch (JSONException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); }*/ } }); } }

里面用JSON数据的因为这部分接口是别人写的,代码编译没有问题,但不打保票逻辑一定没问题,所以打了注释,其他部分总体的硬界面的跳转显示是没有问题的。
【用安卓写搜索】 

    推荐阅读