###前提・実現したいこと
androidstudio 2.3 でアプリ開発の勉強を独学でやっています。
ボタンを押すとテキストが表示されるようにしたいです。
(じゃんけんゲームの動作の一部)
###発生している問題
ボタンを押してもテキストが表示されない。
###該当のソースコード
Java
1package numa.jyanken; 2 3 import android.support.v7.app.AppCompatActivity; 4 import android.os.Bundle; 5 import android.view.View; 6 import android.widget.Button; 7 import android.widget.TextView; 8 9 import java.util.Random; 10 11public class JyankenActivity extends AppCompatActivity { 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.jyanken_main); 17 18 final TextView textview = (TextView) findViewById(R.id.editText); 19 Button btn = (Button) findViewById(R.id.button); 20 btn.setOnClickListener(new View.OnClickListener() { 21 @Override 22 public void onClick(View v) { 23 24 int[] player = new int[2]; 25 26 for (int i = 0; i <= 1; i++) { 27 Random rnd = new Random(); 28 int rand = rnd.nextInt(3); 29 player[i] = (int) Math.floor(rand); 30 } 31 32 if (player[0] - player[1] == 0) { 33 textview.setText("引き分け"); 34 } else if (player[0] - player[1] == -2) { 35 textview.setText("負け"); 36 } else if (player[0] - player[1] == -1) { 37 textview.setText("勝ち"); 38 } else if (player[0] - player[1] == 1) { 39 textview.setText("負け"); 40 } else { 41 textview.setText("勝ち"); 42 } 43 44 45 } 46 47 48 }); 49 } 50} 51
XML
1<?xml version="1.0" encoding="utf-8"?> 2<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:app="http://schemas.android.com/apk/res-auto" 4 xmlns:tools="http://schemas.android.com/tools" 5 android:layout_width="match_parent" 6 android:layout_height="match_parent" 7 tools:context="numa.jyanken.JyankenActivity"> 8 9 <Button 10 android:id="@+id/button" 11 android:layout_width="wrap_content" 12 android:layout_height="wrap_content" 13 android:onClick="click" 14 android:text="Button" 15 tools:layout_editor_absoluteX="147dp" 16 tools:layout_editor_absoluteY="230dp" /> 17 18 <EditText 19 android:id="@+id/editText" 20 android:layout_width="wrap_content" 21 android:layout_height="wrap_content" 22 android:ems="10" 23 android:inputType="textPersonName" 24 android:text="Name" 25 tools:layout_editor_absoluteX="84dp" 26 tools:layout_editor_absoluteY="163dp" /> 27</android.support.constraint.ConstraintLayout> 28
###試したこと
何を間違えているのかを考えましたが、わかりませんでした。
###補足情報(言語/FW/ツール等のバージョンなど)
AndroidStudio2.3
回答2件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。