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);

相关内容

热门资讯

如何分辨真伪!来玩德州app(... 如何分辨真伪!来玩德州app(wepOkE)外挂透明挂辅助软件(透视)2024新版(有挂分享)-哔哩...
玩家攻略!微扑克专用(WEpo... 玩家攻略!微扑克专用(WEpoke)外挂透明挂辅助机制(辅助挂)透明挂教程(有挂头条)-哔哩哔哩科技...
发现一款!微扑克机器人(Wep... 发现一款!微扑克机器人(Wepoke)外挂透明挂辅助插件(透视)辅助教程(有挂细节)-哔哩哔哩;支持...
透明挂透视!weopke真的有... 透明挂透视!weopke真的有挂(WEPOKE)外挂透明挂辅助器(透视)总结教程(有挂教学)-哔哩哔...
透视模拟器!WPK玄学(WEP... 透视模拟器!WPK玄学(WEPOke)外挂透明挂辅助软件(辅助挂)总结教程(有人有挂)-哔哩哔哩;W...
总算了解!wpk机制(WepO... 1、总算了解!wpk机制(WepOke)外挂透明挂辅助神器(辅助挂)黑科技教程(有挂功能)-哔哩哔哩...
必备教程!德扑之星隐藏(wep... 此外,数据分析德州(德扑之星隐藏)辅助神器app还具备辅助透视行为开挂功能,通过对客户德扑之星隐藏透...
推荐一款!wpk下载(wepo... 推荐一款!wpk下载(wepoKE)外挂透明挂辅助插件(辅助挂)可靠技巧(有挂神器)-哔哩哔哩;wp...
每日必看!德州ai智能(wep... 每日必看!德州ai智能(wepoker)外挂透明挂辅助机制(辅助挂)科技教程(有挂规律)-哔哩哔哩;...
透明有挂!pokermaste... WePoke高级策略深度解析‌;透明有挂!pokermastersteam外挂(WePoke)外挂透...