构建autofill service
对于在显示列表中的数据填充,需要app在继承AutofillService时,在重写onFillRequest()数据请求时进行数据装在载,这里指对onFillRequest做一些解释;
public class MyAutofillService extends AutofillService{
.....
@Override
public void onFillRequest(FillRequest request, CancellationSignal cancellationSignal,
FillCallback callback) {
//创建数据集;
FillResponse.Builder response = new FillResponse.Builder();
for (int i = 1; i <= NUMBER_DATASETS; i++) {
Dataset.Builder dataset = new Dataset.Builder();
//创建RemoteViews.插入xml布局并填写数据;
RemoteView presentation =new RemoteViews(getApplicationContext().
.packageName,R.layout.multidataset_service_list_item);
//给这个布局Item显示一个名字,remoteViewsText;
presentation.setTextViewText(R.id.text, text);
//把新的data添加到数据集;
response.addDataset(dataset.build());
}
//最后通知android系统,完成该服务
callback.onSuccess(response.build());
}
}
接下来我们来说一下关于EditText的一些相关使用和数据;
1.AutofillHints和setImportantForAutofill
关于Autofill 在view上的两个关键属性autofillHints和setImportantForAutofill,autofillHints:在点击自动填充后,显示提示列表,如果不做处理或不添加属性则不会出现提示列表;
setImportantForAutofill:这个属性主要是用于在打开带有EdiText或TextView等一些View时,采用此属性进行设置,是否自动弹出或手动弹出,
activity_layout.xml定义;
<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:autofillHints="password"
android:inputType="textPassword"
android:importantForAutofill="no"
android:inputType="text"/>
MainActivity.java定义
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText et=findViewById(R.id.username);
//点击自动填充弹出提示列表
et.setAutofillHints(View.AUTOFILL_HINT_PASSWORD);
//用来设置是否自动弹出提示列表
et.setImportantForAutofill(View.IMPORTANT_FOR_AUTOFILL_NO);
}
}
view.setAutofillHints() 方法来设置标记,相关值都是View类(api 26版本以上)中定义的一些String类型常量;
AUTOFILL_HINT_NAME 用户真名
AUTOFILL_HINT_PASSWORD 用户密码
AUTOFILL_HINT_PHONE 电话号码
AUTOFILL_HINT_EMAIL_ADDRESS 邮箱地址
AUTOFILL_HINT_POSTAL_CODE 邮寄编号
AUTOFILL_HINT_POSTAL_ADDRESS 邮寄地址
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY 信用卡到期日
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DATE 信用卡到期日期
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_MONTH 信用卡到期月
AUTOFILL_HINT_CREDIT_CARD_NUMBER 信用卡卡号
AUTOFILL_HINT_CREDIT_CARD_SECURITY_CODE 信用卡安全密码
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_YEAR 信用卡到期年
AUTOFILL_HINT_CREDIT_CARD_EXPIRATION_DAY 信用卡到期日
setImportantForAutofill()相关值都是View类(api 26版本以上);
//Automatically determine whether a view is important for autofill.
public static final int IMPORTANT_FOR_AUTOFILL_AUTO = 0x0;
//The view is important for autofill, but its children (if any) will not be traversed.
public static final int IMPORTANT_FOR_AUTOFILL_YES = 0x1;
//The view is not important for autofill, but its children (if any) will be traversed.
public static final int IMPORTANT_FOR_AUTOFILL_NO = 0x2;
//The view is important for autofill, but its children (if any) will not be traversed.
public static final int IMPORTANT_FOR_AUTOFILL_YES_EXCLUDE_DESCENDANTS = 0x4;
//The view is not important for autofill, and its children (if any) will not be traversed.
public static final int IMPORTANT_FOR_AUTOFILL_NO_EXCLUDE_DESCENDANTS = 0x8;
官方Demo;
https://github.com/googlesamples/android-AutofillFramework
https://download.csdn.net/download/xiao_yuanjl/10862116