2016-04-07 15 views
1
  1. Bu koddur.
  2. Daha fazla bilgiyi buraya koymam gerekiyor mu?

Metin ve düğme görünümlerimin neden genişletildiğini bulmaya çalışıyorum ve bu kodun çalışması sırasında normale dönüyorum.Bu kodun çalışması sırasında görüşlerim neden genişliyor ve normale dönüyor?

Daha fazla ayrıntıya ihtiyacım olduğunu söyler, bu yüzden burada bazı saçmalıklardan bahsedeceğim.

package com.notesquirrel.johnald.memorymagic; 

import android.content.SharedPreferences; 
import android.media.MediaPlayer; 

import android.os.Handler; 
import android.os.Message; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.view.animation.Animation; 
import android.view.animation.AnimationUtils; 
import android.widget.Button; 
import android.widget.TextView; 
import android.widget.Toast; 

import java.util.Random; 


public class GameActivity extends AppCompatActivity implements View.OnClickListener { 

    Animation wobble; 


    SharedPreferences prefs; 
    SharedPreferences.Editor editor; 
    String dataName = "MyData"; 
    String intName = "MyInt"; 
    int defaultInt = 0; 
    int highScore; 


    TextView textScore; 
    TextView textDifficulty; 
    TextView textWatchGo; 

    Button button1; 
    Button button2; 
    Button button3; 
    Button button4; 
    Button buttonRestart; 


    int difficultyLevel = 3; 
    int[] sequenceToCopy = new int[100]; 

    private Handler myHandler; 
    boolean playSequence = false; 
    int elementToPlay = 0; 

    int playerResponses; 
    int playerScore; 
    boolean isResponding; 

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

     wobble = AnimationUtils.loadAnimation(this, R.anim.wobble); 


     prefs = getSharedPreferences(dataName, MODE_PRIVATE); 
     editor = prefs.edit(); 
     highScore = prefs.getInt(intName, defaultInt); 


     final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru); 
     final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2); 
     final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3); 
     final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4); 

     textScore = (TextView) findViewById(R.id.textScore); 
     assert textScore != null; 
     textScore.setText("Score: " + playerScore); 

     textDifficulty = (TextView) findViewById(R.id.textDifficulty); 
     assert textDifficulty != null; 
     textDifficulty.setText("Level: " + difficultyLevel); 

     textWatchGo = (TextView) findViewById(R.id.textWatchGo); 

     button1 = (Button) findViewById(R.id.button21); 
     button2 = (Button) findViewById(R.id.button32); 
     button3 = (Button) findViewById(R.id.button43); 
     button4 = (Button) findViewById(R.id.button54); 
     buttonRestart = (Button) findViewById(R.id.buttonRestart); 

     button1.setOnClickListener(this); 
     button2.setOnClickListener(this); 
     button3.setOnClickListener(this); 
     button4.setOnClickListener(this); 
     buttonRestart.setOnClickListener(this); 
     //thread 
     myHandler = new Handler() { 
      public void handleMessage(Message msg) { 
       super.handleMessage(msg); 

       if (playSequence) { 

        button1.setVisibility(View.VISIBLE); 
        button2.setVisibility(View.VISIBLE); 
        button3.setVisibility(View.VISIBLE); 
        button4.setVisibility(View.VISIBLE); 

        switch (sequenceToCopy[elementToPlay]) { 
         case 1: 
          // button1.setVisibility(View.INVISIBLE); 
          button1.startAnimation(wobble); 
          powerup7.start(); 
          break; 


         case 2: 
          //button2.setVisibility(View.INVISIBLE); 
          button2.startAnimation(wobble); 
          powerup8.start(); 
          break; 

         case 3: 
          //button3.setVisibility(View.INVISIBLE); 
          button3.startAnimation(wobble); 
          powerup9.start(); 
          break; 

         case 4: 
          // button4.setVisibility(View.INVISIBLE); 
          button4.startAnimation(wobble); 
          powerup10.start(); 
          break; 


        } 
        elementToPlay++; 
        if (elementToPlay == difficultyLevel) { 

         sequenceFinished(); 
        } 


       } 
       myHandler.sendEmptyMessageDelayed(0, 900); 
      } 
     }; 
     myHandler.sendEmptyMessage(0); 


     playASequence(); 


    } 

    @Override 
    public void onClick(View v) { 
     final MediaPlayer powerup7 = MediaPlayer.create(this, R.raw.poweru); 
     final MediaPlayer powerup8 = MediaPlayer.create(this, R.raw.poweru2); 
     final MediaPlayer powerup9 = MediaPlayer.create(this, R.raw.poweru3); 
     final MediaPlayer powerup10 = MediaPlayer.create(this, R.raw.poweru4); 


     if (!playSequence) { 

      switch (v.getId()) { 


       case R.id.button21: 
        powerup7.start(); 
        checkElement(1); 
        break; 

       case R.id.button32: 
        powerup8.start(); 
        checkElement(2); 
        break; 

       case R.id.button43: 
        powerup9.start(); 
        checkElement(3); 
        break; 

       case R.id.button54: 
        powerup10.start(); 
        checkElement(4); 
        break; 

       case R.id.buttonRestart: 
        difficultyLevel = 3; 
        playerScore = 0; 
        textScore.setText("Score: " + playerScore); 
        playASequence(); 
        break; 


      } 
     } 


    } 


    public void createSequence() { 
     //For choosing a random button 
     Random randInt = new Random(); 
     int ourRandom; 
     for (int i = 0; i < difficultyLevel; i++) { 
      //get a random number between 1 and 4 
      ourRandom = randInt.nextInt(4); 
      ourRandom++;//make sure it is not zero 
      //Save that number to our array 
      sequenceToCopy[i] = ourRandom; 
     } 

    } 


    public void playASequence() { 
     createSequence(); 
     isResponding = false; 
     elementToPlay = 0; 
     playerResponses = 0; 
     textWatchGo.setText("WATCH!"); 
     playSequence = true; 

    } 


    public void sequenceFinished() { 
     playSequence = false; 
     //make sure all the buttons are made visible 
     // button1.setVisibility(View.VISIBLE); 
     // button2.setVisibility(View.VISIBLE); 
     // button3.setVisibility(View.VISIBLE); 
     // button4.setVisibility(View.VISIBLE); 
     textWatchGo.setText("GO!"); 
     isResponding = true; 
    } 


    public void checkElement(int thisElement) { 

     if (isResponding) { 
      playerResponses++; 
      if (sequenceToCopy[playerResponses - 1] == thisElement) {//Correct 
       playerScore = playerScore + ((thisElement + 1) * 2); 
       textScore.setText("Score: " + playerScore); 
       if (playerResponses == difficultyLevel) {//got the whole  sequence 
        //don't checkElelment anymore 
        isResponding = false; 
        //now raise the difficulty 
        difficultyLevel++; 
        //and play another sequence 
        playASequence(); 
       } 

      } else {//wrong answer 

       textWatchGo.setText("FAILED!"); 
       //don't checkElelment anymore 
       isResponding = false; 
       if (playerScore > highScore) { 
        highScore = playerScore; 
        editor.putInt(intName, highScore); 
        editor.commit(); 

        Toast.makeText(getApplicationContext(), "New high score!!", Toast.LENGTH_LONG).show(); 

       } 


      } 


     } 
    } 

} 

XML düzeni:

<?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: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.notesquirrel.johnald.memorymagic.GameActivity" 
android:background="#000000"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Score: 999" 
    android:textSize="40sp" 
    android:id="@+id/textScore" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Difficulty: 4" 
    android:textSize="25sp" 
    android:id="@+id/textDifficulty" 
    android:layout_below="@+id/textScore" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Watch/Go" 
    android:id="@+id/textWatchGo" 
    android:layout_marginTop="48dp" 
    android:textSize="25sp" 
    android:layout_below="@+id/textDifficulty" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    android:id="@+id/button21" 
    android:layout_marginTop="50dp" 
    android:layout_below="@+id/textWatchGo" 
    android:layout_alignRight="@+id/textScore" 
    android:layout_alignEnd="@+id/textScore" 
    android:layout_alignLeft="@+id/textScore" 
    android:layout_alignStart="@+id/textScore" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 

    android:text="2" 
    android:id="@+id/button32" 
    android:layout_marginTop="10dp" 

    android:layout_below="@+id/button21" 
    android:layout_alignRight="@+id/button21" 
    android:layout_alignEnd="@+id/button21" 
    android:layout_alignLeft="@+id/button21" 
    android:layout_alignStart="@+id/button21" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="3" 
    android:layout_marginTop="10dp" 

    android:id="@+id/button43" 
    android:layout_below="@+id/button32" 
    android:layout_alignRight="@+id/button32" 
    android:layout_alignEnd="@+id/button32" 
    android:layout_alignLeft="@+id/button32" 
    android:layout_alignStart="@+id/button32" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="4" 
    android:layout_marginTop="10dp" 

    android:id="@+id/button54" 
    android:layout_below="@+id/button43" 
    android:layout_alignRight="@+id/button43" 
    android:layout_alignEnd="@+id/button43" 
    android:layout_alignLeft="@+id/button43" 
    android:layout_alignStart="@+id/button43" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Restart" 
    android:id="@+id/buttonRestart" 
    android:layout_marginTop="10dp" 

    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    android:layout_alignParentEnd="true" /> 

</RelativeLayout> 
+0

Bu düğmeleri içeren düzeni göster –

+0

Tamam, java'nın altına ekledim. – user6169595

+0

Yeni mesaj var. Olası işleyici sızıntısını ele almamak buna neden olabilir mi? – user6169595

cevap

0

benim cihazla sorunum yok. Ama bazı kısıtlamaları kaldırdım.

<?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: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.notesquirrel.johnald.memorymagic.GameActivity" 
    android:background="#000000"> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Score: 999" 
    android:textSize="40sp" 
    android:id="@+id/textScore" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Difficulty: 4" 
    android:textSize="25sp" 
    android:id="@+id/textDifficulty" 
    android:layout_below="@+id/textScore" 
    android:layout_centerHorizontal="true" /> 

<TextView 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textAppearance="?android:attr/textAppearanceLarge" 
    android:text="Watch/Go" 
    android:id="@+id/textWatchGo" 
    android:layout_marginTop="48dp" 
    android:textSize="25sp" 
    android:layout_below="@+id/textDifficulty" 
    android:layout_centerHorizontal="true" /> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="1" 
    android:id="@+id/button21" 
    android:layout_marginTop="50dp" 
    android:layout_below="@+id/textWatchGo" 
    android:layout_alignRight="@+id/textScore" 
    android:layout_alignLeft="@+id/textScore" 
/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="2" 
    android:id="@+id/button32" 
    android:layout_marginTop="10dp" 
    android:layout_below="@+id/button21" 
    android:layout_alignRight="@+id/button21" 
    android:layout_alignLeft="@+id/button21" 
/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="3" 
    android:layout_marginTop="10dp" 
    android:id="@+id/button43" 
    android:layout_below="@+id/button32" 
    android:layout_alignRight="@+id/button32" 
    android:layout_alignLeft="@+id/button32" 
/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="4" 
    android:layout_marginTop="10dp" 
    android:id="@+id/button54" 
    android:layout_below="@+id/button43" 
    android:layout_alignRight="@+id/button43" 
    android:layout_alignLeft="@+id/button43" 
/> 

<Button 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:text="Restart" 
    android:id="@+id/buttonRestart" 
    android:layout_marginTop="10dp" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentRight="true" 
    /> 

</RelativeLayout> 
+0

Tamam, bunu anladın. Yukarıdaki görünümün sonuna kadar tüm görünümler hizalandı. Dolayısıyla, java'm skoru artırdığında, ilk görünümün genişliğini artırdı ve bu da diğer görünümlerin bu ilk görünümün sonuna hizalanmasını sağladı. Sonra oyunu kaybettiğimde ve skoru sıfırladığımda normal boyuta geri çekilecektim ... – user6169595

+0

Sorunu çözmede yardımcı oldu mu? –

+0

Evet. Teşekkür ederim. – user6169595

İlgili konular