Example usage for android.text TextWatcher TextWatcher

List of usage examples for android.text TextWatcher TextWatcher

Introduction

In this page you can find the example usage for android.text TextWatcher TextWatcher.

Prototype

TextWatcher

Source Link

Usage

From source file:com.easemob.ui.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorItem = (RelativeLayout) getView().findViewById(R.id.rl_error_item);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = MyApplication.getInstance().getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter// www . j a va2 s .c  o  m
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(MyApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), "??", 0).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:org.csp.everyaware.fragments.FragmentWizardStep2.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    Log.d("FragmentWizardStep2", "onCreateView()");

    //inflating del layout del fragment
    ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_wizard_step2, container, false);

    mUserEt = (EditText) rootView.findViewById(R.id.usernameEt);
    mPswdEt = (EditText) rootView.findViewById(R.id.passwordEt);
    mOpResTv = (TextView) rootView.findViewById(R.id.operationResultTv);
    mActivateBtn = (Button) rootView.findViewById(R.id.activateAccountBtn);

    mActivateBtn.setOnClickListener(new OnClickListener() {
        @Override//from  www .  j av a2 s.  co  m
        public void onClick(View v) {
            if (!mUserMod || !mPswdMod) {
                Toast.makeText(getActivity().getApplicationContext(),
                        getResources().getString(R.string.fill_both_field_str), Toast.LENGTH_LONG).show();
                mOpResTv.setText("");
                return;
            }
            mActivateBtn.setEnabled(false);

            String[] credentials = new String[2];

            credentials[0] = mUserEt.getText().toString();
            credentials[1] = mPswdEt.getText().toString();

            Log.d("OnClickListener", "onClick()--> credentials: " + credentials[0] + " " + credentials[1]);

            //is internet connection available? 
            boolean[] connectivity = Utils.haveNetworkConnection(getActivity().getApplicationContext());
            boolean connectivityOn = connectivity[0] || connectivity[1];

            if (connectivityOn)
                new ActivateAccountTask().execute(credentials);
            else {
                mActivateBtn.setEnabled(true);
                Toast.makeText(getActivity().getApplicationContext(), R.string.alert_dialog_no_internet,
                        Toast.LENGTH_LONG).show();
            }
        }
    });

    mUserMod = false;

    mUserEt.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            if (mUserEt.getText().toString().length() > 0)
                mUserMod = true;
            else
                mUserMod = false;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    mPswdMod = false;

    mPswdEt.addTextChangedListener(new TextWatcher() {
        @Override
        public void afterTextChanged(Editable s) {
            if (mPswdEt.getText().toString().length() > 0)
                mPswdMod = true;
            else
                mPswdMod = false;
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }
    });

    return rootView;
}

From source file:app.CT.BTCCalculator.fragments.BreakevenFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Register Bus Provider instance.
    BusProvider.getInstance().register(this);

    View view = getView();/* ww  w.j  a va  2  s.  c  o m*/

    // Initialize text fields.
    assert view != null;
    btcBought = (EditText) view.findViewById(R.id.btcBought);
    btcBoughtPrice = (EditText) view.findViewById(R.id.btcBoughtPrice);
    btcSold = (EditText) view.findViewById(R.id.btcSold);
    btcSoldPrice = (EditText) view.findViewById(R.id.btcSoldPrice);
    optimalBTCPrice = (EditText) view.findViewById(R.id.optimalBTCPrice);
    optimalBTC = (EditText) view.findViewById(R.id.optimalBTC);
    resultText = (TextView) view.findViewById(R.id.resultText);

    Button buttonCalculate = (Button) view.findViewById(R.id.buttonCalculate);

    // Checks whether the first visible EditText element is focused in order to enable
    // and show the keyboard to the user. The corresponding XML element has android:imeOptions="actionNext".
    // All EditText elements below are now programmed to show keyboard when pressed.
    btcBought.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcBoughtPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcSold.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    btcSoldPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTCPrice.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTC.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showSoftKeyboard(v);
        }
    });

    optimalBTCPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            try {
                float result = ((Float.valueOf(btcSold.getText().toString()))
                        * Float.valueOf(btcSoldPrice.getText().toString()))
                        / Float.valueOf(optimalBTCPrice.getText().toString());
                optimalBTC.setText(String.valueOf(result));
            } catch (Exception ignored) {
            }

        }
    });

    btcBoughtPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            containsCurrentRate[0] = false;
        }
    });

    btcSoldPrice.addTextChangedListener(new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
        }

        @Override
        public void afterTextChanged(Editable s) {
            containsCurrentRate[1] = false;
        }
    });

    buttonCalculate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            float buyAmount, buyCost, sellAmount, sellPrice, optimalBTCCost, optimalBTCAmount, remainder,
                    totalCost, totalAmount, finalPrice;
            boolean didItWork = true;
            boolean validTransaction = true;

            // Error checking to prevent crashes.
            try {
                // Gets the input entered from the user.
                buyAmount = Float.valueOf(btcBought.getText().toString());
                buyCost = Float.valueOf(btcBoughtPrice.getText().toString());
                sellAmount = Float.valueOf(btcSold.getText().toString());
                sellPrice = Float.valueOf(btcSoldPrice.getText().toString());
                optimalBTCCost = Float.valueOf(optimalBTCPrice.getText().toString());
                optimalBTCAmount = Float.valueOf(optimalBTC.getText().toString());

                // Calculates remainder from the buying and selling.
                remainder = Math.abs((buyAmount - sellAmount));

                // User cannot sell more than they own.
                if (roundTwoDecimals(optimalBTCAmount * optimalBTCCost) > roundTwoDecimals(
                        sellAmount * sellPrice)) {
                    final android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(
                            getActivity());
                    alertDialog.setTitle("Error");
                    alertDialog.setMessage(getString(R.string.optimalBTCErrorMsg));
                    alertDialog.setCancelable(false);
                    alertDialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();
                    validTransaction = false;
                }

                // Checks if the user typed in a greater selling amount than buying.
                if (sellAmount > buyAmount) {
                    // Create new dialog popup.
                    final android.support.v7.app.AlertDialog.Builder alertDialog = new android.support.v7.app.AlertDialog.Builder(
                            getActivity());

                    alertDialog.setTitle("Error");
                    alertDialog.setMessage("You cannot sell more than you own.");
                    alertDialog.setCancelable(false);
                    alertDialog.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // If this button is clicked, close current dialog.
                            dialog.cancel();
                        }
                    });
                    alertDialog.show();

                    // Invalidates transaction since selling amount > buying amount.
                    validTransaction = false;
                }

                // Calculations to output.
                totalCost = buyAmount * buyCost;
                totalAmount = optimalBTCAmount + remainder;
                finalPrice = totalCost / totalAmount;

                if (validTransaction) {
                    resultText.setText(String.format(getString(R.string.resultText),
                            String.valueOf(totalAmount), String.valueOf(roundTwoDecimals(finalPrice))));
                }
            } catch (Exception e) {
                // Sets bool to false in order to execute "finally" block below.
                didItWork = false;
            } finally {
                if (!didItWork) {
                    // Creates new dialog popup.
                    final AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(getActivity());

                    alertDialog2.setTitle("Error");
                    alertDialog2.setMessage("Please fill in all fields.");
                    alertDialog2.setCancelable(false);
                    alertDialog2.setNeutralButton("Ok", new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            // If this button is clicked, close current dialog.
                            dialog.cancel();
                        }
                    });

                    // Show the dialog.
                    alertDialog2.show();
                }
            }

        }
    });
}

From source file:com.easemob.chatui.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = BeewayApplication.getInstance().getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter//from  w  w  w . j a v a2s.c  o  m
    listView.setAdapter(adapter);
    final String st = getResources().getString(R.string.Cant_chat_with_yourself);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(BeewayApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), st, Toast.LENGTH_SHORT).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:com.easemob.chatuidemo.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = DemoApplication.getInstance().getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter/*from  ww  w.ja  v a  2s  . c o m*/
    listView.setAdapter(adapter);
    final String st = getResources().getString(R.string.Cant_chat_with_yourself);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(DemoApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), st, 0).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:com.activiti.android.ui.fragments.form.picker.ActivitiUserPickerFragment.java

@Override
public void onStart() {
    super.onStart();

    searchForm = ((EditText) getDialog().getWindow().findViewById(R.id.search_query));
    if (searchForm.getText().length() == 0) {
        ((MaterialDialog) getDialog()).getActionButton(DialogAction.POSITIVE).setEnabled(false);
    }/*from ww w.j a  v a  2s  . c  om*/
    searchForm.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            if (s.length() == 0 || !s.toString().contains("@")) {
                ((MaterialDialog) getDialog()).getActionButton(DialogAction.POSITIVE).setEnabled(false);
            } else {
                ((MaterialDialog) getDialog()).getActionButton(DialogAction.POSITIVE).setEnabled(true);
            }
        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {
        }
    });
    searchContact = ((ImageButton) getDialog().getWindow().findViewById(R.id.search_contact));
    searchContact.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            pickContact();
        }
    });
}

From source file:cn.kangeqiu.kq.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorItem = (RelativeLayout) getView().findViewById(R.id.rl_error_item);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = BaseApplication.getInstance().getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter/*  w w w.  j  av  a  2s. c  om*/
    listView.setAdapter(adapter);
    final String st = getResources().getString(R.string.Cant_chat_with_yourself);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(BaseApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), st, 0).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:com.app.jiaxiaotong.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = ((MyHXSDKHelper) HXSDKHelper.getInstance()).getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter/*from  ww  w .j  ava 2  s . co  m*/
    listView.setAdapter(adapter);
    final String st = getResources().getString(R.string.Cant_chat_with_yourself);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(AppContext.getAppContext().getUserName()))
                Toast.makeText(getActivity(), st, Toast.LENGTH_SHORT).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:com.lnpdit.chatuidemo.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorItem = (RelativeLayout) getView().findViewById(R.id.rl_error_item);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = CydlApplication.getInstance().getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter//from   ww w  .j a  v  a2s .c o  m
    listView.setAdapter(adapter);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(CydlApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), "??", 0).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}

From source file:cn.ucai.foraging.activity.ChatHistoryFragment.java

@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
    inputMethodManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
    errorText = (TextView) errorItem.findViewById(R.id.tv_connect_errormsg);
    // contact list
    contactList = ((DemoHXSDKHelper) HXSDKHelper.getInstance()).getContactList();
    listView = (ListView) getView().findViewById(R.id.list);
    adapter = new ChatHistoryAdapter(getActivity(), 1, loadUsersWithRecentChat());
    // adapter/*  www.  j  a v a2 s .  c om*/
    listView.setAdapter(adapter);
    final String st = getResources().getString(R.string.Cant_chat_with_yourself);
    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            EMContact emContact = adapter.getItem(position);
            if (adapter.getItem(position).getUsername().equals(ForagingApplication.getInstance().getUserName()))
                Toast.makeText(getActivity(), st, 0).show();
            else {
                // ??
                Intent intent = new Intent(getActivity(), ChatActivity.class);
                if (emContact instanceof EMGroup) {
                    //it is group chat
                    intent.putExtra("chatType", ChatActivity.CHATTYPE_GROUP);
                    intent.putExtra("groupId", ((EMGroup) emContact).getGroupId());
                } else {
                    //it is single chat
                    intent.putExtra("userId", emContact.getUsername());
                }
                startActivity(intent);
            }
        }
    });
    // ??
    registerForContextMenu(listView);

    listView.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            // ??
            if (getActivity().getWindow()
                    .getAttributes().softInputMode != WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN) {
                if (getActivity().getCurrentFocus() != null)
                    inputMethodManager.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(),
                            InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    // ?
    query = (EditText) getView().findViewById(R.id.query);
    // ?button
    clearSearch = (ImageButton) getView().findViewById(R.id.search_clear);
    query.addTextChangedListener(new TextWatcher() {
        public void onTextChanged(CharSequence s, int start, int before, int count) {

            adapter.getFilter().filter(s);
            if (s.length() > 0) {
                clearSearch.setVisibility(View.VISIBLE);
            } else {
                clearSearch.setVisibility(View.INVISIBLE);
            }
        }

        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        public void afterTextChanged(Editable s) {
        }
    });
    clearSearch.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            query.getText().clear();

        }
    });

}