Android : 텍스트가 입력되지 않은 경우 AutoCompleteTextView에 제안 표시
AutoCompleteTextView
내가 사용하고 user-가 클릭 할, 나는 그것이 텍스트가없는 경우에도 제안 을 표시 할 때 , - setThreshold(0)
정확하게 작동하는 동일 setThreshold(1)
- 사용자가 제안을 표시하는 하나의 문자를 입력해야합니다 .
이 문서화 된 행동입니다 .
경우
threshold
미만 또는 0 일, 1 임계 값을 적용한다.
을 통해 드롭 다운을 수동으로 표시 showDropDown()
할 수 있으므로 원할 때 표시 할 수 있습니다. 또는 AutoCompleteTextView
서브 클래스 및 오버라이드 enoughToFilter()
를 true
항상 반환 합니다.
여기 내 클래스 InstantAutoComplete 입니다. AutoCompleteTextView
와 사이 에있는 항목 Spinner
입니다.
import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.AutoCompleteTextView;
public class InstantAutoComplete extends AutoCompleteTextView {
public InstantAutoComplete(Context context) {
super(context);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1) {
super(arg0, arg1);
}
public InstantAutoComplete(Context arg0, AttributeSet arg1, int arg2) {
super(arg0, arg1, arg2);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused && getAdapter() != null) {
performFiltering(getText(), 0);
}
}
}
다음과 같이 XML에서 사용하십시오.
<your.namespace.InstantAutoComplete ... />
가장 쉬운 방법 :
setOnTouchListener와 showDropDown ()을 사용하십시오.
AutoCompleteTextView text;
.....
.....
text.setOnTouchListener(new View.OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event){
text.showDropDown();
return false;
}
});
Destil의 코드는 InstantAutoComplete
객체 가 하나만있을 때 작동 합니다. 그래도 두 가지 작동하지 않을 수 있습니다. 이유는 좋아요. 그러나 showDropDown()
(CommonsWare가 권고 한대로) 다음 onFocusChanged()
과 같이 입력하면 :
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
showDropDown();
}
}
문제를 해결했습니다.
그것은 결합 된 두 가지 대답이지만 누군가 시간을 절약 할 수 있기 때문입니다.
어댑터는 처음에 필터링을 수행하지 않습니다.
필터링을 수행하지 않는 드롭 다운 목록이 비어 있습니다.
필터링을 초기에 수행해야 할 수도 있습니다.
이를 위해 filter()
항목 추가를 완료 한 후 호출 할 수 있습니다 .
adapter.add("a1");
adapter.add("a2");
adapter.add("a3");
adapter.getFilter().filter(null);
위의 Destil의 대답은 거의 작동하지만 미묘한 버그가 있습니다. 사용자가 처음으로 작동하지만 필드를 떠났다가 다시 돌아 오면 mPopupCanBeUpdated의 값이 숨겨 졌을 때 계속해서 다운 드롭을 표시하지 않습니다. onFocusChanged 메소드를 다음과 같이 변경하는 것입니다.
@Override
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
if (getText().toString().length() == 0) {
// We want to trigger the drop down, replace the text.
setText("");
}
}
}
onFocusChangeListener를 사용할 수 있습니다.
TCKimlikNo.setOnFocusChangeListener(new OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
TCKimlikNo.showDropDown();
}
}
});
CustomAutoCompleteTextView를 만들려면 1. setThreshold, enoughToFilter, onFocusChanged 메소드를 대체하십시오.
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
private int myThreshold;
public CustomAutoCompleteTextView (Context context) {
super(context);
}
public CustomAutoCompleteTextView (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public CustomAutoCompleteTextView (Context context, AttributeSet attrs) {
super(context, attrs);
}
//set threshold 0.
public void setThreshold(int threshold) {
if (threshold < 0) {
threshold = 0;
}
myThreshold = threshold;
}
//if threshold is 0 than return true
public boolean enoughToFilter() {
return true;
}
//invoke on focus
protected void onFocusChanged(boolean focused, int direction,
Rect previouslyFocusedRect) {
//skip space and backspace
super.performFiltering("", 67);
// TODO Auto-generated method stub
super.onFocusChanged(focused, direction, previouslyFocusedRect);
}
protected void performFiltering(CharSequence text, int keyCode) {
// TODO Auto-generated method stub
super.performFiltering(text, keyCode);
}
public int getThreshold() {
return myThreshold;
}
}
autoCompleteTextView의 터치 또는 클릭 이벤트 또는 원하는 위치 에서이 메소드를 호출하십시오.
autoCompleteTextView.showDropDown()
시도 해봐
searchAutoComplete.setThreshold(0);
searchAutoComplete.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {//cut last probel
if (charSequence.length() > 1) {
if (charSequence.charAt(charSequence.length() - 1) == ' ') {
searchAutoComplete.setText(charSequence.subSequence(0, charSequence.length() - 1));
searchAutoComplete.setSelection(charSequence.length() - 1);
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
//when clicked in autocomplete text view
@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.header_search_etv:
if (searchAutoComplete.getText().toString().length() == 0) {
searchAutoComplete.setText(" ");
}
break;
}
}):
이 의사 코드에서 나를 위해 일했습니다.
public class CustomAutoCompleteTextView extends AutoCompleteTextView {
public CustomAutoCompleteTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean enoughToFilter() {
return true;
}
@Override
protected void onFocusChanged(boolean focused, int direction, Rect previouslyFocusedRect) {
super.onFocusChanged(focused, direction, previouslyFocusedRect);
if (focused) {
performFiltering(getText(), 0);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
this.showDropDown();
return super.onTouchEvent(event);
}
}
Java의 onCreate 메소드에 제안십시오.
final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(
this, android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.Loc_names));
textView1 =(AutoCompleteTextView) findViewById(R.id.acT1);
textView1.setAdapter(arrayAdapter);
textView1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View arg0) {
textView1.setMaxLines(5);
textView1.showDropDown();
}
});
그리고 이것은 Xml 파일에 ...
<AutoCompleteTextView
android:layout_width="200dp"
android:layout_height="30dp"
android:hint="@string/select_location"
android:id="@+id/acT1"
android:textAlignment="center"/>
그리고 값 ... 아래에 string.xml에 Array를 만듭니다.
<string-array name="Loc_names">
<item>Pakistan</item>
<item>Germany</item>
<item>Russia/NCR</item>
<item>China</item>
<item>India</item>
<item>Sweden</item>
<item>Australia</item>
</string-array>
그리고 당신은 갈 수 있습니다.
7 년 후에도 문제는 동일하게 유지됩니다. 여기 어떤 조건에서 멍청한 팝업이 나타나도록하는 함수가있는 클래스가 있습니다. AutoCompleteTextView에 어댑터를 설정하고 데이터를 추가하고 showDropdownNow()
함수를 호출하기 만하면됩니다.
@David Vávra에 대한 신용. 그의 코드를 기반으로합니다.
import android.content.Context
import android.util.AttributeSet
import android.widget.AutoCompleteTextView
class InstantAutoCompleteTextView : AutoCompleteTextView {
constructor(context: Context) : super(context)
constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context, attrs, defStyleAttr)
override fun enoughToFilter(): Boolean {
return true
}
fun showDropdownNow() {
if (adapter != null) {
// Remember a current text
val savedText = text
// Set empty text and perform filtering. As the result we restore all items inside of
// a filter's internal item collection.
setText(null, true)
// Set back the saved text and DO NOT perform filtering. As the result of these steps
// we have a text shown in UI, and what is more important we have items not filtered
setText(savedText, false)
// Move cursor to the end of a text
setSelection(text.length)
// Now we can show a dropdown with full list of options not filtered by displayed text
performFiltering(null, 0)
}
}
}
'IT' 카테고리의 다른 글
System.Web.Http v5.0.0.0으로 업그레이드하기위한 NuGet 패키지는 어디에 있습니까? (0) | 2020.07.16 |
---|---|
Bootstrap 3 Mobile의 메뉴 / Navbar에서 슬라이드 (0) | 2020.07.16 |
AppDelegate.m의 화면에 현재 표시되는 UIViewController를 가져옵니다. (0) | 2020.07.15 |
일반 배열의 요소 제거 (0) | 2020.07.15 |
R에서 당신을위한 제안 제안 (0) | 2020.07.15 |