2017-03-04 1 views
1

を作ることはできません。この数学ゲームでは、簡単な方程式を解きたいと思っています。これは次のようになります:9 * __ = 45、プレイヤーは正しい数を入力して方程式を解き、正しいボタンを押します。正しいスコアがプレーヤーに追加されている場合。は、私は現在、非常に単純な数学のゲームを作ってるんだのTextViewのミックスを使用して計算とのEditText

空きスペースがあるのEditText、他はTextViewsです。 TextView & EditTextの組み合わせを使用しているので、私は何とかプログラムを読み込んで計算できるように変換する必要があります。私は狂ったように読んでいて、成功していないすべての種類の方法を試しました。これを回避するにはどうすればよいですか?

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
     xmlns:tools="http://schemas.android.com/tools" 
     android:id="@+id/activity_play" 
     android:layout_width="match_parent" 
     android:layout_height="match_parent" 
     android:paddingBottom="@dimen/activity_vertical_margin" 
     android:paddingLeft="@dimen/activity_horizontal_margin" 
     android:paddingRight="@dimen/activity_horizontal_margin" 
     android:paddingTop="@dimen/activity_vertical_margin" 
     tools:context="com.example.android.laboration2.PlayActivity"> 


     <TextView 
      android:id="@+id/textPlayMultiply" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignTop="@+id/textPlayNumber1" 
      android:layout_centerHorizontal="true" 
      android:text="@string/multiply" 
      android:textAlignment="textStart" 
      android:layout_gravity = "start" 
      android:textColor="@android:color/black" 
      android:textSize="40sp" /> 

     <TextView 
      android:id="@+id/textPlayNumber1" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignEnd="@+id/textPlayScore" 
      android:layout_alignParentTop="true" 
      android:layout_alignRight="@+id/textPlayScore" 
      android:layout_marginTop="48dp" 
      android:text="@string/number_1" 
      android:textAlignment="center" 
      android:textColor="@android:color/black" 
      android:textSize="50sp" /> 

     <TextView 
      android:id="@+id/textPlayEqual" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="16dp" 
      android:text="@string/equal" 
      android:textAlignment="center" 
      android:textColor="@android:color/black" 
      android:textSize="50sp" 
      android:layout_below="@+id/editTextPlayNumber2" 
      android:layout_alignLeft="@+id/textPlayMultiply" 
      android:layout_alignStart="@+id/textPlayMultiply" /> 

     <TextView 
      android:id="@+id/textPlayResult" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="29dp" 
      android:text="@string/result_number3" 
      android:textAlignment="center" 
      android:textColor="@android:color/black" 
      android:textSize="50sp" 
      android:layout_below="@+id/textPlayEqual" 
      android:layout_alignLeft="@+id/textPlayEqual" 
      android:layout_alignStart="@+id/textPlayEqual" /> 

     <TextView 
      android:id="@+id/textPlayScore" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignParentBottom="true" 
      android:layout_marginBottom="31dp" 
      android:layout_toStartOf="@+id/answerButton" 
      android:text="@string/score_0" 
      android:textAlignment="textStart" 
      android:layout_gravity="start" 
      android:textColor="@android:color/black" 
      android:textSize="24sp" 
      android:layout_toLeftOf="@+id/answerButton" /> 

     <Button 
      android:id="@+id/answerButton" 
      android:layout_width="120dp" 
      android:layout_height="wrap_content" 
      android:layout_marginTop="36dp" 
      android:background="@android:color/holo_orange_dark" 
      android:text="@string/button_result" 
      android:textAllCaps="false" 
      android:textColor="@android:color/white" 
      android:textSize="18sp" 
      android:textStyle="bold" 
      android:layout_below="@+id/textPlayResult" 
      android:layout_centerHorizontal="true" /> 

     <EditText 
      android:id="@+id/editTextPlayNumber2" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textPlayMultiply" 
      android:layout_alignBottom="@+id/textPlayMultiply" 
      android:layout_marginLeft="16dp" 
      android:layout_marginStart="16dp" 
      android:layout_toEndOf="@+id/answerButton" 
      android:layout_toRightOf="@+id/answerButton" 
      android:hint=" " 
      android:inputType="number" 
      android:textAlignment="center" 
      android:textColor="@android:color/black" 
      android:textSize="50sp" /> 

     <TextView 
      android:id="@+id/textPlayLevel" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_alignBaseline="@+id/textPlayScore" 
      android:layout_alignBottom="@+id/textPlayScore" 
      android:layout_toEndOf="@+id/answerButton" 
      android:layout_toRightOf="@+id/answerButton" 
      android:text="@string/level_0" 
      android:textAlignment="center" 
      android:textColor="@android:color/black" 
      android:textSize="24sp" /> 


    </RelativeLayout> 

activity_play.xml

これは私のデータがどのように見えるかです。

PlayActivity.java

package com.example.android.laboration2; 
    import android.support.v7.app.AppCompatActivity; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.Button; 
    import android.widget.EditText; 
    import android.widget.TextView; 


    public class PlayActivity extends AppCompatActivity implements View.OnClickListener { 


     TextView textPlayNumber1; 
     EditText editTextPlayNumber2; 
     TextView textPlayResult; 
     TextView textPlayScore; 
     TextView textPlayLevel; 
     Button answerButton; 


     @Override 
     protected void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      setContentView(R.layout.activity_play); 

      textPlayNumber1 = (TextView) findViewById(R.id.textPlayNumber1); 
      editTextPlayNumber2 = (EditText) findViewById(R.id.editTextPlayNumber2); 
      textPlayResult = (TextView) findViewById(R.id.textPlayResult); 
      textPlayScore = (TextView) findViewById(R.id.textPlayScore); 
      textPlayLevel = (TextView) findViewById(R.id.textPlayLevel); 

      answerButton = (Button) findViewById(R.id.answerButton); 
      answerButton.setOnClickListener(this); 

     }//onCreate ends here 


     @Override 
     public void onClick(View v) { 

     }//onClick ends here 


    }//PlayActivity ends here 
+0

を行うことができますか?あなたは計算コードを表示していません。 – Roljhon

+0

EditTextがStringの間にTextViewがIntを使用すると常に不満を表明したので、これまで何を試してみたのかは簡単には分かりませんでした。だから私は計算をセットアップする方法を教えてくれる誰かを願って、すべてを消去しました。助言がありますか? –

+0

あなたのコードを投稿すれば、単純に仮定することはできません。これらの要素の適切な値を引き出し、正しく解析するなら、両方とも技術的に動作するはずです – Roljhon

答えて

1

あなたのTextViewとのEditTextすべては、String型の値を持っています。これらの値をIntegerに解析し、結果を計算して表示する必要があります。ここで

は、アクションのonClick解答ボタン -

int score = 0; 
@Override 
public void onClick(View v) { 
     switch(v.getId()){ 
      case R.id.answerButton: 
       int playNum1 = Integer.parseInt(textPlayNumber1.getText().toString()); 
       int playNum2 = Integer.parseInt(editTextPlayNumber2.getText().toString()); 
       int playResult = Integer.parseInt(textPlayResult.getText().toString()); 
       if(playNum1*playNum2 == playResult){ 
        score++; 
        textPlayScore.setText(""+score); 
       }      
      break; 
     } 
    } 

・ホープ、このことができますです。

+0

これは私に大きな助けになりました!十分にあなたに感謝することはできません!私の問題は、私がこれらのいずれかを変換する必要があると思っていたため、常にエラーが発生していました。上記のように、私は両方を文字列にしていたので、整数に解析する必要がありました。私はいつもEditTextをおそらくintのために誤っていました。なぜなら、そこには数字しか書けないからです。以前の投稿でそれについて何かを読んでください。 –

+0

ようこそ。私はあなたが何をここで混乱させたのか理解した。 EditText入力タイプをNumberに設定します。これは、ユーザーの便宜のためにソフト入力方法(キーボード)にのみ影響します。しかし、あなたが得る価値は常に文字列です。あなたはそれについてここで読むことができます:https://developer.android.com/training/keyboard-input/style.html – tahsinRupam

+0

正直なところ、Android Developersのサイトはひどいです。私は彼らがすべてを書き直すか、初心者のための理解しやすいバージョンを作る必要があると思います。私は、他のコードと他のコードとの組み合わせでどのようにコードを使用するか、実際に何を使用するかを説明するときには、ほとんど何も得られません。もう一度、ありがとう!これはダーアイのために私を殺している! 6時間/日の読書とコーディング、読書とコーディング。 私は十分に得て、自分のアカウントを作ることに決めました。今私は以前にそれをやっていないための馬鹿のように感じる。助けは文字通り数分で来て、活動は素晴らしいです! –

2

あなたはどのようにあなたはそれがそれらを混在させることはできませんと言っている

int playNumberValue = Integer.getInteger(textPlayNumber1.getText().toString()); 
int userInputValue = Integer.getInteger(editTextPlayNumber2.getText().toString()); 
int result = Integer.getInteger(textPlayResult.getText().toString()); 

if(result == userInputValue+playNumberValue) 
    //win game 
+1

ありがとうございました。心から感謝する。しかし、上記の記事は私が使いたいと思っていたものです。 –

関連する問題