andorid 如何构建autofill service和AutofillHints和setImportantForAutofill相关使用:
16lz
2021-01-23
构建autofill service
对于在显示列表中的数据填充,需要app在继承AutofillService时,在重写onFillRequest()数据请求时进行数据装在载,这里指对onFillRequest做一些解释;
public class MyAutofillService extends AutofillService{.....@Overridepublic 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定义;
MainActivity.java定义
public class MainActivity extends Activity {@Overrideprotected 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;
android N 新增API Autofill
官方Demo;
https://github.com/googlesamples/android-AutofillFramework
https://download.csdn.net/download/xiao_yuanjl/10862116
更多相关文章
- Android 属性动画(Property Animation) 完全解析 (上)
- Android 中解析JSON形式的数据
- Android应用程序组件Content Provider的共享数据更新通知机制分
- Android SQLite数据库升级的问题
- 使用SharedPreferences存储和读取数据
- Android:Content Provider数据共享
- EditText属性详解