Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

289
Views
El ciclo while simple no funciona

Tengo un pequeño problema. Creo que la solución es muy simple, pero desafortunadamente no puedo encontrarla. Espero que alguien pueda ayudarme. Tengo un ciclo while que tiene que contar hasta diez y escribir el número en un TextView. Sigue sin funcionar... ¡Gracias por tu ayuda! Aquí está el código:

 package de.androidnewcomer.animation; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.TextView; import static android.R.attr.button; import static de.androidnewcomer.animation.R.id.textView; public class MainActivity extends AppCompatActivity implements View.OnClickListener { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ImageView ball=(ImageView)findViewById(R.id.ball); Button button=(Button)findViewById(R.id.button); button.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.button: count(); break; } } private void count() { TextView textView=(TextView)findViewById(R.id.textView); int i; i=1; while(i<10) { i++; textView.setText(i); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } } }
over 4 years ago · Santiago Trujillo
3 answers
Answer question

0

El uso de setText() con un valor entero es para establecer una referencia de recurso de cadena. Para configurar el texto en sí, debe proporcionar una cadena: Use setText("" + i); y debería funcionar.

over 4 years ago · Santiago Trujillo Report

0

textView.setText(..) necesita un objeto de cadena, pero usa un int. Tienes que convertir tu int en una cadena con las siguientes opciones posibles:

  1. Puede usar String.valueOf(i) : textView.setText(String.valueOf(i));

  2. Puede usar Integer.toString(i) : textView.setText(Integer.toString(i));

  3. Puede usar el literal de cadena vacía : textView.setText("" + i);

Prefiero la última opción. Con su código, debería verse como el siguiente código:

 private void count() { TextView textView=(TextView)findViewById(R.id.textView); int i; i=1; while(i<10) { i++; textView.setText("" + i); try { Thread.sleep(200); } catch (InterruptedException e) { e.printStackTrace(); } } }

Puede usar un bucle for en lugar de un bucle while, como el siguiente código:

 private void count() { TextView textView=(TextView)findViewById(R.id.textView); for(int i = 1; i < 11; i++){ // From 1 to 10 textView.setText("" + i); Thread.sleep(200); } }
over 4 years ago · Santiago Trujillo Report

0

Use una cuenta regresiva porque está bloqueando el hilo principal

 CountDownTimer countDownTimer = new CountDownTimer(2000 /*amount*/, 200/*step*/) { public void onTick(long millisUntilFinished) { textView.setText("what ever you want"); } public void onFinish() { textView.setText("Done"); } }; countDownTimer.start();
over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!