我是新来的,如果我犯错误就很抱歉。我也是来自德国,对不好的英语感到抱歉。
我的代码有两个问题。 第一个是整数列表“my”每次获得相同的Integer,第二个问题是当我第二次按下开始按钮时for循环不起作用。 这是我的代码:
public class MainActivity extends AppCompatActivity implements MediaPlayer.OnCompletionListener {
String text;
MediaPlayer mp;
int i;
List<Integer> my=new ArrayList<Integer>();;
int load;
int an=0;
String[] help;
boolean existing=false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText ed=(EditText) findViewById(R.id.editText);
text=ed.getText().toString();
tts();
}
});
Button lös=(Button) findViewById(R.id.button6);
lös.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
EditText ed=(EditText) findViewById(R.id.editText);
ed.setText("");
}
});
}
public void tts(){
if (text.isEmpty()){
Toast.makeText(this,"Gib erstmal was zum aussprechen ein!",Toast.LENGTH_SHORT).show();
}
else{
text = text.replace("A", "a");
text = text.replace("B", "b");
text = text.replace("C", "c");
text = text.replace("D", "d");
text = text.replace("E", "e");
text = text.replace("F", "f");
text = text.replace("G", "g");
text = text.replace("H", "h");
text = text.replace("I", "i");
text = text.replace("J", "j");
text = text.replace("K", "k");
text = text.replace("L", "l");
text = text.replace("M", "m");
text = text.replace("N", "n");
text = text.replace("O", "o");
text = text.replace("P", "p");
text = text.replace("Q", "q");
text = text.replace("R", "r");
text = text.replace("S", "s");
text = text.replace("T", "t");
text = text.replace("U", "u");
text = text.replace("V", "v");
text = text.replace("W", "w");
text = text.replace("X", "x");
text = text.replace("Y", "y");
text = text.replace("Z", "z");
text = text.replace("Ä", "ä");
text = text.replace("Ö", "ö");
text = text.replace("Ü", "ü");
if (text.contains("cks")) {
text = text.replace("cks", "40,");
}
if (text.contains("ieh")) {
text = text.replace("ieh", "8,");
}
if (text.contains("sch")){
text = text.replace("sch", "27,");
}
if (text.contains("chs")){
text = text.replace("chs", "40,");
}
if (text.contains("ie")) {
text = text.replace("ie", "1,");
}
if (text.contains("ih")) {
text = text.replace("ih", "8,");
}
if (text.contains("üh")) {
text = text.replace("üh", "9,");
}
if (text.contains("uh")) {
text = text.replace("uh", "10,");
}
if (text.contains("ee")) {
text = text.replace("ee", "11,");
}
if (text.contains("eh")){
text = text.replace("eh", "11,");
}
if (text.contains("öh")){
text = text.replace("öh", "12,");
}
if (text.contains("oh")){
text = text.replace("oh", "13,");
}
if (text.contains("oo")){
text = text.replace("oo", "13,");
}
if (text.contains("äh")){
text = text.replace("äh", "14,");
}
if (text.contains("ah")){
text = text.replace("ah", "15,");
}
if (text.contains("aa")){
text = text.replace("aa", "15,");
}
if (text.contains("eu")){
text = text.replace("eu", "16,");
}
if (text.contains("äu")){
text = text.replace("äu", "16,");
}
if (text.contains("ei")){
text = text.replace("ei", "17,");
}
if (text.contains("ai")){
text = text.replace("ai", "17,");
}
if (text.contains("au")){
text = text.replace("au", "18,");
}
if (text.contains("pp")){
text = text.replace("pp", "19,");
}
if (text.contains("tt")){
text = text.replace("tt", "20,");
}
if (text.contains("dt")){
text = text.replace("dt", "20,");
}
if (text.contains("th")){
text = text.replace("th", "20,");
}
if (text.contains("ck")){
text = text.replace("ck", "21,");
}
if (text.contains("bb")){
text = text.replace("bb", "22,");
}
if (text.contains("dd")){
text = text.replace("dd", "23,");
}
if (text.contains("gg")){
text = text.replace("gg", "24,");
}
if (text.contains("ff")){
text = text.replace("ff", "25,");
}
if (text.contains("ph")){
text = text.replace("ph", "25,");
}
if (text.contains("ss")){
text = text.replace("ss", "26,");
}
if (text.contains("rr")){
text = text.replace("rr", "30,");
}
if (text.contains("rh")){
text = text.replace("rh", "30,");
}
if (text.contains("ll")){
text = text.replace("ll", "35,");
}
if (text.contains("mm")){
text = text.replace("mm", "36,");
}
if (text.contains("nn")){
text = text.replace("nn", "37,");
}
if (text.contains("ng")){
text = text.replace("ng", "37,");
}
if (text.contains("pf")){
text = text.replace("pf", "39,");
}
if (text.contains("ks")){
text = text.replace("ks", "40,");
}
if (text.contains("gs")){
text = text.replace("gs", "40,");
}
if (text.contains("tz")){
text = text.replace("tz", "41,");
}
if (text.contains("t")){
text = text.replace("t", "20,");
}
if (text.contains("p")){
text = text.replace("p", "19,");
}
if (text.contains("ä")){
text = text.replace("ä", "14,");
}
if (text.contains("c")){
text = text.replace("c", "21,");
}
if (text.contains("b")){
text = text.replace("b", "22,");
}
if (text.contains("k")){
text = text.replace("k", "21,");
}
if (text.contains("d")){
text = text.replace("d", "23,");
}
if (text.contains("ü")){
text = text.replace("ü", "2,");
}
if (text.contains("y")){
text = text.replace("y", "2,");
}
if (text.contains("u")){
text = text.replace("u", "3,");
}
if (text.contains("e")){
text = text.replace("e", "4,");
}
if (text.contains("ä")){
text = text.replace("ä", "4,");
}
if (text.contains("ö")){
text = text.replace("ö", "5,");
}
if (text.contains("o")){
text = text.replace("o", "6,");
}
if (text.contains("a")){
text = text.replace("a", "7,");
}
if (text.contains("g")){
text = text.replace("g", "24,");
}
if (text.contains("s")){
text = text.replace("s", "26,");
}
if (text.contains("f")){
text = text.replace("f", "25,");
}
if (text.contains("v")){
text = text.replace("v", "25,");
}
if (text.contains("ß")){
text = text.replace("ß", "26,");
}
if (text.contains("r")){
text = text.replace("r", "30,");
}
if (text.contains("h")){
text = text.replace("h", "31,");
}
if (text.contains("w")){
text = text.replace("w", "32,");
}
if (text.contains("j")){
text = text.replace("j", "34,");
}
if (text.contains("l")){
text = text.replace("l", "35,");
}
if (text.contains("m")){
text = text.replace("m", "36,");
}
if (text.contains("n")){
text = text.replace("n", "37,");
}
if (text.contains("x")){
text = text.replace("x", "40,");
}
if (text.contains("z")){
text = text.replace("z", "41,");
}
if (text.contains("i")){
text = text.replace("i", "1,");
}
//if (text.contains("q"))text = text.replace("q","42,"); !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
//if (text.contains(" "))text=text.replace(" ","wait");
text = text.substring(0, text.length() - 1); //
help = text.split(",");
for (int in = 0; in < help.length; in++) {
if (help[i].equals("40")) {
an = R.raw.a40;
my.add(an);
}
if (help[i].equals("27")) {
an = R.raw.a27;
my.add(an);
}
if (help[i].equals("1")) {
an = R.raw.a1;
my.add(an);
}
if (help[i].equals("9")) {
an = R.raw.a9;
my.add(an);
}
if (help[i].equals("10")) {
an = R.raw.a10;
my.add(an);
}
if (help[i].equals("11")) {
an = R.raw.a11;
my.add(an);
}
if (help[i].equals("12")) {
an = R.raw.a12;
my.add(an);
}
if (help[i].equals("13")) {
an = R.raw.a13;
my.add(an);
}
if (help[i].equals("14")) {
an = R.raw.a14;
my.add(an);
}
if (help[i].equals("15")) {
an = R.raw.a15;
my.add(an);
}
if (help[i].equals("16")) {
an = R.raw.a16;
my.add(an);
}
if (help[i].equals("17")) {
an = R.raw.a17;
my.add(an);
}
if (help[i].equals("18")) {
an = R.raw.a18;
my.add(an);
}
if (help[i].equals("19")) {
an = R.raw.a19;
my.add(an);
}
if (help[i].equals("20")) {
an = R.raw.a20;
my.add(an);
}
if (help[i].equals("21")) {
an = R.raw.a21;
my.add(an);
}
if (help[i].equals("22")) {
an = R.raw.a22;
my.add(an);
}
if (help[i].equals("23")) {
an = R.raw.a23;
my.add(an);
}
if (help[i].equals("24")) {
an = R.raw.a24;
my.add(an);
}
if (help[i].equals("25")) {
an = R.raw.a25;
my.add(an);
}
if (help[i].equals("26")) {
an = R.raw.a26;
my.add(an);
}
if (help[i].equals("30")) {
an = R.raw.a30;
my.add(an);
}
if (help[i].equals("35")) {
an = R.raw.a35;
my.add(an);
}
if (help[i].equals("36")) {
an = R.raw.a36;
my.add(an);
}
if (help[i].equals("37")) {
an = R.raw.a37;
my.add(an);
}
if (help[i].equals("39")) {
an = R.raw.a39;
my.add(an);
}
if (help[i].equals("40")) {
an = R.raw.a40;
my.add(an);
}
if (help[i].equals("41")) {
an = R.raw.a41;
my.add(an);
}
if (help[i].equals("2")) {
an = R.raw.a2;
my.add(an);
}
if (help[i].equals("3")) {
an = R.raw.a3;
my.add(an);
}
if (help[i].equals("4")) {
an = R.raw.a4;
my.add(an);
}
if (help[i].equals("5")) {
an = R.raw.a5;
my.add(an);
}
if (help[i].equals("6")) {
an = R.raw.a6;
my.add(an);
}
if (help[i].equals("31")) {
an = R.raw.a31;
my.add(an);
}
if (help[i].equals("32")) {
an = R.raw.a32;
my.add(an);
}
if (help[i].equals("34")) {
an = R.raw.a34;
my.add(an);
}
}
i = 0;
mp = MediaPlayer.create(this, my.get(i));
mp.setOnCompletionListener(this);
mp.start();
existing = true;
}
}
@Override
public void onCompletion(MediaPlayer mediaPlayer) {
i++;
try {
load=my.get(i);
}
catch (Exception e){
load= 0;
}
if (load==0){
Toast.makeText(this,"Fertig",Toast.LENGTH_SHORT).show();
mp.release();
}
else {
mp = MediaPlayer.create(this, load);
mp.setOnCompletionListener(this);
mp.start();
}
}
}
答案 0 :(得分:0)
你有
i = 0;
mp = MediaPlayer.create(this, my.get(i));
所以它总是得到元素0一个i只有0(第一个)。
您已在中指定变量作为循环计数器,但在循环中使用了 i ,例如你有
for (int in = 0; in < help.length; in++) {
if (help[i].equals("40")) {
an = R.raw.a40;
my.add(an);
} .........
}
将循环中 i 的每次出现更改为或int in = 0; in < help.length; in++
更改为int i = 0; i < help.length; i++