构建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.插入 布局并填写数据;
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. 定义;
<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 信用卡到期日
官方Demo;
https://github.com/googlesamples/android-Autofill work
继续阅读与本文标签相同的文章
-
有关厂商都在积极布局功率碳化硅
2026-05-18栏目: 教程
-
反向链接对网站权重有影响吗?
2026-05-18栏目: 教程
-
国内首创:海南台风灾害影响评估三维模拟系统投入试运行
2026-05-18栏目: 教程
-
大智能时代,需要什么样的产品经理
2026-05-18栏目: 教程
-
怎样才能让用户更喜欢你的APP应用
2026-05-18栏目: 教程
