Google의 설명에 따라 오류 메시지를 표시하도록 Android EditText 설계
나는 필요합니다.EditText
오류:
대신 오류를 호출하면 다음과 같습니다.
참고: 앱이 SDK 19(4.4.2)에서 실행되고 있습니다.
최소 SDK는 1입니다.
이것을 자동으로 수행하는 setError와 유사한 방법이 있습니까, 아니면 코드를 작성해야 합니까?
감사해요.
Google이 소개한 이후로 타사 라이브러리를 사용할 필요가 없습니다.TextInputLayout
의 일환으로design-support-library
.
기본적인 예는 다음과 같습니다.
레이아웃
<android.support.design.widget.TextInputLayout
android:id="@+id/text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:errorEnabled="true">
<android.support.design.widget.TextInputEditText
android:id="@+id/edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter your name" />
</android.support.design.widget.TextInputLayout>
참고: 설정별app:errorEnabled="true"
의 특성으로서TextInputLayout
오류가 표시되면 크기가 변경되지 않으므로 기본적으로 공간을 차단합니다.
코드
아래의 오류를 표시하려면EditText
당신은 그저 전화만 하면 됩니다.#setError
에서TextInputLayout
(어린이에게는 해당되지 않음)EditText
):
TextInputLayout til = (TextInputLayout) findViewById(R.id.text_input_layout);
til.setError("You need to enter a name");
결과
오류를 숨기고 색조를 재설정하려면 간단히 전화하십시오.til.setError(null)
.
메모
를 사용하려면TextInputLayout
당신은 다음을 당신의 것에 추가해야 합니다.build.gradle
종속성:
dependencies {
compile 'com.android.support:design:25.1.0'
}
사용자 정의 색상 설정
기본적으로 라인은EditText
빨간색이 될 것입니다.다른 색상을 표시해야 할 경우 전화를 걸자마자 다음 코드를 사용할 수 있습니다.setError
.
editText.getBackground().setColorFilter(getResources().getColor(R.color.red_500_primary), PorterDuff.Mode.SRC_ATOP);
삭제하려면 간단히 전화를 걸어 주십시오.clearColorFilter
다음과 같은 기능:
editText.getBackground().clearColorFilter();
불러myTextInputLayout.setError()
대신에myEditText.setError()
.
이러한 컨테이너와 컨테이너는 설정 오류에 대해 이중 기능을 제공합니다.필요한 기능은 컨테이너의 기능입니다.하지만 그것을 위해서는 최소한의 23 버전이 필요할 수 있습니다.
당신의.EditText
에 싸여 있어야 합니다.TextInputLayout
<android.support.design.widget.TextInputLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/tilEmail">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="@+id/etEmail"
android:hint="Email"
android:layout_marginTop="10dp"
/>
</android.support.design.widget.TextInputLayout>
원하는 대로 오류 메시지를 받으려면 오류를 다음으로 설정합니다.TextInputLayout
TextInputLayout tilEmail = (TextInputLayout) findViewById(R.id.tilEmail);
if (error){
tilEmail.setError("Invalid email id");
}
설계 지원 라이브러리 종속성을 추가해야 합니다.Gradle 종속성에 이 행 추가
compile 'com.android.support:design:22.2.0'
리버스의 답변은 훌륭하지만 부동 오류 툴팁 같은 것을 제거하는 방법을 지적하지 않았습니다.
필요할 것입니다.edittext.setError(null)
그것을 제거하기 위해.
또한, 누군가가 지적했듯이, 당신은 필요하지 않습니다.TextInputLayout.setErrorEnabled(true)
레이아웃
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="@+id/edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter something" />
</android.support.design.widget.TextInputLayout>
코드
TextInputLayout til = (TextInputLayout) editText.getParent();
til.setError("Your input is not valid...");
editText.setError(null);
답변에 언급된 대로 구글의 디자인 라이브러리를 사용할 때 여전히 오류에 직면한 사람이 있다면, @h_k가 언급한 대로 사용하십시오. 이는 -입니다.
텍스트 입력 레이아웃에서 setError를 호출하는 대신 텍스트 편집 자체에서 setError를 사용할 수 있습니다.
TextInputLayout til = (TextInputLayout)editText.getParent();
til.setErrorEnabled(true);
til.setError("some error..");
private EditText edt_firstName;
private String firstName;
edt_firstName = findViewById(R.id.edt_firstName);
private void validateData() {
firstName = edt_firstName.getText().toString().trim();
if (!firstName.isEmpty(){
//here api call for ....
}else{
if (firstName.isEmpty()) {
edt_firstName.setError("Please Enter First Name");
edt_firstName.requestFocus();
}
}
}
언급URL : https://stackoverflow.com/questions/30953449/design-android-edittext-to-show-error-message-as-described-by-google
'programing' 카테고리의 다른 글
C#을 사용하여 Office-vba-macros에 디지털 서명 적용 (0) | 2023.08.13 |
---|---|
리눅스에서 파이프는 어떻게 작동합니까? (0) | 2023.08.13 |
PHP 서브스크립트가 문자가 아닌 단어로 끝나는지 확인 (0) | 2023.08.13 |
이 SQL 쿼리는 어떻게 동일한 id_product로 결과를 반환합니까? (0) | 2023.08.13 |
VBA에 범위가 있는지 테스트합니다. (0) | 2023.08.13 |