// 第3回終了時のソース
// 文字コード UTF-8、改行 CRLF
---------------------------------------------------------------
activity_main.xml
---------------------------------------------------------------
---------------------------------------------------------------
MainActivity.java
---------------------------------------------------------------
package com.example.janken01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.ImageView;
import android.content.Intent;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
TextView txtView01;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnA = (Button)findViewById(R.id.button_A); // グー
Button btnB = (Button)findViewById(R.id.button_B); // チョキ
Button btnC = (Button)findViewById(R.id.button_C); // パー
Button btnGo = (Button)findViewById(R.id.button_Go); // 勝負ボタン
txtView01 = (TextView)findViewById(R.id.text1);
btnGo.setOnClickListener( new View.OnClickListener(){
public void onClick( View view ){
Intent intent = new Intent();
intent.setClassName("com.example.janken01","com.example.janken01.FightActivity");
startActivity(intent);
}
});
btnA.setOnClickListener( new View.OnClickListener(){
public void onClick( View view ){
showResult(1);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.gu);
}
});
btnB.setOnClickListener( new View.OnClickListener(){
public void onClick( View view ){
showResult(2);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.choki);
}
});
btnC.setOnClickListener( new View.OnClickListener(){
public void onClick( View view ){
showResult(3);
ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.pa);
}
});
}
public void showResult(int user){ // 勝敗を決めて表示する
int b = 0;
Random rnd = new Random();
int c = rnd.nextInt(3) + 1; // 乱数 1 or 2 or 3
if( c==1 ){ // コンピュータの手を表示
txtView01.setText("コンピュータ:グー");
}else if( c==2 ){
txtView01.setText("コンピュータ:チョキ");
}else{
txtView01.setText("コンピュータ:パー");
}
if( user==c ){ // あいこだった
return;
}
if( user==1 ){ // 勝敗を判別
if( c==2 ) b=1; else b=0;
}else if( user==2 ){
if( c==3 ) b=1; else b=0;
}else{
if( c==1 ) b=1; else b=0;
}
if( b==1 ){
}else{
}
}
}
---------------------------------------------------------------
activity_fight.xml
---------------------------------------------------------------
---------------------------------------------------------------
FightActivity.java
---------------------------------------------------------------
package com.example.janken01;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
public class FightActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fight);
}
}