2016-12-11 2 views
0

Tout fonctionne correctement, à l'exception des TextView et EditText qui ne se mettent à jour que lorsque la torche de la caméra cesse de clignoter. Le programme est censé clignoter en code morse (ce qu'il fait) mettre en surbrillance le texte comme il arrive à la lettre et afficher la lettre en cours dans le code morse.EditText et Text View ne sont mis à jour qu'une seule fois lorsque la boucle for est terminée au lieu de plusieurs fois

public class TextFlashActivity extends AppCompatActivity { 

private TextView textView; 
private String morseString; 
private String text; 
private String iLearnedWhatImmutableMeans; 
private EditText editText; 
public static HashMap morseCode; 
public static Camera cam = null; 
public static Camera.Parameters parameters = null; 
public static Boolean isLightOn = false; 
private Button button; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.text_flash_layout); 
    init(); 
    button = (Button) findViewById(R.id.Button_flash); 
    button.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      morseTextFlash(); 
     } 
    }); 
} 

@Override 
protected void onPause() { 
    super.onPause(); 
    cam.release(); 
} 


private void morseTextFlash() { 

    text = editText.getText().toString(); 
    Spannable WordtoSpan = new SpannableString(text); 
    iLearnedWhatImmutableMeans = text.toLowerCase(); 
    for (int i = 0; i < text.length(); i++) { 
     WordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 0, i + 1, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); 
     editText.setText(WordtoSpan); 
     textView.setText((String) morseCode.get(iLearnedWhatImmutableMeans.charAt(i))); 
     if (iLearnedWhatImmutableMeans.charAt(i) != ' ') 
      morseString = (String) morseCode.get(iLearnedWhatImmutableMeans.charAt(i)); 
     for (int j = 0; j < morseString.length(); j++) { 
      try { 
       Thread.sleep(1000); 
      } catch (InterruptedException e) { 
       e.printStackTrace(); 
      } 
      if (morseString.charAt(j) == '.') { 
       try { 
        flashSwitch(); 
        Thread.sleep(100); 
        flashSwitch(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } else { 
       try { 
        flashSwitch(); 
        Thread.sleep(400); 
        flashSwitch(); 
       } catch (InterruptedException e) { 
        e.printStackTrace(); 
       } 
      } 
     } 
    } 
} 

private void init() { 
    textView = (TextView) findViewById(R.id.TV_flash); 
    editText = (EditText) findViewById(R.id.ET_flash); 
    cam = Camera.open(); 
    parameters = cam.getParameters(); 
    cam.startPreview(); 
    hashMapInit(); 
} 


private void flashSwitch() { 
    if (isLightOn) { 
     parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); 
     cam.setParameters(parameters); 
     isLightOn = false; 
    } else { 
     parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); 
     cam.setParameters(parameters); 
     isLightOn = true; 
    } 
} 


private void hashMapInit() { 
    morseCode = new HashMap<Character, String>(); 
    morseCode.put('a', ".-"); 
    morseCode.put('b', "-..."); 
    morseCode.put('c', "-.-"); 
    morseCode.put('d', "-.."); 
    morseCode.put('e', "."); 
    morseCode.put('f', "..-."); 
    morseCode.put('g', "--."); 
    morseCode.put('h', "...."); 
    morseCode.put('i', ".."); 
    morseCode.put('j', ".---"); 
    morseCode.put('k', "-."); 
    morseCode.put('l', ".-.."); 
    morseCode.put('m', "--"); 
    morseCode.put('n', "-."); 
    morseCode.put('o', "---"); 
    morseCode.put('p', ".--."); 
    morseCode.put('q', "--.-"); 
    morseCode.put('r', ".-."); 
    morseCode.put('s', "..."); 
    morseCode.put('t', "-"); 
    morseCode.put('u', "..-"); 
    morseCode.put('v', "...-"); 
    morseCode.put('w', ".--"); 
    morseCode.put('x', "-..-"); 
    morseCode.put('y', "-.--"); 
    morseCode.put('z', "--.."); 
    morseCode.put('1', ".----"); 
    morseCode.put('2', "..---"); 
    morseCode.put('3', "...--"); 
    morseCode.put('4', "....-"); 
    morseCode.put('5', "....."); 
    morseCode.put('6', "-...."); 
    morseCode.put('7', "--..."); 
    morseCode.put('8', "---.."); 
    morseCode.put('9', "----."); 
    morseCode.put('0', "-----"); 
} 

}

Répondre

0

Ne jamais utiliser Thread.sleep lors de l'exécution sur le main thread, ce bloque tous les changements de l'interface utilisateur sur l'écran, et rend l'application qui ne répond pas au toucher.

Vous pouvez utiliser à la place le Handler qui vient intégré avec toutes les vues dans Android, quelque chose comme ceci:

editText.setText("hello"); 
editText.postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      editText.setText("hello again"); 
      // schedule the next change here 
     } 
}, 1000); 

En savoir plus sur postDelayed ici: https://developer.android.com/reference/android/view/View.html#postDelayed(java.lang.Runnable, long)