我正在尝试制作一个简单的配对应用。该应用程序以所有TextViews以“#”作为文本开始。当我按下文本字段时,它会变为为该字段存储的字母。
无论如何,我想打开两个字段(我的意思是更改字符串)并进行比较。如果它们相等,则用新字母保持“打开”(例如两者都是“A”)。在字母相同的情况下,一切都很好,但是当字母不同时我会遇到困难。
如果它们不相等,我想让它们用新文本显示一秒钟,然后转到上一个符号(即“#”)。当我按下第一个字段时,它会更改其文本,但是当我转到第二个字段并且如果它们存储的字母不相等时,第二个字段不会更改文本,并且第一个字段中的文本将返回到上一个字段
如果字符串不相等,我需要做什么才能在短时间内显示带有新文本的字段?
这就是我的表格:
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);
}
}
答案 0 :(得分:3)
尝试使用处理程序。您可以将textViews更新回&#34;#&#34;使用postDelayed方法。
private final Handler handler = new Handler();
Runnable runnable = new Runnable() {
public void run() {
//update your textViews here
}
};
handler.postDelayed(runnable, millisecondsOfDelay);
希望它有所帮助!