Could we define unknown amount of clickable texts which have different outputs in for loop?

147 views Asked by At

I want all ClickableSpans to have toasts show their own text. But whatever I write in Toast in onClick in for loop, all ClickableSpans show same Toast output. Briefly, I just want ClickableSpans to show different Toasts in for loop. How can I do it? I want all words have clickable. And when all words are clicked, they do different thing. Thank you for your help.

public class MainActivity extends AppCompatActivity {
private String[] textArray;
    private String text = "1981 senesinde kuantum bilgisayar fikrini Paul Beniof, Max Planck’ın " +
            "enerjinin sürekli olmadan kesikli değerlerde yer alan m, n, k enerji kuantlarıyla ";

    private Chronometer chronometer;

    private int hold=0;
    private TextView textView;
    private int i;

    private MediaPlayer player;
    private long pauseOffset;
    private boolean running;
    private boolean start=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textArray = text.split(" ");
        final SpannableString ss = new SpannableString(text);
        chronometer = findViewById(R.id.chronometer);
        chronometer.setFormat("Time: %s");
        chronometer.setBase(SystemClock.elapsedRealtime());

        textView = findViewById(R.id.text_view);
        textView.setText(text);
        textView.setTextColor(Color.BLACK);

        final ClickableSpan[] clickableSpans = new ClickableSpan[textArray.length];
        for(i = 0; i<textArray.length; i++){

            clickableSpans[i] = new ClickableSpan() {
                @Override
                public void onClick(View widget) {
                    Toast.makeText(MainActivity.this, "", Toast.LENGTH_LONG).show();
                }

                @Override
                public void updateDrawState(TextPaint ds) {
                    super.updateDrawState(ds);
                    ds.setColor(Color.BLACK);
                    ds.setUnderlineText(false);
                }


            };

            final boolean[] c = {true};

            chronometer.setOnChronometerTickListener(new Chronometer.OnChronometerTickListener() {
                @Override
                public void onChronometerTick(Chronometer chronometer) {

                    if ((SystemClock.elapsedRealtime() - chronometer.getBase()) >= 5000&& c[0]) {
                        for(int i=0; i<textArray.length;i++){
                            ss.setSpan(clickableSpans[i], hold, hold + textArray[i].length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                            hold+=textArray[i].length();
                            hold++;
                        }
                        start=false;
                        hold=0;
                        textView.setText(ss);
                        textView.setMovementMethod(LinkMovementMethod.getInstance());
                        if (running) {
                            chronometer.stop();
                            pauseOffset = SystemClock.elapsedRealtime() - chronometer.getBase();
                            running = false;
                        }
                        c[0]=false;
                    }
                }
            });
            hold=0;
        }
        i=0;

    }

    public void play(View v) {
        if(start){
            if (!running) {
                chronometer.setBase(SystemClock.elapsedRealtime() - pauseOffset);
                chronometer.start();
                running = true;
            }
        }
    }

    public void stop(View v) {
        Intent intent = new Intent(MainActivity.this, MainActivity.class);
        startActivity(intent);
        finish();
    }
}
0

There are 0 answers