当前位置: 代码迷 >> 综合 >> android AutoCompleteTextView 的 setAdapter方法的坑! 设置进去不显示! 都是泪啊
  详细解决方案

android AutoCompleteTextView 的 setAdapter方法的坑! 设置进去不显示! 都是泪啊

热度:85   发布时间:2023-12-12 20:47:55.0

AutoCompleteTextView 就是输入账号密码可以自动补全的那个功能,android 给你封装好了,

网上看教程之后

就是setAdapter 不进去

auto.setAdapter(adapter);Log.d(TAG, "setAdapter");auto.setDropDownHeight(1500);auto.setThreshold(1);auto.setCompletionHint("最近的5条记录");auto.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View view, boolean b) {if(hasWindowFocus()){((AutoCompleteTextView) view).showDropDown();Log.d(TAG, "onFocusChange: showdown");}}});

里面有一个 setDropDownHeight(1500),

坑就在这里了,我已开始设置了 70 结果啥也没有!!!,因为 这个 70 是px 为单位的 不是 dp 。。。。

一开始想想70dp 总够大了吧。。

所以下来的框太窄显示不出什么的。。。

 

另外存储使用了sharedPerference。

下面贴源代码:

xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"tools:context=".ThirdActivity"android:orientation="vertical"><LinearLayoutandroid:layout_width="match_parent"android:layout_height="wrap_content"><ImageViewandroid:layout_width="60dp"android:layout_height="60dp"android:background="@drawable/cat"/><LinearLayout android:layout_width="0px"android:layout_height="0px" android:focusable="true"android:focusableInTouchMode="true"></LinearLayout><!--上一个layout 用于取出焦点--><LinearLayoutandroid:layout_width="0dp"android:layout_height="wrap_content"android:layout_weight="1"android:orientation="vertical"><AutoCompleteTextViewandroid:hint="请输入账号"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/auto1"/><AutoCompleteTextViewandroid:hint="请输入密码"android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/auto2"/></LinearLayout></LinearLayout><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center_horizontal"android:id="@+id/conform"android:text="登录"/>
</LinearLayout>

java

 

package com.example.expriment12;import android.content.SharedPreferences;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.AutoCompleteTextView;
import android.widget.Button;
import android.widget.Toast;public class ThirdActivity extends AppCompatActivity {private Button conform;private AutoCompleteTextView auto1,auto2;private ArrayAdapter<String> adapter;private SharedPreferences.Editor editer;private String TAG = "weimeng";@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_third);conform = (Button) findViewById(R.id.conform);auto1 = (AutoCompleteTextView) findViewById(R.id.auto1);auto2 = (AutoCompleteTextView) findViewById(R.id.auto2);editer = getSharedPreferences("data",MODE_PRIVATE).edit();initAutoComplete("history1",auto1);initAutoComplete("history2",auto2);conform.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View view) {saveHistory("history1",auto1);saveHistory("history2",auto2);Toast.makeText(ThirdActivity.this, "保存了上述账号和密码已经完成了任务,不用求登陆", Toast.LENGTH_SHORT).show();}});}private void initAutoComplete(String field,AutoCompleteTextView auto) {SharedPreferences pref = getSharedPreferences("data", MODE_PRIVATE);String longHistory = pref.getString(field, "nothing");Log.d(TAG, "initAutoComplete: +longHistory"+longHistory);String[] hisArrays = longHistory.split(",");Log.d(TAG, "hisArrays length="+hisArrays.length);adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, hisArrays);//只保留最近的50条记录if (hisArrays.length > 50) {String[] newArray = new String[50];System.arraycopy(hisArrays, 0, newArray, 0, 50);adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,newArray);}auto.setAdapter(adapter);Log.d(TAG, "setAdapter");auto.setDropDownHeight(1500);auto.setThreshold(1);auto.setCompletionHint("最近的5条记录");auto.setOnFocusChangeListener(new View.OnFocusChangeListener() {@Overridepublic void onFocusChange(View view, boolean b) {if(hasWindowFocus()){((AutoCompleteTextView) view).showDropDown();Log.d(TAG, "onFocusChange: showdown");}}});}//放入 pref 的 field 字段中private void saveHistory(String field,AutoCompleteTextView auto) {String text = auto.getText().toString();SharedPreferences sp = getSharedPreferences("data", MODE_PRIVATE);String longHistory = sp.getString(field, "nothing");Log.d(TAG, "saveHistory: "+longHistory);//避免重复if (!longHistory.contains(text + ",")) {StringBuilder sb = new StringBuilder(longHistory);sb.insert(0, text + ",");editer.putString(field, sb.toString()).apply();}}}

效果图:

以为一点别的地方就没了,所以没法手机截屏。。

电脑前置摄像头拍了一下:是镜像。。。模糊了点不过效果是有的

  相关解决方案