2016-05-14 84 views
2

後沒有出現我創建autoCompleteTextView如下Layout和Java類:的Android定製AutoCompleteTextView隱藏

<?xml version="1.0" encoding="utf-8"?> 
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/autoCompleteTextViewGroup" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:animateLayoutChanges="true" 
    android:minHeight="@dimen/component_height"> 

    <android.support.design.widget.TextInputLayout 
     android:id="@+id/textInputLayout" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_gravity="start" 
     android:animateLayoutChanges="true"> 

     <AutoCompleteTextView 
      android:id="@+id/autoCompleteTextView" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:completionThreshold="1" 
      android:singleLine="true" /> 

    </android.support.design.widget.TextInputLayout> 

    <android.support.v4.widget.ContentLoadingProgressBar 
     android:id="@+id/progressBar" 
     android:layout_width="24dp" 
     android:layout_height="24dp" 
     android:layout_gravity="center_vertical" 
     android:indeterminate="true" 
     android:indeterminateDrawable="@anim/anim_indeterminate_mini" 
     android:interpolator="@anim/anim_progress_interpolator" 
     android:visibility="visible" /> 
</FrameLayout> 

和Java類:

public class MAutoCompleteText extends LinearLayout { 
AutoCompleteTextView autoCompleteTextView; 
ContentLoadingProgressBar progressBar; 
TextInputLayout textInputLayout; 
ViewGroup autoCompleteTextViewGroup; 


public MAutoCompleteText(Context context) { 
    super(context); 
    init(context); 
} 

public MAutoCompleteText(Context context, AttributeSet attrs) { 
    super(context, attrs); 
    init(context); 
} 

public MAutoCompleteText(Context context, AttributeSet attrs, int defStyleAttr) { 
    super(context, attrs, defStyleAttr); 
    init(context); 
} 

@TargetApi(21) 
public MAutoCompleteText(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 
    super(context, attrs, defStyleAttr, defStyleRes); 
    init(context); 
} 

private void init(Context context) { 
    inflate(context, R.layout.layout_auto_complete_text, this); 
    autoCompleteTextViewGroup = (ViewGroup) findViewById(R.id.autoCompleteTextViewGroup); 
    textInputLayout = (TextInputLayout) findViewById(R.id.textInputLayout); 
    autoCompleteTextView = (AutoCompleteTextView) findViewById(R.id.autoCompleteTextView); 
    progressBar = (ContentLoadingProgressBar) findViewById(R.id.progressBar); 

} 

public void setAdapter(ArrayAdapter<String> adapter) { 
    autoCompleteTextView.setAdapter(adapter); 

} 

public String getText() { 
    return autoCompleteTextView.getText().toString(); 
} 

public void setErrorEnabled(boolean status) { 
    textInputLayout.setErrorEnabled(status); 
} 

public void setError(int errorId) { 
    textInputLayout.setError(getContext().getString(errorId)); 
} 

public int getVisibility() { 
    return autoCompleteTextViewGroup.getVisibility(); 
} 

public void setVisibility(int visibility) { 
    autoCompleteTextViewGroup.setVisibility(visibility); 
    autoCompleteTextViewGroup.setMinimumHeight(R.dimen.component_height); 
} 

public void setProgressBarVisibility(int visibility) { 
    progressBar.setVisibility(visibility); 
} 

public MAutoCompleteText setHint(int hintId) { 
    autoCompleteTextView.setHint(hintId); 
    return this; 
} 

public void setImeOption(int imeOption) { 
    autoCompleteTextView.setImeOptions(imeOption); 
} 

}

一切工作正常。我setVisibility(GONE)和我的組件隱藏UI, ,但是當我使用setVisibility(VISIBLE)不會發生任何事情!

如何以編程方式顯示我的組件?

回答

0

,我發現我的問題的答案,至少現在的工作... :) 我改變setVisibility下面的代碼:

public void setVisibility(int visibility) { 
    autoCompleteTextViewGroup.setVisibility(visibility); 
    if (visibility == View.VISIBLE) { 
     textInputLayout.setVisibility(visibility); 
     autoCompleteTextView.setVisibility(visibility); 
     progressBar.setVisibility(visibility); 
     requestLayout(); 
    } 

} 

事實上requestLayout();再次在UI上重新繪製我的組件的佈局,並且它顯示正確。