반응형
랜덤 수를 이용한 홀 짝
package com.example.increase;
//본인의 package에 따라 변경
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class HollActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_holl);
// 폴더이름 확인(activity_holl)
// 여기부터 버튼과 글 불러온다.
EditText sEtm = findViewById(R.id.mEtMine);
TextView sEtc = findViewById(R.id.mEtCom);
TextView sEtr = findViewById(R.id.mEtResult);
Button sbtn = findViewById(R.id.mBtn);
// 버튼 클릭시 이벤트
sbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int ran = (int) (Math.random() * 2);
String rans = "";
if(ran == 1){
rans = "홀";
}else if(ran == 0){
rans = "짝";
}
sEtc.setText(rans);
String a = sEtm.getText().toString();
System.out.println("랜"+sEtm);
if (a.equals(rans)) {
sEtr.setText("승");
}else if(!(a.equals(rans))){
sEtr.setText("패");
}else {
sEtr.setText("다시");
}
}
});
}
}
- java코드부분
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="130dp"
tools:layout_editor_absoluteY="125dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/mTvMine"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="나"
android:textSize="18sp" />
<EditText
android:id="@+id/mEtMine"
android:layout_width="374dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="홀" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/mTvCom"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="컴"
android:textSize="18sp" />
<EditText
android:id="@+id/mEtCom"
android:layout_width="374dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<TextView
android:id="@+id/mTvResult"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="결과"
android:textSize="18sp" />
<EditText
android:id="@+id/mEtResult"
android:layout_width="374dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal">
<Button
android:id="@+id/mBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="실행하기" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
- 안드로이드 화면 설정
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.increase">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.Increase">
<activity android:name=".HollActivity">
//폴더이름 확인(HollActivity)
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
- manifests 폴더 이름을 확인을위해 참조
반응형
'앱 > Android(안드로이드 스튜디오)' 카테고리의 다른 글
Android(안드로이드 스튜디오) 배너 광고 달기 (1) | 2024.07.09 |
---|---|
안드로이드 스튜디오 Running Devices (Device mode)변경 (0) | 2024.03.10 |
Android(안드로이드 스튜디오)AI TensorFlow(머신러닝 학습) (0) | 2021.04.26 |
Android(안드로이드 스튜디오) 난수(랜덤함수) (0) | 2021.04.26 |
Android(안드로이드 스튜디오) 버튼 이벤트 (0) | 2021.04.26 |