簡體   English   中英

如何在我的主要活動中循環一段代碼?

[英]How do I loop a piece of code in my main activity?

我正在使用Android Studio,並且想每半秒循環一次

"Random rand = new Random(); 
int value = rand.nextInt(10);"

因此,無論如何,感謝您的時間,如果您能提供幫助,那將是非常棒的。 :)

此致
伊戈爾

編輯
感謝大家的友好和有益的回答。 嘗試每個答案后,我都會盡快選擇最佳答案。 (現在不在我的電腦上),但再次感謝大家。 編輯
對於任何有類似問題的人,我都能奏效。 這是最終代碼。 打包sarju7.click;

import android.os.Handler;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

import java.util.Random;


public class MainActivity extends ActionBarActivity {

    Random rand = new Random();
    Handler handler = new Handler();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Runnable r = new Runnable() {
            public void run() {
                int value = rand.nextInt(10);
                TextView t1 = (TextView) findViewById(R.id.clicker);
                t1.setText(Integer.toString(value));
                handler.postDelayed(this, 400);
            }
        };
        handler.postDelayed(r, 400);
    }

    }

再次感謝大家。 你們是最棒的。 我愛所有的堆棧溢出!

使用postDelayed() 例如,此活動每五秒鍾顯示一次Toast

/***
  Copyright (c) 2012 CommonsWare, LLC
  Licensed under the Apache License, Version 2.0 (the "License"); you may not
  use this file except in compliance with the License. You may obtain a copy
  of the License at http://www.apache.org/licenses/LICENSE-2.0. Unless required
  by applicable law or agreed to in writing, software distributed under the
  License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
  OF ANY KIND, either express or implied. See the License for the specific
  language governing permissions and limitations under the License.

  From _The Busy Coder's Guide to Android Development_
    http://commonsware.com/Android
 */

package com.commonsware.android.post;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class PostDelayedDemo extends Activity implements Runnable {
  private static final int PERIOD=5000;
  private View root=null;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    root=findViewById(android.R.id.content);
  }

  @Override
  public void onResume() {
    super.onResume();

    run();
  }

  @Override
  public void onPause() {
    root.removeCallbacks(this);

    super.onPause();
  }

  @Override
  public void run() {
    Toast.makeText(PostDelayedDemo.this, "Who-hoo!", Toast.LENGTH_SHORT)
         .show();
    root.postDelayed(this, PERIOD);
  }
}

run()Runnable run()方法中進行工作,並安排Runnable在所需的延遲時間后再次運行。 只需調用removeCallbacks()即可結束循環。 您可以在任何小部件上調用postDelayed() 就我而言,我正在使用框架提供的FrameLayout稱為android.R.id.content ,因為此活動沒有其他UI。

用這個

在onCreate上創建

 Random rand = new Random();
handler=new Handler();

handler.postDelayed(myRunnable, 100);

在onCreate外部聲明

myRunnable=new Runnable() {

        @Override
        public void run() {

           int value = rand.nextInt(10);
            handler.postDelayed(this, 100);
        }
    }
Random rand = new Random();
Handler handler = new Handler()

Runnable r=new Runnable() {
        public void run() { 
            int value = rand.nextInt(10);
            handler.postDelayed(this, 500);     
        }
    };

handler.postDelayed(r, 500);

雖然這里的所有答案都不錯,但不一定能說明為什么要這樣做或將對結果做些什么 您可以在代碼中的任何位置執行此操作,但是您可能會問,因為如果在其中運行UI線程並且您可能不希望這樣做,它將阻塞UI線程。

您需要在另一個線程或服務上在后台運行此程序,以便用戶可以在循環運行時進行交互。 那是基本的答案-在MAIN或UI線程之外的另一個線程上運行此循環。 (這里有很多答案可以解決這個問題。)

如果您希望每半秒在屏幕上顯示一個隨機數,那么這些選項中的許多選項都很好,只是它們不能解釋如果您在其他線程中運行它們,則需要在Activity創建該類,然后使用runOnUiThread線程方法更新您的視圖類(否則您將得到錯誤)。

如果要使用它作為開始進行進一步的后台處理的起點,則應考慮使用Service ,在其中可以擴展循環的功能以及在UI線程運行時可能需要執行的其他任務。 例如,如果您需要隨機數來選擇屏幕上顯示的圖像,則可能要在向Activity提供圖像的服務中運行它。

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM