diff options
Diffstat (limited to 'mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/LocationView.java')
-rw-r--r-- | mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/LocationView.java | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/LocationView.java b/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/LocationView.java new file mode 100644 index 0000000000..c09d80509d --- /dev/null +++ b/mobile/android/geckoview_example/src/main/java/org/mozilla/geckoview_example/LocationView.java @@ -0,0 +1,64 @@ +/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; indent-tabs-mode: nil; -*- + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.geckoview_example; + +import android.content.Context; +import android.view.KeyEvent; +import android.view.View; +import android.view.inputmethod.EditorInfo; +import android.widget.TextView; +import androidx.appcompat.widget.AppCompatEditText; + +public class LocationView extends AppCompatEditText { + + private CommitListener mCommitListener; + private FocusAndCommitListener mFocusCommitListener = new FocusAndCommitListener(); + + public interface CommitListener { + void onCommit(String text); + } + + public LocationView(Context context) { + super(context); + + this.setInputType(EditorInfo.TYPE_CLASS_TEXT | EditorInfo.TYPE_TEXT_VARIATION_URI); + this.setSingleLine(true); + this.setSelectAllOnFocus(true); + this.setHint(R.string.location_hint); + + setOnFocusChangeListener(mFocusCommitListener); + setOnEditorActionListener(mFocusCommitListener); + } + + public void setCommitListener(CommitListener listener) { + mCommitListener = listener; + } + + private class FocusAndCommitListener implements OnFocusChangeListener, OnEditorActionListener { + private String mInitialText; + private boolean mCommitted; + + @Override + public void onFocusChange(View view, boolean focused) { + if (focused) { + mInitialText = ((TextView) view).getText().toString(); + mCommitted = false; + } else if (!mCommitted) { + setText(mInitialText); + } + } + + @Override + public boolean onEditorAction(TextView textView, int i, KeyEvent keyEvent) { + if (mCommitListener != null) { + mCommitListener.onCommit(textView.getText().toString()); + } + + mCommitted = true; + return true; + } + } +} |