我对 java 和 android dev 完全陌生,所以希望我能很好地描述我正在努力实现的目标(因此,为什么我的代码中也有这么多评论,请尝试帮助我)。
我在要显示的资产文件夹中有一组图片,当单击 ibnext 按钮时,我希望图片(和相关的 txt 文件)更改为下一张。为简单起见,txt 文件和图片的名称分别为 1.txt 和 1.jpg、2.txt 和 2.jpg 等。
以下是我的代码,它都适用于显示第一个图像和 txt,但我无法达到可以将 1 添加到“progressL1”然后重绘图像和 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 为 ++ 之后如何调用公共 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;
}
}