Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android隨筆25——搜索框輸入內容後界面的隱藏邏輯

android隨筆25——搜索框輸入內容後界面的隱藏邏輯

編輯:關於Android編程

xml文件

<code class=" hljs xml"><relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/white" tools:context=".Operate">

    <relativelayout android:id="@+id/opeate_title" android:layout_width="match_parent" android:layout_height="@dimen/title_height" android:layout_gravity="center_vertical|center_horizontal" android:background="@drawable/top" android:gravity="center_vertical|center_vertical" android:orientation="horizontal">

        <relativelayout android:id="@+id/ope_back" android:layout_width="50dp" android:layout_height="fill_parent" android:clickable="true" android:gravity="center">

            <imageview android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/back">
        </imageview></relativelayout>


        <textview android:id="@+id/ope_tv_title" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:text="@string/operate_title">
        </textview>
    </relativelayout>

    <relativelayout android:id="@+id/operate_lay_search" android:layout_width="match_parent" android:layout_height="@dimen/title_height" android:layout_below="@id/opeate_title" android:focusable="true" android:focusableintouchmode="true" android:orientation="horizontal">

        <edittext android:id="@+id/operate_edit_search" android:layout_width="fill_parent" android:layout_height="35dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:background="@drawable/bg_edit_search" android:drawableleft="@drawable/sy_search" android:hint="食堂名稱" android:paddingleft="10dp">
    </edittext></relativelayout>

    <view android:id="@+id/line1" android:layout_width="match_parent" android:layout_height="@dimen/line_height" android:layout_below="@id/operate_lay_search" android:background="@color/line">

    <linearlayout android:id="@+id/ope_lay_listcontent_search" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@id/line1" android:orientation="vertical" android:visibility="gone">

        <listview android:id="@+id/ope_lv_content_search" android:layout_width="match_parent" android:layout_height="wrap_content">
        </listview>
    </linearlayout>

    <linearlayout android:id="@+id/ope_lay_listcontent" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_above="@+id/operate_btn_more2" android:layout_below="@id/line1" android:orientation="vertical">

        <listview android:id="@+id/ope_lv_content" android:layout_width="match_parent" android:layout_height="wrap_content">
        </listview>
    </linearlayout>

    </view></relativelayout></code><button android:id="@+id/operate_btn_more2" android:layout_width="match_parent" android:layout_height="40dp" android:layout_alignparentbottom="true" android:layout_gravity="center" android:layout_marginbottom="60dip" android:layout_marginleft="10dp" android:layout_marginright="10dp" android:background="@drawable/login_title" android:text="點擊加載更多" android:textcolor="@color/white"><code class=" hljs xml">

    <linearlayout android:id="@+id/operate_lay_home" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_gravity="center" android:gravity="bottom" android:orientation="vertical">

        <radiogroup android:id="@+id/home_radio_group" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="bottom" android:gravity="center_vertical" android:orientation="horizontal">

            <radiobutton android:id="@+id/home_tab_main" android:checked="true" android:drawabletop="@drawable/home_main_selector" android:text="@string/menu_shouye" android:textcolor="@drawable/text_select">

            <radiobutton android:id="@+id/home_tab_personal" android:drawabletop="@drawable/home_personal_selector" android:text="@string/menu_geren" android:textcolor="@drawable/text_select">
        </radiobutton></radiobutton></radiogroup>
    </linearlayout>

</code></button>

java文件

package com.gexun.sunmess_hg;

import java.util.ArrayList;
import java.util.List;

import org.kymjs.aframe.http.KJHttp;
import org.kymjs.aframe.http.KJStringParams;
import org.kymjs.aframe.http.StringCallBack;
import org.kymjs.aframe.ui.BindView;
import org.kymjs.aframe.ui.activity.BaseActivity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.gexun.sunmess_hg.common.CommonDate;
import com.gexun.sunmess_hg.common.HttpUrl;
import com.gexun.sunmess_hg.common.MyConstace;
import com.gexun.sunmess_hg.dataobject.SchoolDto;
import com.gexun.sunmess_hg.dataobject.SchoolMsgDto;
import com.gexun.sunmess_hg.widget.CustomProgressDialog;

/**
 * 主頁面點擊按鈕1 2 之後跳轉到該頁面
 * 運轉類
 */
public class Operate extends BaseActivity {

    // 地點列表
//  @BindView(id = R.id.ope_lv_content )
    private ListView opeListV;
    @BindView(id = R.id.ope_lv_content_search, click = true)
    private ListView opeSearchListV;
    @BindView(id = R.id.ope_back, click = true)
    private View layback;
    @BindView(id = R.id.operate_lay_home, click = true)
    private View home;
    @BindView(id = R.id.operate_btn_more2, click = true)
    private View operate_btn_more;
    @BindView(id = R.id.operate_edit_search)
    private EditText search;
    //搜索按鈕
    @BindView(id = R.id.operate_edit_search, click = true)
    private View searchBtn;


    @BindView(id = R.id.ope_lay_listcontent_search)
    private View searchContent;

    @BindView(id = R.id.ope_lay_listcontent)
    private View schoolContent;

    //底部導航欄
    @BindView(id = R.id.home_tab_main , click = true)
    private RadioButton home_tab_main;
    @BindView(id = R.id.home_tab_personal , click =true)
    private RadioButton home_tab_personal;



//  @BindView(id = R.id.operate_swiperefresh, click = true)
//  private android.support.v4.widget.SwipeRefreshLayout swip;

//  @BindView(id = R.id.operate_swiperefresh_search)
//  private android.support.v4.widget.SwipeRefreshLayout searchSwip;

    private List schoolDtoList = new ArrayList();
    private List schoolDtoAllList = new ArrayList();

    private List schoolSearchDtoList = new ArrayList();
    private List schoolSearchDtoAllList = new ArrayList();

    private com.gexun.sunmess_hg.adapter.ListOperateAdapter ListOperateAdapter;
    private com.gexun.sunmess_hg.adapter.ListOperateSearchAdapter ListOperateSearchAdapter;
    private CustomProgressDialog dialog;

    private int pageNum = 1;// 分頁,初始頁為1
    private String destination ;//跳轉目的地標志:食物界面,監控界面
    private Intent intent;

    @Override
    public void setRootView() {
        // TODO Auto-generated method stub
        requestWindowFeature(Window.FEATURE_NO_TITLE);//設置無標題
        setContentView(R.layout.operate);//引入布局
        setBackListener(false);
        dialog = CustomProgressDialog.createDialog(Operate.this, "正在加載");//設置dialog顯示內容
        pageNum = 1;//設置顯示頁
        schoolDtoAllList.clear();//清空list內容
        destination = getIntent().getStringExtra("destination");//得到intent攜帶的描述信息
    }

    @Override
    protected void initWidget() {
        // TODO Auto-generated method stub
        super.initWidget();
//      search.setOnClickListener(new View.OnClickListener() {
//          
//          @Override
//          public void onClick(View v) {
//              // TODO Auto-generated method stub
//              Log.i("zdj","search-onclick");
//              searchContent.setVisibility(View.VISIBLE);
//              schoolContent.setVisibility(View.GONE);
//              home.setVisibility(View.GONE);
//              operate_btn_more.setVisibility(View.GONE);  
//          }
//      });
//      search.setOnFocusChangeListener(new OnFocusChangeListener() {
//          
//          @Override
//          public void onFocusChange(View v, boolean hasFocus) {
//              // TODO Auto-generated method stub
//              Log.i("zdj","search-onFocusChange");
//              searchContent.setVisibility(View.VISIBLE);
//              schoolContent.setVisibility(View.GONE);
//              home.setVisibility(View.GONE);
//              operate_btn_more.setVisibility(View.GONE);  
//          }
//      });
        // initData();

        // 頂部刷新的樣式
//      swip.setColorScheme(android.R.color.holo_red_light,
//              android.R.color.holo_green_light,
//              android.R.color.holo_blue_bright,
//              android.R.color.holo_orange_light);
//      swip.setOnRefreshListener(new OnRefreshListener() {
//
//          @Override
//          public void onRefresh() {
//              // TODO Auto-generated method stub
//
//              pageNum++;
//              initData();
//              swip.setRefreshing(false);
//
//          }
//      });


//===================================================================
        **// 給搜索框添加事件 當輸入框中有用戶輸入的內容 後 立刻隱藏其它界面
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int arg1, int arg2,
                    int arg3) {
//              Log.i("zdj","搜索框數值改變:"+s);
                if (s.length() > 0) {
                    searchContent.setVisibility(View.VISIBLE);
                    schoolContent.setVisibility(View.GONE);
                    home.setVisibility(View.GONE);
                    operate_btn_more.setVisibility(View.GONE);
                    //獲取搜索結果
                    initFuzzySearchData(s.toString());

                } else {
                    searchContent.setVisibility(View.GONE);
                    schoolContent.setVisibility(View.VISIBLE);
                    home.setVisibility(View.VISIBLE);
                    operate_btn_more.setVisibility(View.VISIBLE);
                }
            }
            @Override
            public void beforeTextChanged(CharSequence arg0, int arg1,
                    int arg2, int arg3) {
                // TODO Auto-generated method stub
            }
            @Override
            public void afterTextChanged(Editable arg0) {
            }
        });
    }**

//===================================================================
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();

    }

    @Override
    public void widgetClick(View v) {
        // TODO Auto-generated method stub
        super.widgetClick(v);
        switch (v.getId()) {
        case R.id.ope_back://標題欄返回按鈕
            intent = new Intent(Operate.this, HomeActivity.class);
            startActivity(intent);
            finish();
            break;
        case R.id.home_tab_main://底部導航返回主頁面按鈕
            intent = new Intent(Operate.this, HomeActivity.class);
            startActivity(intent);
            finish();
            break;
        case R.id.home_tab_personal://底部導航欄跳轉個人中心頁面按鈕
            intent = new Intent(Operate.this, PersonalActivity2.class);
            startActivity(intent);
            finish();
            break;
        case R.id.operate_btn_more2://加載更多按鈕
            pageNum++;
            initData();

            break;
        case R.id.operate_edit_search://搜索框
            search.setText("");

            break;          

        default:
            break;
        }

    }

    // 初始化經營點數據
    public void initData() {
        dialog.show();//顯示dialog
        SharedPreferences sp = getSharedPreferences(MyConstace.SP_NAME,0);
        String schoolName = sp.getString("SCHOOL_NAME","1");
        String refectoryName = sp.getString("refectoryName", "杭高中(錢江分校)");

        System.err.println("===================="+schoolName);

        KJHttp http = new KJHttp();
        KJStringParams params = new KJStringParams();
        // 設置參數
        params.put("limit", CommonDate.limit + "");// 每頁加載數量
        params.put("page", pageNum + "");// 表示加載第幾頁數據
//      params.put("frefectoryName", schoolName+"");
        params.put("frefectoryName", refectoryName+"");
//      params.put("frefectoryName", "杭高中(錢江校區)");
        Log.e("aaa", "請求參數:" + params.toString());
        // params.put("frefectoryName", "--");//模糊查詢字段
        // 發送請求
        http.urlGet(HttpUrl.schoolInfo, params, new StringCallBack() {
            @Override
            // 請求成功的回調函數
            public void onSuccess(String str) {
//              Log.i("zdj", "站點數據結果:" + str);
                System.err.println("=================="+str);
                SchoolMsgDto schoolMsgDto = JSON.parseObject(str,
                        SchoolMsgDto.class);

                schoolDtoList = JSON.parseArray(schoolMsgDto.getItems()
                        .toString(), SchoolDto.class);//解析Json
                if (schoolDtoList != null) {
                    for (int i = 0; i < schoolDtoList.size(); i++) {
                        schoolDtoAllList.add(schoolDtoList.get(i));
                    }
                }

                opeListV = (ListView) findViewById(R.id.ope_lv_content);
                ListOperateAdapter = new com.gexun.sunmess_hg.adapter.ListOperateAdapter(
                        Operate.this, schoolDtoAllList,destination);


                opeListV.setAdapter(ListOperateAdapter);
                // dialog.cancel();
                dialog.cancel();
            }

            // 請求失敗的回調函數
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(Operate.this, "網絡連接失敗", 3000).show();
                dialog.cancel();
            }
        });
    }

    /**
     * 獲取模糊搜索數據
     * 
     * @param searchContent
     *            模糊搜索內容
     */

    public void initFuzzySearchData(String searchContent) {
//      dialog.show();
        //清空數據
        schoolSearchDtoAllList.clear();
        schoolSearchDtoList.clear();
        KJHttp http = new KJHttp();
        KJStringParams params = new KJStringParams();
        params.put("limit",  "0");// 每頁加載數量
        params.put("page",   "0");// 表示加載第幾頁數據
        // 設置參數
        params.put("frefectoryName", searchContent);// 模糊查詢字段
        Log.e("zdj", "模糊請求參數:" + params.toString());

        // 發送請求
        http.urlGet(HttpUrl.schoolInfo, params, new StringCallBack() {
            @Override
            // 請求成功的回調函數
            public void onSuccess(String str) {
//              Log.i("zdj", "模糊搜索數據結果:" + str);
                SchoolMsgDto schoolMsgDto = JSON.parseObject(str,
                        SchoolMsgDto.class);

                schoolSearchDtoList = JSON.parseArray(schoolMsgDto.getItems()
                        .toString(), SchoolDto.class);
                if (schoolSearchDtoList != null) {
                    for (int i = 0; i < schoolSearchDtoList.size(); i++) {
                        schoolSearchDtoAllList.add(schoolSearchDtoList.get(i));
                    }
                }

                opeSearchListV = (ListView) findViewById(R.id.ope_lv_content_search);
                ListOperateSearchAdapter = new com.gexun.sunmess_hg.adapter.ListOperateSearchAdapter(
                        Operate.this, schoolSearchDtoAllList);
                opeSearchListV.setAdapter(ListOperateSearchAdapter);
                // dialog.cancel();
            }

            // 請求失敗的回調函數
            @Override
            public void onFailure(Throwable t, int errorNo, String strMsg) {
                super.onFailure(t, errorNo, strMsg);
                Toast.makeText(Operate.this, "網絡連接失敗", 3000).show();
//              dialog.cancel();
            }
        });

    }
    //監聽返回鍵
    public boolean onKeyDown(int keyCode, KeyEvent event) {

        if (keyCode == KeyEvent.KEYCODE_BACK
                 && event.getRepeatCount() == 0) {
            //do something...
            intent = new Intent(Operate.this, HomeActivity.class);
            startActivity(intent);
            finish();
             return true;//此處設置為false 就會屏蔽返回鍵功能
         }
         return super.onKeyDown(keyCode, event);
     }
    @Override
    /** 初始化菜單的方法 */
    public boolean onCreateOptionsMenu(Menu menu) {
        // TODO Auto-generated method stub
        getMenuInflater().inflate(R.menu.activity_menu1, menu);
        return true;
    }
    @Override
    /** 菜單條目選中的方法 執行相應的操作   */
    public boolean onOptionsItemSelected(MenuItem item) {
        // TODO Auto-generated method stub
        switch (item.getItemId()) {

        case R.id.menu_next:
            //跳轉到下一頁
            Intent i = new Intent(this,GxCemare2.class);
            startActivity(i);
            finish();
            break;

        default:
            break;
        }
        return true;
    }

}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved