0

私はJavaとAndroid開発にまったく慣れていないので、私が達成しようとしていることを十分に説明できることを願っています(したがって、私のコードにも非常に多くのコメントがあるのはなぜですか、試してみてください)。

画像 (および関連付けられている txt ファイル) を次の画像に変更したい ibnext ボタンをクリックすると、表示したい一連の画像がアセット フォルダーに含まれています。簡単にするために、txt ファイルと画像の名前は 1.txt & 1.jpg、2.txt & 2.jpg などです。

以下は私のコードで、最初の画像とtxtを表示するためにすべて機能しますが、「progressL1」に1を追加してから画像とtxtを再描画できるようにはなりません。非最終変数とグローバル変数を検索するループに陥っていますが、これはあまり意味がありません。

よろしくお願いします。初心者の質問で申し訳ありませんが、これで途方に暮れています...

public class Lessonsinglegroup extends Activity implements OnInitListener {


//define the image and text view for use later
ImageView Image;
TextView Text;

//define the texttospeak stuff

private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lessonsinglegroup);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //link the image and text boxes to the xml
    Image = (ImageView)findViewById(R.id.image);
    Text = (TextView)findViewById(R.id.text);
    loadDataFromAsset(progressL1);

    //finish with the asset load

    //define tts stuff
    inputText = (TextView) findViewById(R.id.text);
    speakButton = (ImageButton) findViewById(R.id.ibtalk);
    nextButton = (ImageButton) findViewById(R.id.ibnext);


    //start the speech stuff
    speakButton.setOnClickListener(new View.OnClickListener() {

    public void onClick(View v) {
        String text = inputText.getText().toString();
        if (text!=null && text.length()>0) {
            tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        }
    }
    });

    Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);


        }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        //sucess with TTS create it
        tts = new TextToSpeech(this, this);
        }
    else {
        //missing TTS so install it
        Intent installIntent = new Intent();
        installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
    }
}


}

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_LONG).show();
            }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech",     Toast.LENGTH_LONG).show();
    }
}

//actually load the text file and image file   
public void loadDataFromAsset(int progressL1) {
    //load the asset files themselves
    try {
        InputStream is = getAssets().open(progressL1 + ".txt");
        //check file size
        int size = is.available();
        //create a buffer to handle it
        byte[] buffer = new byte[size];
        //send the data to the buffer
        is.read(buffer);
        //close the stream down
        is.close();
        //set the text we recovered to the TextView
        Text.setText(new String(buffer));
    }
    catch (IOException ex) {
        return;
    }

    //image file next
    try {
        InputStream ims = getAssets().open(progressL1 + ".jpg");
        //load the image as drawable
        Drawable d = Drawable.createFromStream(ims,  null);
        //set the drawable image to the imageview
        Image.setImageDrawable(d);
    }
    catch (IOException ex) {
        return;
            }





//end of image and text file loading.

//when next is clicked start doing this
nextButton.setOnClickListener(new View.OnClickListener() {


      public void onClick(View v) {
          try {
              progressL1++;
              Lessonsinglegroup.loadDataFromAsset();
          }
          catch (IOException ex) {
                return;
                    }
      }   
          });
      }

================================================== ================================================== ================================編集================ =============================

いくつかのきちんとしたコードで、私は自分が望むものに近づいていると思いますが、progressL1 が ++ になった後に public loadDataFromAsset セクションを呼び出す方法がまだわかりません。

このコードの方が良いと思いますか?誰か考えますか?

public class Lessonsinglegroup extends Activity implements OnClickListener, OnInitListener {
//old version did not have OnClcikListener in it here-------------->

//define the image and text view for use later
ImageView Image;
TextView Text;

//define the texttospeak stuff

private int MY_DATA_CHECK_CODE = 0;
public int progressL1 = 0;
private TextView inputText;
private TextToSpeech tts;
private ImageButton speakButton;
private ImageButton nextButton;


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_lessonsinglegroup);
    getActionBar().setDisplayHomeAsUpEnabled(true);

    //link the image and text boxes to the xml
    Image = (ImageView)findViewById(R.id.image);
    Text = (TextView)findViewById(R.id.text);
    loadDataFromAsset(progressL1);

    //finish with the asset load

    //define tts stuff
    inputText = (TextView) findViewById(R.id.text);
    speakButton = (ImageButton) findViewById(R.id.ibtalk);
    nextButton = (ImageButton) findViewById(R.id.ibnext);


    //start the speech stuff
//    speakButton.setOnClickListener(new View.OnClickListener() {

    //new onclick listener style

    speakButton.setOnClickListener(this);
    nextButton.setOnClickListener(this);

}
    public void onClick(View v) {
        if (v==nextButton) {
            progressL1++;
            Toast.makeText(Lessonsinglegroup.this, "progress +1", Toast.LENGTH_LONG).show();
        //HERE IS WHERE I AM CONFUSED HOW TO CALL FORWARD-------<<<<<<  

        }

        if (v==speakButton){

        String text = inputText.getText().toString();
        if (text!=null && text.length()>0) {
            tts.speak(text, TextToSpeech.QUEUE_ADD, null);
        }

    };

    Intent checkIntent = new Intent();
        checkIntent.setAction(TextToSpeech.Engine.ACTION_CHECK_TTS_DATA);
        startActivityForResult(checkIntent, MY_DATA_CHECK_CODE);


        }
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == MY_DATA_CHECK_CODE) {
    if (resultCode == TextToSpeech.Engine.CHECK_VOICE_DATA_PASS) {
        //sucess with TTS create it
        tts = new TextToSpeech(this, this);
        }
    else {
        //missing TTS so install it
        Intent installIntent = new Intent();
           installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
        startActivity(installIntent);
    }
}


}

public void onInit(int status) {
    if (status == TextToSpeech.SUCCESS) {
        Toast.makeText(Lessonsinglegroup.this, "Text to Speech initialised", Toast.LENGTH_LONG).show();
            }
    else if (status == TextToSpeech.ERROR) {
        Toast.makeText(Lessonsinglegroup.this, "Error starting Text to Speech", Toast.LENGTH_LONG).show();
    }
}

//actually load the text file and image file   
public void loadDataFromAsset(int progressL1) {
    //load the asset files themselves
    try {
        InputStream is = getAssets().open(progressL1 + ".txt");
        //check file size
        int size = is.available();
        //create a buffer to handle it
        byte[] buffer = new byte[size];
        //send the data to the buffer
        is.read(buffer);
        //close the stream down
        is.close();
        //set the text we recovered to the TextView
        Text.setText(new String(buffer));
    }
    catch (IOException ex) {
        return;
    }

    //image file next
    try {
        InputStream ims = getAssets().open(progressL1 + ".jpg");
        //load the image as drawable
        Drawable d = Drawable.createFromStream(ims,  null);
        //set the drawable image to the imageview
        Image.setImageDrawable(d);
    }
    catch (IOException ex) {
        return;
            }







      }
4

1 に答える 1

0

あなたの質問はまだ非常に不明確です。「onClick内からloadDataFromAssetを呼び出すにはどうすればよいですか?」という意味だと思います。

onClick は「匿名の内部クラス」です。外側のクラスである Lessonsinglegroup のメソッドを呼び出すには、次のようにします。

Lessonsinglegroup.this.loadDataFromAsset()
于 2012-11-04T12:04:21.680 に答える