Android
Android Studio EditText.getText().toString().trim() / 유효성체크 / 로그처리 / Toast, Snackbar
leopard4
2023. 1. 26. 16:17
0
package com.leopard4.uitest1;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.snackbar.Snackbar;
import com.google.android.material.textfield.TextInputEditText;
public class MainActivity extends AppCompatActivity {
TextView txtName;
Button btnSave;
EditText editName;
EditText editPass;
EditText editEmail;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 위코드로, 화면이 연결되었으니
// 화면에 있는 뷰를 가져와서! 변수로 만든다.
txtName = findViewById(R.id.txtName);
btnSave = findViewById(R.id.btnSave);
editName = findViewById(R.id.editName);
editPass = findViewById(R.id.editPass);
editEmail = findViewById(R.id.editEmail);
txtName.setText("홍길동");
// 저장버튼이 눌렸을때
btnSave.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// 1. 입력한 이름을 가져와서 텍스트뷰에 보여준다.
String name = editName.getText().toString().trim();
String pass = editPass.getText().toString().trim();
String email = editEmail.getText().toString().trim();
// 1-1. 데이터가 유효한지 체크한다.
// .equals("") 또는 .isEmpty()
if (name.isEmpty() || pass.isEmpty() || email.isEmpty() ) {
Toast.makeText(getApplicationContext(), "제대로 입력하세요", Toast.LENGTH_SHORT).show();
return;
}
txtName.setText("안녕하세요" +" " + name);
// 2. 입력한 비밀번호와 이메일은 로그로 출력!(프린트하는방법)
Log.i("UITEST_MAIN", pass + " " + email);
// 3. 토스트 Toast 메시지 표시하는 방법
Toast.makeText(getApplicationContext(), "유저가 입력한 이름은 "+ name, Toast.LENGTH_SHORT).show();
// 4. 스낵바 (SnackBar) 로 메시지 표시하는 방법
Snackbar.make(btnSave ,"유저가 입력한 이름은 "+name, Snackbar.LENGTH_SHORT).show();
}
});
}
}
ems는 길이
디자인 코드 전체
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity">
<TextView
android:id="@+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:background="#34a2f5"
android:paddingLeft="20dp"
android:paddingTop="15dp"
android:paddingRight="20dp"
android:paddingBottom="15dp"
android:text="안녕하세요"
android:textColor="#ffffff"
android:textSize="30sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="@+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="66dp"
android:layout_marginTop="22dp"
android:layout_marginEnd="67dp"
android:ems="10"
android:hint="이름입력"
android:inputType="text"
android:textSize="24sp"
app:layout_constraintBottom_toTopOf="@+id/editPass"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtName" />
<EditText
android:id="@+id/editPass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="66dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="67dp"
android:ems="10"
android:hint="비밀번호입력"
android:inputType="textPassword"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editName" />
<EditText
android:id="@+id/editEmail"
android:layout_width="295dp"
android:layout_height="50dp"
android:layout_marginStart="67dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="40dp"
android:ems="10"
android:hint="이메일입력"
android:inputType="textEmailAddress"
android:textSize="26sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editPass" />
<Button
android:id="@+id/btnSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="14dp"
android:layout_marginRight="20dp"
android:backgroundTint="#BC170B"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:text="저 장"
android:textSize="26sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editEmail" />
</androidx.constraintlayout.widget.ConstraintLayout>