嘿,我使用下面的代码来使用按钮来获得新的文本到一个文本视图, 但它的滞后,并没有工作一半的时间,我按它,但另一半的时间它工作正常,任何人有任何idé?随机文本,缓慢和迟缓
final Button button = (Button) findViewById(R.id.slumpaovrigt);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Random myRandom5 = new Random();
TextView textovrigt = (TextView) findViewById(R.id.textovrigt);
switch(myRandom5.nextInt() %151) {
case 0:
textovrigt.setText("text 1");
break;
case 1:
textovrigt.setText("text 2");
break;
case 2:
textovrigt.setText("text 3");
break;
case 3:
textovrigt.setText("text 4");
break;
}
}
});
}
}
此外,我使用4个不同的XML视图白衣相同的代码但对于不同的按钮和textviews并且还白衣不同的文本中,每个含有150周围不同的文本(例)。
改变了代码升技它变得更好,但是还是有点慢
这是在java文件
import java.util.Random;
import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class Ovrigtskamt extends Activity {
Button slumpaovrigt;
TextView textovrigt;
Random slumpaovrigt2 = new Random();
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.ovrigttext);
super.onCreate(savedInstanceState);
this.setRequestedOrientation(
ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
slumpaovrigt = (Button) findViewById(R.id.slumpaovrigt);
textovrigt = (TextView)findViewById(R.id.textovrigt);
loadDoc();
}
private void loadDoc(){
String s = "";
for(int x=0;x<=100;x++){
s += "Line: "+String.valueOf(x)+"\n";
}
textovrigt.setMovementMethod(new ScrollingMovementMethod());
textovrigt.setText("Klicka på Slumpa");
slumpaovrigt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
switch(slumpaovrigt2.nextInt() %4) {
case 0:
case 0:
textovrigt.setText("text 1");
break;
case 1:
textovrigt.setText("text 2");
break;
case 2:
textovrigt.setText("text 3");
break;
case 3:
textovrigt.setText("text 4");
break;
}
}
});
}
}
完整的代码,但作为所述丝毫151个不同的文本
是的,我确实有151在实际的代码,我改成了random.nextInt(4),但它仍然缓慢。 – user1373308
这条线现在看起来像什么?只是'switch(random.nextInt(4)){'并且不是%151我希望? –
OH我误读了你所说的话,现在改变了它,它完美的工作,你的救星m8! ty – user1373308