예제를 시작하기 전에 Android에서 TextInputLayout이 무엇인지 알아야 합니다. TextInputLayout은 선형 레이아웃으로 확장됩니다. 텍스트 편집을 위한 래퍼 역할을 하고 텍스트 편집을 위한 플랫 힌트 애니메이션을 보여줍니다.
이 예제는 Android TextInputLayout을 구현하는 방법을 보여줍니다.
1단계 − Android Studio에서 새 프로젝트를 생성하고 파일 ⇒ 새 프로젝트로 이동하여 필요한 모든 세부 정보를 입력하여 새 프로젝트를 생성합니다.
2단계 − res/layout/activity_main.xml에 다음 코드를 추가합니다.
<?xml version = "1.0" encoding = "utf-8"?> <LinearLayout xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity" android:background = "#dde4dd" android:orientation = "vertical"> <android.support.design.widget.TextInputLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/layoutEmail" android:layout_marginTop = "8dp" android:layout_marginStart = "8dp" android:layout_marginEnd = "8dp" style = "@style/Widget.MaterialComponents.TextInputLayout.FilledBox"> <android.support.design.widget.TextInputEditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/email" android:hint = "Enter Email id" android:inputType = "textEmailAddress"/> </android.support.design.widget.TextInputLayout> <android.support.design.widget.TextInputLayout android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/layoutPassword" android:layout_marginTop = "8dp" android:layout_marginStart = "8dp" android:layout_marginEnd = "8dp" style = "@style/Widget.MaterialComponents.TextInputLayout.FilledBox"> <android.support.design.widget.TextInputEditText android:layout_width = "match_parent" android:layout_height = "wrap_content" android:id = "@+id/password" android:hint = "Password" android:inputType = "textPassword"/> </android.support.design.widget.TextInputLayout> <Button android:id = "@+id/click" android:layout_width = "wrap_content" android:layout_height = "wrap_content" android:layout_gravity = "center" android:text = "Click"></Button> </LinearLayout>
위의 코드에서 우리는 두 개의 TextInputEditText와 하나의 버튼을 제공했습니다. 버튼을 클릭하면 편집 텍스트에서 데이터를 가져와 토스트에 표시합니다.
3단계 − src/MainActivity.java
에 다음 코드 추가package com.example.andy.myapplication;
import android.graphics.Point;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.TextureView;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
EditText email,password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
email = findViewById(R.id.email);
password = findViewById(R.id.password);
Button click = findViewById(R.id.click);
click.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(!email.getText().toString().isEmpty()&&(!password.getText().toString().isEmpty())) {
Toast.makeText(MainActivity.this, "you have entered email id " + email.getText().toString() + "Password " + password.getText().toString(), Toast.LENGTH_LONG).show();
} else {
email.setError("Please Enter Email id");
password.setError("Please Enter Pass word");
}
}
});
}
} 4단계 − build.gradle을 열고 디자인 지원 라이브러리 종속성을 추가합니다.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.myapplication"
minSdkVersion 15
targetSdkVersion 28
compileSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
} 응용 프로그램을 실행해 보겠습니다. 실제 Android 모바일 장치를 컴퓨터에 연결했다고 가정합니다. Android 스튜디오에서 앱을 실행하려면 프로젝트의 활동 파일 중 하나를 열고 도구 모음에서 실행 아이콘을 클릭합니다. 모바일 장치를 옵션으로 선택한 다음 기본 화면을 표시할 모바일 장치를 확인하십시오 -

위의 결과는 초기 화면입니다. 이제 데이터를 입력하고 아래와 같이 버튼을 클릭합니다. -

이제 모든 edittext 데이터를 제거하고 버튼을 클릭하십시오. 아래와 같이 결과가 표시됩니다 -
