반응형
이제 거의 다 만들어진 거 같다.
이번에 실패한 화면과 성공 화면을 만들어 주었다.
각각 xml 파일을 보여주겠다.
activity_fail
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView2"
android:layout_width="match_parent"
android:layout_height="157dp"
android:gravity="center"
android:text="실패"
android:textColor="@color/red"
android:textSize="100dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="다시 도전 하시겠습니까?"
android:gravity="center"
android:textSize="35dp"
android:textStyle="bold"
/>
<Button
android:id="@+id/fail_retry_btn"
android:layout_width="273dp"
android:layout_height="98dp"
android:text="다시하기"
android:textSize="50dp"
android:layout_gravity="center"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
© 2022 GitHub, Inc.
Terms
Privacy
Security
St
acitvity_success
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/textView3"
android:layout_width="match_parent"
android:layout_height="157dp"
android:text="성공"
android:textSize="100dp"
android:textStyle="bold"
android:textColor="@color/green"
android:gravity="center"
/>
<TextView
android:id="@+id/textView4"
android:layout_width="match_parent"
android:layout_height="100dp"
android:text="다시 시작 하겠습니까?"
android:textStyle="bold"
android:textSize="35dp"
android:gravity="center"
/>
<Button
android:id="@+id/suc_retry_btn"
android:layout_width="273dp"
android:layout_height="98dp"
android:text="다시하기"
android:layout_gravity="center"
android:textSize="50dp"
/>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
그러면 추가된 Activity코드를 보자.
FailActivity.java
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class FailActivity extends AppCompatActivity {
BackKey back = new BackKey(this);
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_fail);
MainActivity.count = 1;
Button retrybtn = findViewById(R.id.fail_retry_btn);
retrybtn.setOnClickListener((view) -> {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
});
}
@Override
public void onBackPressed() {
back.onBackPressed();
}
}
SuccessActivity.java
import android.annotation.SuppressLint;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.widget.Button;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class SuccessActivity extends AppCompatActivity {
BackKey back = new BackKey(this);
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_success);
MainActivity.count = 1;
Button retrybtn = findViewById(R.id.suc_retry_btn);
retrybtn.setOnClickListener((view -> {
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}));
}
@Override
public void onBackPressed() {
back.onBackPressed();
}
}
그리고 저번에 올린 뒤로 가기 2번 입력도 추가했다.
BackKey.java
import android.app.Activity;
import android.widget.Toast;
public class BackKey {
private long BackKeyPressedTime = 0;
private Toast toast;
private final Activity activity;
public BackKey(Activity activity) {
this.activity = activity;
}
public void showMessage(){
toast = Toast.makeText(activity,"\"뒤로가기\" 버튼을 한번 더 누르시면 종료 됩니다.",Toast.LENGTH_SHORT);
toast.show();
}
public void onBackPressed(){
if(System.currentTimeMillis() <= BackKeyPressedTime + 2000){
activity.finish();
toast.cancel();
}
if(System.currentTimeMillis() >= BackKeyPressedTime + 2000){
showMessage();
BackKeyPressedTime = System.currentTimeMillis();
}
}
}
그리고 MainActivity가 좀 바뀌었다.
MainActivity.java
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.os.Vibrator;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
public class MainActivity extends AppCompatActivity {
public String word = "truck";
public static int count = 1;
@SuppressLint("SourceLockedOrientationActivity")
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_main);
EditText e = findViewById(R.id.Enter);
TextView Empty1 = findViewById(R.id.Empty1);
TextView Empty2 = findViewById(R.id.Empty2);
TextView Empty3 = findViewById(R.id.Empty3);
TextView Empty4 = findViewById(R.id.Empty4);
TextView Empty5 = findViewById(R.id.Empty5);
LinearLayout linearLayout = findViewById(R.id.linear2);
LinearLayout linearLayout1 = findViewById(R.id.linear3);
LinearLayout linearLayout2 = findViewById(R.id.linear4);
LinearLayout linearLayout3 = findViewById(R.id.linear5);
e.setOnKeyListener((view, i, keyEvent) -> {
if (keyEvent.getAction() == KeyEvent.ACTION_DOWN) // 두번 입력 방지
return true;
if (i == KeyEvent.KEYCODE_ENTER) {
String Text = e.getText().toString();
int size = Text.length();
if (size != 5) {
Viverater();
Toast t = Toast.makeText(getApplicationContext(), "5글자만 입력 해 주십시오.", Toast.LENGTH_LONG);
t.show();
e.setText("");
} else if(Text.matches(".*[0-9].*")){
Viverater();
Toast t = Toast.makeText(getApplicationContext(), "영단어만 입력 해 주십시오.", Toast.LENGTH_LONG);
t.show();
e.setText("");
}
else {
char[] answerchar = Text.toCharArray();
char[] Ochar = word.toCharArray();
if (count == 1) {
e.setText("");
if(Text.equals(word)) ChangeSuccessActivity();
//gray
for (int j = 0; j < 5; j++) {
String s = String.valueOf(answerchar[j]);
if (!word.contains(s)) {
if (j == 0) {
Empty1.setBackgroundColor(Color.parseColor("#808080"));
Empty1.setText(s);
} else if (j == 1) {
Empty2.setBackgroundColor(Color.parseColor("#808080"));
Empty2.setText(s);
} else if (j == 2) {
Empty3.setBackgroundColor(Color.parseColor("#808080"));
Empty3.setText(s);
} else if (j == 3) {
Empty4.setBackgroundColor(Color.parseColor("#808080"));
Empty4.setText(s);
} else {
Empty5.setBackgroundColor(Color.parseColor("#808080"));
Empty5.setText(s);
}
}
}
//yellow
for (int j = 0; j < 5; j++) {
if (answerchar[0] == Ochar[j] || answerchar[0] - 'A' == Ochar[j]) {
Empty1.setBackgroundColor(Color.parseColor("#FFFF00"));
String s = String.valueOf(answerchar[0]);
Empty1.setText(s);
}
if (answerchar[1] == Ochar[j] || answerchar[1] - 'A' == Ochar[j]) {
Empty2.setBackgroundColor(Color.parseColor("#FFFF00"));
String s = String.valueOf(answerchar[1]);
Empty2.setText(s);
}
if (answerchar[2] == Ochar[j] || answerchar[2] - 'A' == Ochar[j]) {
Empty3.setBackgroundColor(Color.parseColor("#FFFF00"));
String s = String.valueOf(answerchar[2]);
Empty3.setText(s);
}
if (answerchar[3] == Ochar[j] || answerchar[3] - 'A' == Ochar[j]) {
Empty4.setBackgroundColor(Color.parseColor("#FFFF00"));
String s = String.valueOf(answerchar[3]);
Empty4.setText(s);
}
if (answerchar[4] == Ochar[j] || answerchar[4] - 'A' == Ochar[j]) {
Empty5.setBackgroundColor(Color.parseColor("#FFFF00"));
String s = String.valueOf(answerchar[4]);
Empty5.setText(s);
}
}
//Green
if (answerchar[0] == Ochar[0] || answerchar[0] - 'A' == Ochar[0]) {
Empty1.setBackgroundColor(Color.parseColor("#7CFC00"));
String s = String.valueOf(answerchar[0]);
Empty1.setText(s);
}
if (answerchar[1] == Ochar[1] || answerchar[1] - 'A' == Ochar[1]) {
Empty2.setBackgroundColor(Color.parseColor("#7CFC00"));
String s = String.valueOf(answerchar[1]);
Empty2.setText(s);
}
if (answerchar[2] == Ochar[2] || answerchar[2] - 'A' == Ochar[2]) {
Empty3.setBackgroundColor(Color.parseColor("#7CFC00"));
String s = String.valueOf(answerchar[2]);
Empty3.setText(s);
}
if (answerchar[3] == Ochar[3] || answerchar[3] - 'A' == Ochar[3]) {
Empty4.setBackgroundColor(Color.parseColor("#7CFC00"));
String s = String.valueOf(answerchar[3]);
Empty4.setText(s);
}
if (answerchar[4] == Ochar[4] || answerchar[4] - 'A' == Ochar[4]) {
Empty5.setBackgroundColor(Color.parseColor("#7CFC00"));
String s = String.valueOf(answerchar[4]);
Empty5.setText(s);
}
count++;
} else if (count == 2) {
e.setText("");
if(Text.equals(word)) ChangeSuccessActivity();
for (int j = 0; j < 5; j++) {
String s = String.valueOf(answerchar[j]);
TextView textView = MakeTextView(s, 71, 300, Ochar, j);
linearLayout.addView(textView);
}
count++;
} else if (count == 3) {
e.setText("");
if(Text.equals(word)) ChangeSuccessActivity();
for (int j = 0; j < 5; j++) {
String s = String.valueOf(answerchar[j]);
TextView textView = MakeTextView(s, 71, 60, Ochar, j);
linearLayout1.addView(textView);
}
count++;
} else if (count == 4) {
e.setText("");
if(Text.equals(word)) ChangeSuccessActivity();
for (int j = 0; j < 5; j++) {
String s = String.valueOf(answerchar[j]);
TextView textView = MakeTextView(s, 71, 60, Ochar, j);
linearLayout2.addView(textView);
}
count++;
} else if (count == 5) {
e.setText("");
if(Text.equals(word)) ChangeSuccessActivity();
for (int j = 0; j < 5; j++) {
String s = String.valueOf(answerchar[j]);
TextView textView = MakeTextView(s, 71, 60, Ochar, j);
linearLayout3.addView(textView);
}
count++;
} else if (count == 6) {
e.setText("");
if (Text.equals(word)) ChangeSuccessActivity();
else {
Intent intent = new Intent(getApplicationContext(),FailActivity.class);
startActivity(intent);
finish();
}
}
}
}
return true;
});
}
public void Viverater(){
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
vibrator.vibrate(1000);
}
public TextView MakeTextView(String Text,int leftmargin, int topmargin, char[] Oe,int texttrans){
TextView t1 = new TextView(getApplicationContext());
t1.setText(Text);
t1.setWidth(130);
t1.setHeight(130);
t1.setGravity(Gravity.CENTER_VERTICAL| Gravity.CENTER_HORIZONTAL);
t1.setTextSize(30);
t1.setTypeface(null, Typeface.BOLD);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(leftmargin,topmargin,0,0);
t1.setLayoutParams(params);
t1.setBackgroundResource(R.drawable.background);
ChangeColorgray(t1,Text);
ChangeColoryellow(t1,Text,Oe);
ChangeColorgreen(t1,Text,Oe,texttrans);
return t1;
}
public void ChangeColorgray(TextView textView, String Text){
if (!word.contains(Text)){
textView.setBackgroundColor(Color.parseColor("#808080"));
}
}
public void ChangeColoryellow(TextView textView, String text, char[] Oe){
char T = text.charAt(0);
if (word.contains(text)) {
for (int j = 0; j < 5; j++)
if (T != Oe[j]) {
textView.setBackgroundColor(Color.parseColor("#FFFF00"));
return;
}
}
}
public void ChangeColorgreen(TextView textView, String text, char[] Oe, int angker){
char T = text.charAt(0);
if(T == Oe[angker]){
textView.setBackgroundColor(Color.parseColor("#7CFC00"));
}
}
public void ChangeSuccessActivity(){
Intent intent = new Intent(getApplicationContext(),SuccessActivity.class);
startActivity(intent);
finish();
}
}
사실 조금 바뀐 건 아니지만,
어쨌든 이렇게 만들어주면
동적으로 뷰가 생성되면서 총 6번의 기회를 주고 성공과 실패의 여부를 나눌 수 있다.
이제 마지막으로 남은 게
외부 api를 이용해서 사용자가 적은 단어가 있는 단어인지 와 랜덤으로 정답 단어를 가지고 오는 것이다.
이 작업은 언제 할지는 모르지만 하게 된다면 가져와 보겠다.
반응형
'안드로이드' 카테고리의 다른 글
[안드로이드] 라이브 데이터 + 데이터 바인딩 알아보기(LiveData + DataBinding) (0) | 2022.03.11 |
---|---|
[안드로이드] Databinding 알아보기 (0) | 2022.03.09 |
[안드로이드] MVVM 알아보기 (0) | 2022.03.07 |
[안드로이드/JAVA] 안드로이드 뒤로가기 2번 입력 (0) | 2022.03.06 |
[안드로이드/JAVA] 안드로이드로 Wordle 만들어 보기 #1 (0) | 2022.03.05 |