2016-04-12 72 views
0

我試圖做一個簡單的匹配配對應用程序。該應用程序以具有「#」的所有TextView作爲文本開始。當我按下一個文本字段時,它將更改爲爲該字段存儲的字母。簡單匹配配對應用程序,比較兩個TextViews

無論如何,我想打開兩個字段(我的意思是改變他們的字符串)並比較它們。如果它們相同,它們仍然用新的字母「打開」(例如都是「A」)。在平等的信件的情況下,一切正常,但我有困難時,字母不同。

如果它們不相等,我想讓它們以一秒鐘的新文本展示自己,然後在之後轉到前一個標記(即「#」)。當我按下第一個字段時,它會更改其文本,但是當我轉到第二個字段時,如果它們的存儲的字母不相等,則第二個字段不會更改文本,第一個字段中的文本會返回到之前的標記。

如果字符串不相等,我需要做些什麼才能在短時間內顯示帶有新文本的字段?

這是我的表的樣子:

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

嘗試使用的處理程序。您可以使用其postDelayed方法將textViews更新回「#」。

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

希望它有幫助!