Code example for EditText

Methods: getText, getWindowToken

0
 
        mEditNameView.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            @Override 
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    mController.setUserStopName(mEditNameView.getText().toString());
                    endNameEdit(); 
                    return true; 
                } 
                return false; 
            } 
        }); 
 
        // "Cancel" 
        View cancel = mView.findViewById(R.id.edit_name_cancel);
        cancel.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                endNameEdit(); 
            } 
        }); 
 
        View clear = mView.findViewById(R.id.edit_name_revert);
        clear.setOnClickListener(new View.OnClickListener() {
            @Override 
            public void onClick(View v) {
                mController.setUserStopName(null);
                endNameEdit(); 
            } 
        }); 
        UIHelp.setChildClickable(mView, R.id.show_all, mShowAllClick);
    } 
 
    private final ClickableSpan mShowAllClick = new ClickableSpan() {
        public void onClick(View v) { 
            mController.setRoutesFilter(new ArrayList<String>()); 
            refreshFilter(); 
        } 
    }; 
 
    void refresh() { 
        refreshName(); 
        refreshDirection(); 
        refreshFavorite(); 
        refreshFilter(); 
        refreshError(); 
    } 
 
    private void refreshName() { 
        String name = mController.getStopName();
        String userName = mController.getUserStopName();
 
        if (!TextUtils.isEmpty(userName)) {
            mNameView.setText(userName);
        } else if (name != null) {
            mNameView.setText(name);
        } 
    } 
 
    private void refreshDirection() { 
        String direction = mController.getStopDirection();
        if (direction != null) {
            final int directionText = UIHelp.getStopDirectionText(direction);
            ((TextView)mDirectionView).setText(directionText);
            if (directionText != R.string.direction_none && !mInNameEdit) {
                mDirectionView.setVisibility(View.VISIBLE);
            } else { 
                mDirectionView.setVisibility(View.GONE);
            } 
        } 
    } 
 
    private void refreshFavorite() { 
        mFavoriteView.setImageResource(mController.isFavorite() ?
                R.drawable.focus_star_on : 
                R.drawable.focus_star_off); 
    } 
 
    private void refreshFilter() { 
        TextView v = (TextView)mView.findViewById(R.id.filter);
        ArrayList<String> routesFilter = mController.getRoutesFilter();
        final int num = (routesFilter != null) ? routesFilter.size() : 0;
        if (num > 0) {
            final int total = mController.getNumRoutes();
            v.setText(mContext.getString(R.string.stop_info_filter_header, num, total));
            // Show the filter text (except when in name edit mode) 
            mFilterGroup.setVisibility(mInNameEdit ? View.GONE : View.VISIBLE);
        } else { 
            mFilterGroup.setVisibility(View.GONE);
        } 
    } 
 
    private static class ResponseError implements AlertList.Alert { 
        private final CharSequence mString;
 
        ResponseError(CharSequence seq) {
            mString = seq;
        } 
 
        @Override 
        public String getId() {
            return "STATIC: RESPONSE ERROR"; 
        } 
 
        @Override 
        public int getType() { 
            return AlertList.Alert.TYPE_ERROR; 
        } 
 
        @Override 
        public int getFlags() { 
            return 0; 
        } 
 
        @Override 
        public CharSequence getString() {
            return mString;
        } 
 
        @Override 
        public void onClick() { 
        } 
 
        @Override 
        public int hashCode() { 
            return getId().hashCode();
        } 
 
        @Override 
        public boolean equals(Object obj) {
            if (this == obj)
                return true; 
            if (obj == null)
                return false; 
            if (getClass() != obj.getClass())
                return false; 
            ResponseError other = (ResponseError)obj;
            if (!getId().equals(other.getId()))
                return false; 
            return true; 
        } 
    } 
 
    private ResponseError mResponseError = null;
 
    private void refreshError() { 
        final long now = System.currentTimeMillis();
        final long responseTime = mController.getLastGoodResponseTime();
        AlertList alerts = mController.getAlertList();
 
        if (mResponseError != null) {
            alerts.remove(mResponseError);
        } 
 
        if ((responseTime) != 0 &&
                ((now - responseTime) >= 2 * DateUtils.MINUTE_IN_MILLIS)) {
            CharSequence relativeTime =
                DateUtils.getRelativeTimeSpanString(responseTime,
                        now,
                        DateUtils.MINUTE_IN_MILLIS,
                        0); 
            CharSequence s = mContext.getString(R.string.stop_info_old_data,
                                                relativeTime);
            mResponseError = new ResponseError(s);
            alerts.insert(mResponseError, 0);
        } 
    } 
 
    void beginNameEdit(String initial) {
        // If we can click on this, then we're definitely not 
        // editable, so we should go into edit mode. 
        mEditNameView.setText((initial != null) ? initial : mNameView.getText());
        mNameContainerView.setVisibility(View.GONE);
        mDirectionView.setVisibility(View.GONE);
        mFilterGroup.setVisibility(View.GONE);
        mEditNameContainerView.setVisibility(View.VISIBLE);
        mEditNameView.requestFocus();
        mInNameEdit = true;
        // TODO: Ensure the soft keyboard is up 
    } 
 
    void endNameEdit() { 
        mInNameEdit = false;
        mNameContainerView.setVisibility(View.VISIBLE);
        mEditNameContainerView.setVisibility(View.GONE);
        mDirectionView.setVisibility(View.VISIBLE);
        //setFilterHeader(); 
        InputMethodManager imm =
                (InputMethodManager)mContext.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(mEditNameView.getWindowToken(), 0);
        refreshName(); 
    } 
}