我对android很新,所以请原谅我的无知或无能。
我正在创建一个存储在数组中并通过TextView使用.setText显示的语录(引号)应用程序。
我目前正在努力选择并显示随机说法并播放其相应的音频。我现在正在寻找使用" Next"和"返回"按钮循环播放一系列的谚语,但一直在努力做到这一点。
我想我需要找到当前显示的说法的索引值并将其递增或递减1?
任何帮助都会很棒。感谢。
这是我到目前为止所做的:
public class Sayings extends Activity implements OnClickListener {
HashMap<String, Integer> sayingsResIds = new HashMap<String, Integer>();
MediaPlayer mp;
String[] sayings = {"Saying1", "Saying2", "Saying3", "Saying4", "Saying5"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sayings);
Animation anim;
anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in);
TextView sayingsTextView = (TextView) findViewById(R.id.displaySayings);
sayingsTextView.startAnimation(anim);
Button btnRandom = (Button) findViewById(R.id.random_button);
Button btnNext = (Button) findViewById(R.id.next_button);
Button btnBack = (Button) findViewById(R.id.back_button);
btnRandom.setOnClickListener(this);
btnNext.setOnClickListener(this);
btnBack.setOnClickListener(this);
sayingsResIds.put("Saying1",R.raw.saying1);
sayingsResIds.put("Saying2",R.raw.saying2);
sayingsResIds.put("Saying3",R.raw.saying3);
sayingsResIds.put("Saying4",R.raw.saying4);
sayingsResIds.put("Saying5",R.raw.saying5);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.sayings, menu);
return true;
}
@Override
public void onClick(View v) {
Animation anim;
TextView sayingsTextView = (TextView) findViewById(R.id.displaySayings);
anim = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.fade_in2);
int resId = 0;
switch (v.getId()) {
case R.id.random_button:
String randomSaying = (sayings[new Random().nextInt(sayings.length)]);
sayingsTextView.setText('"' + randomSaying + '"');
resId = sayingsResIds.get(randomSaying);
sayingsTextView.startAnimation(anim);
break;
case R.id.next_button:
// Click Next to find next saying in the array
break;
case R.id.back_button:
// Click Back to find the previous saying in the array
break;
}
if (mp != null) {
mp.release();
}
mp = MediaPlayer.create(this, resId);
mp.start();
}
答案 0 :(得分:2)
首先,您需要在类中声明一个字段来跟踪当前的说法(引号)。
Integer current;
您还必须从onClick移动以下语句并将其声明为类中的字段。
Integer resId = 0;
然后修改onCreate方法以使代码显示在下面
@Override
protected void onCreate(Bundle savedInstanceState) {
//After buttons being declared
current = 0;
btnBack.setEnabled(false);
btnNext.setEnabled(true);
String randomSaying = (sayings[current]);
sayingsTextView.setText('"' + randomSaying + '"');
resId = sayingsResIds.get(randomSaying);
sayingsTextView.startAnimation(anim);
//Rest of your code
}
现在您可以按如下方式修改onClick代码
@Override
public void onClick(View v) {
//your code
switch (v.getId()) {
case R.id.random_button:
current = new Random().nextInt(sayings.length);
String randomSaying = (sayings[current]);
sayingsTextView.setText('"' + randomSaying + '"');
resId = sayingsResIds.get(randomSaying);
sayingsTextView.startAnimation(anim);
if(current == 0){
btnBack.setEnabled(false);
btnNext.setEnabled(true);
} else if(current == sayings.length - 1){
btnBack.setEnabled(true);
btnNext.setEnabled(false);
} else {
btnBack.setEnabled(true);
btnNext.setEnabled(true);
}
break;
case R.id.next_button:
// Click Next to find next saying in the array
if(current == 0){
btnBack.setEnabled(true);
}
current++;
String randomSaying = (sayings[current]);
sayingsTextView.setText('"' + randomSaying + '"');
resId = sayingsResIds.get(randomSaying);
sayingsTextView.startAnimation(anim);
if(current == sayings.length - 1){
btnNext.setEnabled(false);
}
break;
case R.id.back_button:
// Click Next to find next saying in the array
if(current == sayings.length - 1){
btnNext.setEnabled(true);
}
current--;
String randomSaying = (sayings[current]);
sayingsTextView.setText('"' + randomSaying + '"');
resId = sayingsResIds.get(randomSaying);
sayingsTextView.startAnimation(anim);
if(current == 0){
btnBack.setEnabled(false);
}
break;
}
// your code
}
让我知道它是否有效!
修改强>
这是概念