Loops Android Java While循环处理程序未执行
我有6个字符串要按顺序显示在Loops Android Java While循环处理程序未执行,loops,while-loop,handler,Loops,While Loop,Handler,我有6个字符串要按顺序显示在文本视图中。但是有些字符串是空的,我希望我的程序检测空字符串,每次检测空字符串都要等待1秒钟,然后继续下一个文本。 我试着使用处理程序和代码来完成它,但我失败了,任何帮助都将不胜感激 TextView Textview1; int str[] = {R.string.str1,R.string.str2,R.string.str3,R.string.str4,R.string.str5,R.string.str6}; int CurrentText = 0;
文本视图中。但是有些字符串是空的,我希望我的程序检测空字符串,每次检测空字符串都要等待1秒钟,然后继续下一个文本。
我试着使用处理程序和代码来完成它,但我失败了,任何帮助都将不胜感激
TextView Textview1;
int str[] = {R.string.str1,R.string.str2,R.string.str3,R.string.str4,R.string.str5,R.string.str6};
int CurrentText = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Textview1 = (TextView)findViewById(R.id.textview1);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Textview1.setText(str[CurrentText]);
String crnt = (String) getText(str[CurrentText]);
CurrentText++;
if(crnt.equals("empty")){
Handler loop = new Handler();
loop.postDelayed(new Runnable() {
@Override
public void run() {
// TODO Auto-generated method stub
String crnt2 = (String) getText(str[CurrentText]);
while(crnt2.equals("empty")){
CurrentText++;
Textview1.setText(str[CurrentText]);
crnt2 = (String) getText(str[CurrentText]);
}
}
}, 1000);
}
我已经解决了
<<resources>
<string name="app_name">gdfg</string>
<string name="action_settings">Settings</string>
<string name="str1">Alice</string>
<string name="str2">empty</string>
<string name="str3">empty</string>
<string name="str4">empty</string>
<string name="str5">Jane</string>
<string name="str6">Beth</string>
public class MainActivity extends Activity {
TextView Textview1;
TextView Textview2;
int str[] = {R.string.str1,R.string.str2,R.string.str3,R.string.str4,R.string.str5,R.string.str6};
int CurrentText = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Textview1 = (TextView)findViewById(R.id.textview1);
Textview2 = (TextView)findViewById(R.id.textView2);
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Textview1.setText(str[CurrentText]);
String cntrl = (String) getText(str[CurrentText]);
if(cntrl.equals("empty")){
handlerTimer.postDelayed(taskUpdateStuffOnDialog, 1000);
}else{
CurrentText++;
}
}
});
}
//Handler definiton...
private Handler handlerTimer = new Handler();
private Runnable taskUpdateStuffOnDialog = new Runnable() {
public void run() {
CurrentText++;
String cre = (String) getText(str[CurrentText]);
Textview1.setText(cre);
if(cre.equals("empty")){
handlerTimer.postDelayed(this, 1000);
}else{
//Çalışmayı durdurmak için de aşağıdaki kod kullanılabilir:
CurrentText++;
handlerTimer.removeCallbacks(taskUpdateStuffOnDialog);
}
}
};