2016-04-12 7 views
0

私は単純なマッチペアアプリを作ろうとしています。アプリケーションは、テキストとして "#"を持つすべてのTextViewsで始まります。テキストフィールドを押すと、そのフィールドに格納された文字に変更されます。2つのTextViewを比較する単純なマッチングペアアプリ

とにかく、2つのフィールドを開いて(文字列を変更する)、それらを比較したいと思います。両者が等しい場合、新しい文字で「開かれたまま」のままです(両方とも「A」です)。文字が等しい場合はすべて正常に動作しますが、文字が異なる場合は問題があります。

等しくない場合は、新しいテキストを1秒間表示し、その後に前の記号(「#」)を表示するようにしたいと考えています。最初のフィールドを押すとテキストが変更されますが、2番目のフィールドに移動して保存された文字が等しくない場合、2番目のフィールドはテキストを変更せず、最初のフィールドのテキストは前の記号に戻ります。

文字列が等しくない場合、新しいテキストのフィールドを短時間表示するにはどうすればよいですか?

enter image description here

public class MainActivity extends AppCompatActivity { 

    TextView textView; 
    int counterForPressedFields= 0; 
    int textViewForComparationCounter = 0; 
    TextView firstTextViewForComparation; 
    TextView secondTextViewForComparation; 
    int[] nonPressedFields= {1, 1, 1, 1}; 
    int pressedField = 2; 
    int tag = 0; 


    public void show(View view) throws InterruptedException { 

     textView = (TextView) view; 

     tag = Integer.parseInt(textView.getTag().toString()); 

     if (nonPressedFields[tag] == 1) { 

      nonPressedFields[tag] = pressedField; 

      if (counterForPressedFields< 2) { 
       textViewForComparationCounter += 1; 
       counterForPressedFields+= 1; 

       switch (tag) { 

        case 0: 
         textView = (TextView) findViewById(R.id.front1); 
         textView.setText("A"); 
         break; 

        case 1: 
         textView = (TextView) findViewById(R.id.front2); 
         textView.setText("B"); 
         break; 

        case 2: 
         textView = (TextView) findViewById(R.id.front3); 
         textView.setText("A"); 
         break; 
        case 3: 
         textView = (TextView) findViewById(R.id.front4); 
         textView.setText("B"); 
         break; 
       } 

       if (textViewForComparationCounter == 1) { 
        firstTextViewForComparation = (TextView) view; 
        firstTextViewForComparation = textView; 

       } else if (textViewForComparationCounter == 2) { 
        secondTextViewForComparation= (TextView) view; 
        secondTextViewForComparation= textView; 
       } 
      } 

      if(counterForPressedFields == 2){ 
       if(firstTextViewForComparation.getText().toString().equals(secondTextViewForComparation.getText().toString())){ 

        counterForPressedFields= 0; 
        textViewForComparationCounter = 0; 
       }else{ 

        firstTextViewForComparation.setText("#"); 
        secondTextViewForComparation.setText("#"); 
        nonPressedFields[Integer.parseInt(firstTextViewForComparation.getTag().toString())] = 1; 
        nonPressedFields[Integer.parseInt(secondTextViewForComparation.getTag().toString())] = 1; 
        counterForPressedFields= 0; 
        textViewForComparationCounter = 0; 
       } 
      } 

     } 

    } 


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

答えて

3

をハンドラを使用してみてください:

これは私のテーブルがどのように見えるかです。 textViewsをpostDelayedメソッドで "#"に更新することができます。

private final Handler handler = new Handler(); 
Runnable runnable = new Runnable() { 
     public void run() { 
      //update your textViews here 
     } 
    }; 
handler.postDelayed(runnable, millisecondsOfDelay); 

希望します。

関連する問題