1

さて、私の質問はかなり単純です

私のレイアウトには、EditText と 2 つの ImageView があります。

文字に従って画像を表示する必要がある2つのImageViewに任意の単語EditTextを入力し、EditTextの単語として送信して、たとえば1つの文字を呼び出したいときに問題が発生します。

「ホーム」という単語を入力すると、その単語に入力され、文字に従って2つのImageView画像が表示され、C(画像C)、次にA(画像A)が表示されます

問題は、1文字を見つけるプロセスを作成できることです。forで試しましたが、最後の文字しか認識しません。また、一種の遅延(遅延)を作成しようとしましたが、機能しませんでした

私のコードの一部:

public class deletreo extends Activity {

    protected TextView tv;
    protected EditText etxt;
    protected ImageView img,img2;

    final Handler handle = new Handler();

    protected void mth(){
        Thread t = new Thread(){
            public void run(){

                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }
                handle.post(proceso);
            }
        };
        t.start();
    }

    final Runnable proceso = new Runnable(){
        public void run(){
            letra();
        }
    };


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


        etxt = (EditText)findViewById(R.id.text);
        Button btn = (Button)findViewById(R.id.btn7);

        btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {

                mth();

            }   
        });



    }//fin bundle





     private void letra()  {

        String t = etxt.getText().toString();

            char[] array = t.toCharArray();
            int p = array.length;

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

                    if(array[j] == 'a' || array[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);
                        onStop();
                    }

                        if(array[j] == 'b' || array[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);

                        }

この問題を解決する方法はありますか?

4

1 に答える 1

1

問題は、1文字を見つけるプロセスを作成できることです。forで試しましたが、最後の文字しか認識しません。また、一種の遅延(遅延)を作成しようとしましたが、機能しませんでした

もちろん、そのように振る舞います。開始時に1秒の遅延を入れているためletra()、ループ内の両方のimageviewに画像リソースを設定するメソッドを呼び出しています。そのため、最後の文字に関連付けられた画像のみを表示できます。

代わりにこの方法を試してください:

public class deletreo extends Activity {

    protected TextView tv;
    protected EditText etxt;
    protected ImageView img,img2;

    final Handler handle = new Handler();

    protected void mth(){
        Thread t = new Thread(){
            public void run(){

                try{
                    Thread.sleep(1000);
                }catch(InterruptedException e){
                    e.printStackTrace();
                }

            }
        };
        t.start();
    }


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


        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

     private void letra()  {

        String t = etxt.getText().toString();

            char[] array = t.toCharArray();
            int p = array.length;

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

            if(array[j] == 'a' || array[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);
                onStop();
                    }

            if(array[j] == 'b' || array[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);

            }
            mth();
        }
    }
}

ループの各ラウンドの後に遅延関数 mth() を呼び出します。それがうまくいくことを願っています。

于 2012-12-26T09:22:35.583 に答える