android随笔25——搜索框输入内容后界面的隐藏逻辑

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" />
        RelativeLayout>
        

        <TextView
            android:id="@+id/ope_tv_title"
            style="@style/titleText"
            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" />
    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>

    <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" />

    <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"
                style="@style/home_tab_bottom"
                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"
                style="@style/home_tab_bottom"
                android:drawableTop="@drawable/home_personal_selector"
                android:text="@string/menu_geren"
                android:textColor="@drawable/text_select" />
        RadioGroup>
    LinearLayout>

RelativeLayout>

java文件

package com.xxx.xxx

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;
    }

}

你可能感兴趣的:(android随笔)