简体   繁体   English

Android 重复使用布局时自动填充多次输入相同的信息

[英]Android autofill enters the same info multiple times, when reusing layout

I have a fragment in which I reuse the same layout multiple times.我有一个片段,我在其中多次重复使用相同的布局。 The layout has two EditText fields, one for name and one for email. In other words the view lets you put in multiple contacts at the same time, how many is optional.该布局有两个 EditText 字段,一个用于姓名,一个用于 email。换句话说,该视图允许您同时放入多个联系人,数量是可选的。 What happens is:发生的事情是:

  1. The user presses one of the fields, say the email field, and Android's built in autofill feature shows a suggestion.用户按下其中一个字段,例如 email 字段,Android 内置的自动填充功能会显示一条建议。
  2. User presses the option, a stored email address.用户按下选项,存储一个email地址。
  3. The email address is then entered multiple times by the autofill function, into each of the reused layouts.然后自动填充 function 将 email 地址多次输入到每个重复使用的布局中。 Say I have three views for contact information, then each of the three email fields will receive the autofilled email address, clearing anything that was before.假设我有三个联系信息视图,那么三个 email 字段中的每一个都将收到自动填充的 email 地址,清除之前的所有内容。

I want it to be entered only into the currently selected field, is there any way to control this?我希望它只输入到当前选定的字段中,有什么办法可以控制吗?

LinearLayout contactsListLayout;

for (Contact contact : contacts) {
          ContactView view = new ContactView(context, contact);
          contactsListLayout.addView(view);
      }

ContactView.java ContactView.java

public class ContactView extends RelativeLayout  {
...
     public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.contact_view);
     }

...
}

contact_view.xml contact_view.xml

<LinearLayout
     android:orientation="vertical"
     android:layout_width="match_parent"
     android:layout_height="wrap_content">

     <EditText
          android:id="@+id/nameEdit"
          android:inputType="textCapSentences"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:imeOptions="flagNoExtractUi|actionSearch"
          android:hint="Name"/>

     <EditText
          android:id="@+id/emailEdit" 
          android:inputType="textEmailAddress"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:hint="E-mail Address"/>

</LinearLayout>

I have multiple email input fields in a layout.我在布局中有多个 email 输入字段。 When one of the input fields gets focus, autofill options shows up.当其中一个输入字段获得焦点时,会显示自动填充选项。 And if we choose one of the options, autofill service pastes the same value to all input fields even if its focused or not.而且,如果我们选择其中一个选项,自动填充服务会将相同的值粘贴到所有输入字段,无论其是否有焦点。

I tried manipulating the importantForAutoFill flags of my views, to autoFill only the focused input field, but that didn't work.我尝试操纵我的视图的importantForAutoFill标志,以仅自动填充聚焦的输入字段,但这没有用。

So in my custom component, I've overridden the autoFill() method, to make it work only if the view is focused.因此,在我的自定义组件中,我覆盖了autoFill()方法,使其仅在视图获得焦点时才起作用。 If not, suppressed the autoFill action.如果不是,则取消自动填充操作。

override fun autofill(value: AutofillValue?) {
    if (hasFocus()) {
        super.autofill(value)
    }
}

Then the user fills the editText with his email address, you can store it in SQL database, after that user goes to the second view and there will be another field for email address. 然后,用户用他的电子邮件地址填充editText,您可以将其存储在SQL数据库中,然后该用户转到第二个视图,然后会有另一个用于电子邮件地址的字段。 So at that time your app gets String value(email address) that you inserted into your SQL database , and fills in the editText automatically. 因此,那时您的应用程序将获取您插入到SQL数据库中的String值(电子邮件地址),并自动填写editText。

Hopefully my answer helps you. 希望我的回答对您有所帮助。

Here are some links for setting up SQL database: 以下是一些用于设置SQL数据库的链接:

  1. Android SQLite Database with Examples Android SQLite数据库示例

  2. Android SQLite Database Tutorial Android SQLite数据库教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM