【问题标题】:loop, words and images in Android [closed]Android中的循环,文字和图像[关闭]
【发布时间】:2012-12-31 02:46:03
【问题描述】:

我的问题是: 我在 EditText 中输入文本,然后显示每个单独的字母,但这可以使用 2 ImageView 显示。 我对 ImageView 没有任何问题,问题是当您在 for 中输入单词时,尝试延迟显示第一个字母并继续显示其他字母,但它不起作用,只需将单词的最后一个字母发送给我你输入

代码:

public class deletreo extends Activity {

    TextView tv;
    EditText etxt;
    ImageView img,img2;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        setContentView(R.layout.deletreo);

        tv = new TextView(this);
        etxt = (EditText)findViewById(R.id.text);
        Button btn = (Button)findViewById(R.id.btn7);

        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                letra();

            }   
        });



    }//fin bundle





     public void letra()  {

         String t = "";

        t = etxt.getText().toString();
        String l = t.toLowerCase();
        int p = l.length();


            try{
                for( int j = 0 ; j < p ; j++){


                    if(l.charAt(j) == 'a' || l.charAt(j) == 'A'){
                        img = (ImageView)findViewById(R.id.img);
                        img.setImageResource(R.drawable.aa);
                        img2 = (ImageView)findViewById(R.id.img2);
                        img2.setImageResource(R.drawable.image_1);

                    }

                        if(l.charAt(j) == 'b' || l.charAt(j) == 'B'){
                            img = (ImageView)findViewById(R.id.img);
                            img.setImageResource(R.drawable.bb);
                            img2 = (ImageView)findViewById(R.id.img2);
                            img2.setImageResource(R.drawable.image_2);

                        }
                            if(l.charAt(j) == 'c' || l.charAt(j) == 'C'){
                                img = (ImageView)findViewById(R.id.img);
                                img.setImageResource(R.drawable.cc);
                                img2 = (ImageView)findViewById(R.id.img2);
                                img2.setImageResource(R.drawable.image_3);

                            }
                                if(l.charAt(j) == 'd' || l.charAt(j) == 'D'){
                                    img = (ImageView)findViewById(R.id.img);
                                    img.setImageResource(R.drawable.dd);
                                    img2 = (ImageView)findViewById(R.id.img2);
                                    img2.setImageResource(R.drawable.image_4);
                                }
                                    if(t.charAt(j) == 'e' || t.charAt(j) == 'E'){
                                        img = (ImageView)findViewById(R.id.img);
                                        img.setImageResource(R.drawable.ee);
                                        img2 = (ImageView)findViewById(R.id.img2);
                                        img2.setImageResource(R.drawable.image_5);
                                    }
                                        if(t.charAt(j) == 'f' || t.charAt(j) == 'F'){
                                            img = (ImageView)findViewById(R.id.img);
                                            img.setImageResource(R.drawable.ff);
                                            img2 = (ImageView)findViewById(R.id.img2);
                                            img2.setImageResource(R.drawable.image_6);
                                        }
                                            if(t.charAt(j) == 'g' || t.charAt(j) == 'G'){
                                                img = (ImageView)findViewById(R.id.img);
                                                img.setImageResource(R.drawable.gg);
                                                img2 = (ImageView)findViewById(R.id.img2);
                                                img2.setImageResource(R.drawable.image_7);
                                            }
                                                if(t.charAt(j) == 'h' || t.charAt(j) == 'H'){
                                                    img = (ImageView)findViewById(R.id.img);
                                                    img.setImageResource(R.drawable.hh);
                                                    img2 = (ImageView)findViewById(R.id.img2);
                                                    img2.setImageResource(R.drawable.image_8);
                                                }
                                                    if(t.charAt(j) == 'i' || t.charAt(j) == 'I'){
                                                        img = (ImageView)findViewById(R.id.img);
                                                        img.setImageResource(R.drawable.ii);
                                                        img2 = (ImageView)findViewById(R.id.img2);
                                                        img2.setImageResource(R.drawable.image_9);
                                                    }
                                                        if(t.charAt(j) == 'j' || t.charAt(j) == 'J'){
                                                            img = (ImageView)findViewById(R.id.img);
                                                            img.setImageResource(R.drawable.jj);
                                                            img2 = (ImageView)findViewById(R.id.img2);
                                                            img2.setImageResource(R.drawable.image_10);
                                                        }
                                                            if(t.charAt(j) == 'k' || t.charAt(j) == 'K'){
                                                                img = (ImageView)findViewById(R.id.img);
                                                                img.setImageResource(R.drawable.kk);
                                                                img2 = (ImageView)findViewById(R.id.img2);
                                                                img2.setImageResource(R.drawable.image_11);
                                                            }
                                                                if(t.charAt(j) == 'l' || t.charAt(j) == 'L'){
                                                                    img = (ImageView)findViewById(R.id.img);
                                                                    img.setImageResource(R.drawable.ll);
                                                                    img2 = (ImageView)findViewById(R.id.img2);
                                                                    img2.setImageResource(R.drawable.image_12);
                                                                }
                                                                    if(t.charAt(j) == 'm' || t.charAt(j) == 'M'){
                                                                        img = (ImageView)findViewById(R.id.img);
                                                                        img.setImageResource(R.drawable.mm);
                                                                        img2 = (ImageView)findViewById(R.id.img2);
                                                                        img2.setImageResource(R.drawable.image_13);
                                                                    }
                                                                        if(t.charAt(j) == 'n' || t.charAt(j) == 'N'){
                                                                            img = (ImageView)findViewById(R.id.img);
                                                                            img.setImageResource(R.drawable.nn);
                                                                            img2 = (ImageView)findViewById(R.id.img2);
                                                                            img2.setImageResource(R.drawable.image_14);
                                                                        }
                                                                            if(t.charAt(j) == 'ñ' || t.charAt(j) == 'Ñ'){
                                                                                img = (ImageView)findViewById(R.id.img);
                                                                                img.setImageResource(R.drawable.nini);
                                                                                img2 = (ImageView)findViewById(R.id.img2);
                                                                                img2.setImageResource(R.drawable.image_15);
                                                                            }
                                                                                if(t.charAt(j) == 'o' || t.charAt(j) == 'O'){
                                                                                    img = (ImageView)findViewById(R.id.img);
                                                                                    img.setImageResource(R.drawable.oo);
                                                                                    img2 = (ImageView)findViewById(R.id.img2);
                                                                                    img2.setImageResource(R.drawable.image_16);
                                                                                }
                                                                                    if(t.charAt(j) == 'p' || t.charAt(j) == 'P'){
                                                                                        img = (ImageView)findViewById(R.id.img);
                                                                                        img.setImageResource(R.drawable.pp);
                                                                                        img2 = (ImageView)findViewById(R.id.img2);
                                                                                        img2.setImageResource(R.drawable.image_17);
                                                                                    }
                                                                                        if(t.charAt(j) == 'q' || t.charAt(j) == 'Q'){
                                                                                            img = (ImageView)findViewById(R.id.img);
                                                                                            img.setImageResource(R.drawable.qq);
                                                                                            img2 = (ImageView)findViewById(R.id.img2);
                                                                                            img2.setImageResource(R.drawable.image_18);
                                                                                        }
                                                                                            if(t.charAt(j) == 'r' || t.charAt(j) == 'R'){
                                                                                                img = (ImageView)findViewById(R.id.img);
                                                                                                img.setImageResource(R.drawable.rr);
                                                                                                img2 = (ImageView)findViewById(R.id.img2);
                                                                                                img2.setImageResource(R.drawable.image_19);
                                                                                            }
                                                                                                if(t.charAt(j) == 's' || t.charAt(j) == 'S'){
                                                                                                    img = (ImageView)findViewById(R.id.img);
                                                                                                    img.setImageResource(R.drawable.ss);
                                                                                                    img2 = (ImageView)findViewById(R.id.img2);
                                                                                                    img2.setImageResource(R.drawable.image_20);
                                                                                                }
                                                                                                    if(t.charAt(j) == 't' || t.charAt(j) == 'T'){
                                                                                                        img = (ImageView)findViewById(R.id.img);
                                                                                                        img.setImageResource(R.drawable.tt);
                                                                                                        img2 = (ImageView)findViewById(R.id.img2);
                                                                                                        img2.setImageResource(R.drawable.image_21);
                                                                                                    }
                                                                                                        if(t.charAt(j) == 'u' || t.charAt(j) == 'U'){
                                                                                                            img = (ImageView)findViewById(R.id.img);
                                                                                                            img.setImageResource(R.drawable.uu);
                                                                                                            img2 = (ImageView)findViewById(R.id.img2);
                                                                                                            img2.setImageResource(R.drawable.image_22);
                                                                                                        }
                                                                                                            if(t.charAt(j) == 'v' || t.charAt(j) == 'V'){
                                                                                                                img = (ImageView)findViewById(R.id.img);
                                                                                                                img.setImageResource(R.drawable.vv);
                                                                                                                img2 = (ImageView)findViewById(R.id.img2);
                                                                                                                img2.setImageResource(R.drawable.image_23);
                                                                                                            }
                                                                                                                if(t.charAt(j) == 'w' || t.charAt(j) == 'W'){
                                                                                                                    img = (ImageView)findViewById(R.id.img);
                                                                                                                    img.setImageResource(R.drawable.ww);
                                                                                                                    img2 = (ImageView)findViewById(R.id.img2);
                                                                                                                    img2.setImageResource(R.drawable.image_24);
                                                                                                                }
                                                                                                                    if(t.charAt(j) == 'x' || t.charAt(j) == 'X'){
                                                                                                                        img = (ImageView)findViewById(R.id.img);
                                                                                                                        img.setImageResource(R.drawable.xx);
                                                                                                                        img2 = (ImageView)findViewById(R.id.img2);
                                                                                                                        img2.setImageResource(R.drawable.image_25);
                                                                                                                    }
                                                                                                                        if(t.charAt(j) == 'y' || t.charAt(j) == 'Y'){
                                                                                                                            img = (ImageView)findViewById(R.id.img);
                                                                                                                            img.setImageResource(R.drawable.yy);
                                                                                                                            img2 = (ImageView)findViewById(R.id.img2);
                                                                                                                            img2.setImageResource(R.drawable.image_26);
                                                                                                                        }
                                                                                                                            if(t.charAt(j) == 'z' || t.charAt(j) == 'Z'){
                                                                                                                                img = (ImageView)findViewById(R.id.img);
                                                                                                                                img.setImageResource(R.drawable.zz);
                                                                                                                                img2 = (ImageView)findViewById(R.id.img2);
                                                                                                                                img2.setImageResource(R.drawable.image_27);

                                                                                                                            }

                            Thread.sleep(2000);                                                                                                                                                                                                 
                }//fin del for

            }//fin try

            catch (InterruptedException e) {
                e.printStackTrace();
            }
            finally{}


     }//fin letra();



}//fin

【问题讨论】:

  • 那是很多代码。您可以使用调试器(甚至日志语句)来缩小问题范围吗?
  • 对于您的巨型级联 if/else 语句,您应该使用 switch 语句。它将更清洁、更高效。
  • 就是这样,如果只是比较在 EditText 中输入的字母,我什至没有通过切换到严重的东西来改变代码在哪里

标签: java android eclipse android-intent for-loop


【解决方案1】:

在您调用的for 循环的底部:

Thread.sleep(2000);

这只会阻塞 UI 线程 2 秒。由于 UI 线程基本上是您的应用程序的生命线,因此上述语句将停止 all 代码的执行。如果您阻塞主线程,循环外的代码将无法神奇地继续运行,这就是为什么您的 UI 在每次迭代后都不会更新的原因。

与其直接与线程交互,不如将延迟的Runnable 发布到Handler 或使用其他一些模拟“滴答”的机制(例如TimerTimerTaskCountDownTimer 来去提醒)。每打勾,您就可以显示下一个字符。

【讨论】:

  • 我尝试如下: public void letra() { String t = ""; t = etxt.getText().toString();字符串 l = t.toLowerCase(); int p = l.length();尝试{ for( int j = 0 ; j
  • @DiegoF:对不起,但我不知道您评论中(相当混乱的)代码发生了什么变化。我仍然可以看到Thread.sleep(2000),这确实是你应该摆脱的。
  • 对不起,我认为它可能看起来不错,我已经在 for 循环结束时完成了 Thread.sleep (2000) 暂停 2 秒,所以它不起作用,是什么我试着做
  • 我不确定您对我的理解是否正确:您应该使用Thread.sleep(),因为在当前设置中它会阻止其他所有内容。请查看implementing a Handler 或搜索我之前评论中提到的其他类。
  • 知道了,没看懂你的意思,我会尝试一些提到的,我会再次回复,谢谢
【解决方案2】:

您需要将此操作放在单独的线程中,然后使用Handler 暂停和恢复您刚刚创建的新线程。这样做是为了不冻结主 UI 线程,这是 Android 上默认运行的东西。

我在谷歌上搜索了“android add delay inbetween changes”并得到了一堆结果,其中一个是:Add a delay to Progress Dialog

另外,使用switch 语句而不是巨大的级联if/else 语句。它会让你的代码变得更干净、更高效。

【讨论】:

  • 有人愿意告诉我为什么我错了,而不是仅仅对我投反对票吗?它对我没有帮助,对 OP 也没有帮助。
  • 我已经尝试了您建议的方式,制作了一个单独的线程,我得到了相同的结果,我尝试了 4 种不同的线程方式,但我无法工作
  • 那么你做错了。谷歌“如何在 android 中实现多线程”。
  • 我更新了我的代码,结果还是一样,只显示单词的最后一个字母,没有延迟,有什么想法吗?我使用了 Timer 和 TimerTask
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-02-12
  • 2021-07-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-09-01
  • 2012-04-07
相关资源
最近更新 更多