简体   繁体   中英

Repeated text in the TextView to the pressure of the Button

I have this code that is executed at the touch of a Button.

 Button myButton = (Button) view.findViewById(R.id.button01);
        myButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


class = new MyClass();
class.Method();
if(class.Method()) {

    TextView.append(Html.fromHtml((getString(R.string.text01))));

}
else {

    TextView.append(Html.fromHtml((getString(R.string.text02))));

}


try {
    if (class.Method2() && (class.Method3()))
    {
        TextView.append(Html.fromHtml((getString(R.string.text03))));

    }

    else {
        TextView.append(Html.fromHtml((getString(R.string.text04))));

    }
}
catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

try {
    if(class.Method4()) {

        TextView.append(Html.fromHtml((getString(R.string.text05))));

    }
    else {

        TextView.append(Html.fromHtml((getString(R.string.text06))));

    }
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}
            }
        });

If the methods return true, and I press the button two or more times I visualize the text in the TextView repeated many times depending on how many times I pressed the button.

So if the text of text01 is "Example", text03 "Example2" and text05 "Example3" and I press twice on the button, the result is

Example Example2 example3 Example Example2 example3

Why? How can I fix?

您需要使用TextView.setText("parameter")代替TextView.append();

Considering you're using the append method, which will keep appending text each time you press the button, I'm not surprised by these results. If you want to add the text only once you need some way to keep track of either which button has been pressed or what text has been added to the TextView . Depending on how many buttons you have one might be easier than the other.

Instead of append I would use a regular string that I concat the text on to. It looks like you're using fixed strings. This means it should be easy to check what you have added and what you have not. I'd probably do something like this

String concatResult = "";
//button onClick set up goes here

if(class.Method()){
    String stringToAdd = getStringFromResources(); //get appropriate string
    if(!concatResult.contains(stringToAdd){
        concatResult += stringToAdd;
    } 
}

//when you're done with the tests for which string to add
textView.setText(concatResult);

You'll have to play with the spacing of what's added (I don't know how the text is formatted) but this approach should allow you to add text once for each button being pressed. Just using setText within each conditional will set the text of the TextView only to that string, it will not accommidate the additional pressing of other buttons, which looking at your sample code is what you want to do.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM