Android-使用代码创建AutoCompleteTextView(完全没有xml)
创始人
2024-08-19 21:30:07
0
  1. 先创建AutoCompleteTextView的实例:

AutoCompleteTextView autoCompleteTextView = new AutoCompleteTextView(context);

  1. 设置AutoCompleteTextView的布局参数:

autoCompleteTextView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

  1. 给AutoCompleteTextView设置适配器,这里使用了一个简单的字符串数组适配器:

String[] suggestions = {"Apple", "Banana", "Cherry", "Coconut"}; ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, suggestions); autoCompleteTextView.setAdapter(adapter);

  1. 最后将AutoCompleteTextView添加到父布局中:

parentLayout.addView(autoCompleteTextView);

完整示例代码:

LinearLayout parentLayout = findViewById(R.id.parent_layout);

AutoCompleteTextView autoCompleteTextView = new AutoCompleteTextView(context); autoCompleteTextView.setLayoutParams(new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));

String[] suggestions = {"Apple", "Banana", "Cherry", "Coconut"}; ArrayAdapter adapter = new ArrayAdapter(context, android.R.layout.simple_dropdown_item_1line, suggestions); autoCompleteTextView.setAdapter(adapter);

parentLayout.addView(autoCompleteTextView);

相关内容

热门资讯

德州辅助软件开发定制!德州智能... 德州辅助软件开发定制!德州智能辅助,德州ai辅助代理,安装教程(有挂技巧)1、很好的工具软件,可以解...
德州之星有外挂吗!德州扑克ai... 德州之星有外挂吗!德州扑克ai机器人,德州ai软件是什么样,扑克教程(有挂方法);德州之星有外挂吗软...
微扑克的辅助工具苹果!微扑克a... 1、微扑克的辅助工具苹果!微扑克ai操作,微扑克机制技巧,靠谱教程(有挂透视);详细教程。2、微扑克...
aapoker有胜率保护吗!a... aapoker有胜率保护吗!aa扑克辅助,aapoker线上规律,2025新版技巧(有挂方法),支持...
aapoker辅助器是真的吗!... aapoker辅助器是真的吗!aa扑克发牌机制,德州aa poker有外挂吗,新2025教程(有挂攻...
aapoker透明!德扑ai智... aapoker透明!德扑ai智能机器人代理,德州辅助软件线上,专业教程(竟然真的有挂)准备好在德扑a...
德扑之星作弊事件!德扑ai代打... 1、德扑之星作弊事件!德扑ai代打,德扑之星软件透明有人买,必备教程(有挂神器);详细教程。2、德扑...
wepoke一定有挂!wpk德... wepoke一定有挂!wpk德州ai辅助器,wpk俱乐部有外挂吗,黑科技教程(其实真的有挂);实战中...
aapoker辅助使用教程!a... aapoker辅助使用教程!aapoker辅助工具存在吗,aapoker下载教程,新2025版(有挂...
wpk透视辅助可测试真的假的!... wpk透视辅助可测试真的假的!wpk德州辅助,德州微扑克外挂是真的吗,规律教程(本然真的是有挂);支...