-2

私の問題はこれです: EditText にテキストを入力し、個々の文字をそれぞれ表示しますが、2 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
4

2 に答える 2

1

forあなたが呼び出すループの一番下:

Thread.sleep(2000);

これにより、UI スレッドが 2 秒間ブロックされます。基本的に UI スレッドはアプリケーションの生命線であるため、上記のステートメントはコードのすべての実行を停止します。メイン スレッドをブロックすると、ループの外側のコードは魔法のように実行を継続できなくなります。そのため、反復のたびに UI が更新されません。

スレッドと直接やり取りする代わりに、遅延Runnableを aに投稿するHandlerか、「ティック」をエミュレートする他のメカニズムを使用する方がおそらく簡単です (たとえばTimerTimerTaskCountDownTimer思いつく)。ティックごとに、次の文字を表示できます。

于 2012-12-31T03:03:44.760 に答える
0

このアクションを別のスレッドに入れ、aHandlerを使用して、作成したばかりのこの新しいスレッドを一時停止および再開します。これは、メインの UI スレッドをフリーズしないようにするためです。メインの UI スレッドは、Android でデフォルトで実行されるものです。

「android add delay in between changes」をグーグルで検索したところ、一連の結果が得られました。そのうちの1つは次のとおりです。進行状況ダイアログに遅延を追加する

また、switch巨大なカスケードif/elseステートメントの代わりにステートメントを使用します。これにより、コードが非常にクリーンで効率的になります。

于 2012-12-31T03:01:08.653 に答える