首页 文章

用户在编辑文本中输入值时的 Android 启动活动

提问于
浏览
2

我注意到谷歌地图和 lyft 这样的应用程序有一个 feature/behavior,当用户开始在文本字段中键入地址时,文本字段占用整个屏幕(开始一个新的活动),特别关注输入。当用户从列表视图中选择某些内容时,他们将使用所有其他信息返回到屏幕

我只是好奇这样的事情是如何实现的。他们使用不同的活动吗?

在此输入图像描述

我正在处理的应用程序具有类似的功能,但我想我不知道如何在用户开始输入时让我的文本字段覆盖整个屏幕(或者可能启动不同的活动)。

我只是在寻找一个指针或一个例子。不幸的是因为我不知道我正在寻找的确切名称,这使得在文档中搜索它有点困难。任何指针将不胜感激!

**PS:**我知道这种方法,但这不是我想要

<activity android:name=".MyActivity" android:windowSoftInputMode=""/>

2 回答

  • 0

    我实际上会做的是:我会将其创建为“简单”视图。不是片段,不是活动。

    • 我首先创建一个表示 TextView 的“扩展”版本的布局(我将区分视图的展开和折叠状态;展开是填充整个屏幕时的状态)。

    • 我会将它的宽度和高度设置为match_parent,我会将它放在活动(或片段或其他)中。例如,用align_parentTop锚定它。

    • 将主活动布局上的clipChildren设置为false

    • 在这个Activity onCreate里面(或其他一些类似的地方)
      一种容器),我会添加更像这样的东西:

    码:

    mExpandableTextView = (RelativeLayout)findViewById(R.id.expandable_text_view_layout);
        //layout representing the expandable textview (defined in expanded state)
    
    mExpandableTextView.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
        @Override
        public void onGlobalLayout() {
            int width = mExpandableTextView.getWidth();
            int height = mExpandableTextView.getHeight();
            if (width > 0 && height > 0) {
                mExpandableTextView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
    
                // setting the height and width of the view to fixed values (instead of match_parent)
                // this problably is not really necessary but I will write it just to be sure it works as intended
                RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)mExpandableTextView.getLayoutParams();
                params.height = height;
                params.width = width;
                mExpandableTextView.setLayoutParams(params);
    
                mMaxTranslationY = height - mCollapsedHeight;
                    //mCollapsedHeight is the height of the view in collapsed state
                    //provide it in most convenient way for you
    
                mExpandableTextView.setTranslationY(mMaxTranslationY);
                    //setting it to the collapsed state when activity is being displayed for the first time
            }
        }
    });
    

    (如果您的应用适用于设备< API16,请记得使用removeGlobalOnLayoutListener())

    • 然后,当处于折叠状态时,我会检测视图(布局)中唯一可见部分的点击,我会使用以下代码展开或折叠它:

    坍方:

    mExpandableTextView.animate().translationY(mMaxTranslationY); //to collapse with animation
    

    扩大:

    mExpandableTextView.animate().translationY(0); //to expand with animation
    
  • 0

    我最终做的一个更简单的方法是在文本字段激活后启动结果活动,并在新完成后设置值

相关问题