android - 在文本输入布局android之外单击时如何将 float 标签文本重置为其原始位置

标签 android android-layout material-design android-support-library androiddesignsupport

您好,我在布局中使用 float TextView 。当我单击编辑文本并且不输入任何内容或删除编辑文本内的所有内容然后单击该 View 外部时,我的 float 标签仍保持在升高的位置。我想重置它的位置,因为它有提示显示并且没有插入文本。那么如何实现呢?提前致谢。

我正在使用支持设计库来显示 float 标签。

我正在关注 this float 标签教程。

enter image description here

最佳答案

您可以在您的 Activity 中添加一些代码,使编辑文本在点击外部时失去焦点。 这是一个例子:

public static void setupForKeyboardDismiss(View view, final Activity activity) {

        //Set up touch listener for non-text box views to hide keyboard.
        if(!(view instanceof EditText)) {

            view.setOnTouchListener(new View.OnTouchListener() {

                public boolean onTouch(View v, MotionEvent event) {
                    hideSoftKeyboard(activity);
                    return false;
                }

            });
        }

        //If a layout container, iterate over children and seed recursion.
        if (view instanceof ViewGroup) {

            for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {

                View innerView = ((ViewGroup) view).getChildAt(i);

                setupForKeyboardDismiss(innerView, activity);
            }
        }
    }
    public static void hideSoftKeyboard(Activity activity) {
        try {
            InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
        }catch (Exception e){
            e.printStackTrace();
        }
    }

然后您可以调用 setupForKeyboardDismiss 方法传递任何包含这些编辑文本的 View ,实际上您可以通过调用它(在您的 Activity 的 setContentView 之后)将它应用于所有 Activity :

setupForKeyboardDismiss((ViewGroup) activity.findViewById(android.R.id.content), activity);

关于android - 在文本输入布局android之外单击时如何将 float 标签文本重置为其原始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43248814/

相关文章:

android - Recyclerview 和 Detailsfragment : elevation, 层次结构之间的共享元素转换,bringToFront

android - 计算一段时间内行驶距离的最佳方法是什么?

android - 如何在 Android 中为单击的图像按钮制作边框?

android - 如何添加不确定的进度条

android - SlidingTabLayout 选项卡之间的通信

java - 为什么我的对象不保存到持久存储?

java - Android - 从 URL 解析 JSON 明文返回 null 但连接正常

java - android CardView 替代方案

android - 如何获取对 ViewPager 中 fragment View 的引用?

android - 如何确保 CoordinatorLayout 子元素不重叠