gpt4 book ai didi

java - 如何在 Android 中使用 runnable 循环

转载 作者:行者123 更新时间:2023-11-30 03:10:16 25 4
gpt4 key购买 nike

我正在做一个应用程序,我必须在其中显示一系列图片:顺序如下:

  1. 红色 X 必须显示大约 400 毫秒;
  2. 一张名为“屏幕截图”的图片必须显示大约 500毫秒;
  3. 一张名为“噪音”的图片必须展示大约 50 分钟毫秒;
  4. “噪音”图像消失,为 EditText 腾出空间。

EditText 用于获取用户数据。

这是我的代码:

public class Step1Training extends Activity {

ImageFragment myImageFragment;
InputFragment myInputFragment;
Drawable cross;
ImageView myImageView;
EditText myEditText;
int i, length;
String rating;
Handler handler;
boolean continueTask;
int[] screenshots = {
R.drawable.googleplaybooks2,
R.drawable.aldiko1,
R.drawable.chaton2,
R.drawable.cinetrailer1,
R.drawable.fanpage1
};

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

handler = new Handler();
i = 0;
length = screenshots.length;

initUI();

startThread();
}

private void initUI(){
myImageFragment = (ImageFragment)getFragmentManager().findFragmentById(R.id.imageFragment);
myInputFragment = (InputFragment)getFragmentManager().findFragmentById(R.id.inputFragment);

myImageView = (ImageView)myImageFragment.getView().findViewById(R.id.screenshotImageView);
myImageView.setImageResource(R.drawable.cross300x300);

myEditText = (EditText)myInputFragment.getView().findViewById(R.id.ratingEditText);
myEditText.setVisibility(View.GONE);
}

private void startThread(){

while(i<length){
handler.postDelayed(new TaskScreenshot(i), 250);
handler.postDelayed(new TaskNoise(), 750);
handler.postDelayed(new TaskEditText(), 1000);

myEditText.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_ENTER)) {
if(myEditText.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Inserire un numero da 1 a 9", Toast.LENGTH_SHORT).show();
return false;
} else if (Integer.parseInt(myEditText.getText().toString()) >= 1 &&
Integer.parseInt(myEditText.getText().toString()) <= 9){
return true;
}
}
return false;
}
});
System.out.println(i);
i++;
}
}

@Override
public void onBackPressed() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Attenzione")
.setMessage("Sei sicuro/a di voler uscire dall'applicazione?\n" +
"Confermando uscirai dall'applicazione e perderai \n" +
"tutti i dati raccolti finora.")
.setCancelable(false)
.setNegativeButton("No",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setPositiveButton("Si", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int whichButton)
{
Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
});
AlertDialog alert = builder.create();
alert.show();
}

class TaskScreenshot implements Runnable{

int j;

public TaskScreenshot(int _i){
this.j = _i;
}

@Override
public void run() {
System.out.println("Screenshot");
myEditText.setVisibility(View.GONE);
myImageFragment.getView().setVisibility(View.VISIBLE);
myImageView.setImageResource(screenshots[j]);
myImageView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
myImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
}
}

class TaskNoise implements Runnable{

@Override
public void run() {
System.out.println("Noise");
myImageView.setImageResource(R.drawable.noise);
myImageView.getLayoutParams().width = LayoutParams.MATCH_PARENT;
myImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
}
}

class TaskEditText implements Runnable{

@Override
public void run() {
System.out.println("EditText");
myEditText.setVisibility(View.VISIBLE);
myImageFragment.getView().setVisibility(View.GONE);
}
}
}

当我运行此 Activity 时,它显示红色的 X、数组的最后一个屏幕截图和噪声图像。 Logcat 显示:

0
1
2
3
4
Screenshot
Screenshot
Screenshot
Screenshot
Screenshot
Noise
Noise
Noise
Noise
Noise
EditText
EditText
EditText
EditText
EditText

我在这个循环中做错了什么?而且,我怎样才能停止循环,直到用户输入数据?

更新 1:

我已经按照用户 Groco 的建议更改了代码:

private void startThread(final int i){



if(i < length){

handler.postDelayed(new TaskScreenshot(i), 250);
handler.postDelayed(new TaskNoise(), 750);
handler.postDelayed(new TaskEditText(), 1000);

myEditText.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_ENTER)) {
if(myEditText.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Inserire un numero da 1 a 9", Toast.LENGTH_SHORT).show();
return false;
} else if (Integer.parseInt(myEditText.getText().toString()) >= 1 &&
Integer.parseInt(myEditText.getText().toString()) <= 9){
System.out.println(i);
startThread(i+1);
return true;
}
}
return false;
}
});

}
}

但是当我运行它时,应用程序显示 1°、3° 和 5° 屏幕截图(索引 0、2、4),而不是全部。我的索引有问题吗?

最佳答案

使用用户交互的结果重新启动一个周期:

 private void startThread(int i){
if (i<length){

handler.postDelayed(new TaskScreenshot(i), 250);
handler.postDelayed(new TaskNoise(), 750);
handler.postDelayed(new TaskEditText(), 1000);

myEditText.setOnKeyListener(new View.OnKeyListener(){
public boolean onKey(View v, int keyCode, KeyEvent event) {
if((keyCode == KeyEvent.KEYCODE_ENTER)) {
if(myEditText.getText().toString().equals("")){
Toast.makeText(getApplicationContext(), "Inserire un numero da 1 a 9", Toast.LENGTH_SHORT).show();
return false;
} else if (Integer.parseInt(myEditText.getText().toString()) >= 1 &&
Integer.parseInt(myEditText.getText().toString()) <= 9){
System.out.println(i);
i++;
startThread(i);
}
}
return false;
}
});

}
return false;
}

关于java - 如何在 Android 中使用 runnable 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21111852/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com