editText and TextView On FrameLayout


/ Published in: Java
Save to your folder(s)



Copy this code and paste it in your HTML
  1. package com.test.NewClass;
  2.  
  3. import java.util.Random;
  4.  
  5.  
  6. import android.app.Activity;
  7. import android.app.Dialog;
  8. import android.content.Context;
  9. import android.content.Intent;
  10. import android.content.SharedPreferences;
  11. import android.content.SharedPreferences.Editor;
  12. import android.os.Bundle;
  13. import android.text.Editable;
  14. import android.text.InputFilter;
  15. import android.text.Spannable;
  16. import android.text.TextWatcher;
  17. import android.text.style.BackgroundColorSpan;
  18. import android.util.Log;
  19. import android.view.KeyEvent;
  20. import android.view.View;
  21. import android.view.Window;
  22. import android.view.View.OnClickListener;
  23. import android.view.inputmethod.InputMethodManager;
  24. import android.widget.Button;
  25. import android.widget.Chronometer;
  26. import android.widget.EditText;
  27. import android.widget.ImageView;
  28. import android.widget.TextView;
  29. import android.widget.Toast;
  30. import com.test.NewClass.R;
  31.  
  32. public class Qualify extends Activity implements TextWatcher{
  33.  
  34. SoundManager mSoundManager;
  35. private EditText _edit;
  36. private TextView _text;
  37. private String _preText;
  38. private Chronometer _durationText;
  39. private Spannable _spanText;
  40. private ImageView backBtn;
  41. private Button _goround2;
  42. private SharedPreferences _pref;
  43. private String PREFNAME = "ThumbUpChamp";
  44. private InputMethodManager imm;
  45. private int _min,_sec;
  46. private float _totalTime,_wpm;
  47.  
  48. @Override
  49. protected void onCreate(Bundle savedInstanceState) {
  50. super.onCreate(savedInstanceState);
  51. requestWindowFeature(Window.FEATURE_NO_TITLE);
  52. setContentView(R.layout.qualify);
  53. mSoundManager = new SoundManager();
  54. mSoundManager.initSounds(getBaseContext());
  55. mSoundManager.addSound(1, R.raw.sound);
  56. _pref = getSharedPreferences(PREFNAME,Context.MODE_PRIVATE);
  57. //_pref.getFloat(Qualify1,0.0);
  58. _durationText = (Chronometer)findViewById(R.id.durationTextVal);
  59. _durationText.start();
  60. _text = (TextView)findViewById(R.id.text1);
  61.  
  62. Random random = new Random();
  63. int val = random.nextInt(15);
  64.  
  65. _text.setText(ThumbsUpData._text.get(val));
  66. _spanText = (Spannable) _text.getText();
  67. _edit = (EditText)findViewById(R.id.edit1);
  68.  
  69. imm = (InputMethodManager)
  70. Qualify.this.getSystemService(Context.INPUT_METHOD_SERVICE);
  71.  
  72. if (imm != null){
  73. imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0);
  74. }
  75. _preText = _text.getText().toString();
  76. _edit.addTextChangedListener(this);
  77.  
  78. backBtn=(ImageView)findViewById(R.id.backbtnqualify);
  79. backBtn.setOnClickListener(new OnClickListener(){
  80.  
  81. @Override
  82. public void onClick(View arg0) {
  83. if (imm != null){
  84. imm.hideSoftInputFromWindow(_edit.getWindowToken(),0);
  85. }
  86. Qualify.this.finish();
  87. }});
  88. }
  89.  
  90. @Override
  91. public void afterTextChanged(Editable arg0) {
  92. // TODO Auto-generated method stub
  93.  
  94. }
  95.  
  96. @Override
  97. public void beforeTextChanged(CharSequence s, int arg1, int arg2,
  98. int arg3) {
  99. // TODO Auto-generated method stub
  100.  
  101. }
  102.  
  103. @Override
  104. public void onTextChanged(CharSequence s, int start, int before, int count) {
  105.  
  106. if(_edit.length() == _preText.length()){
  107.  
  108. InputFilter[] FilterArray = new InputFilter[1];
  109. FilterArray[0] = new InputFilter.LengthFilter(_preText.length());
  110. _edit.setFilters(FilterArray);
  111. _durationText.stop();
  112.  
  113. _min = Integer.parseInt(_durationText.getText().toString().substring(0,2)) * 60;
  114. _sec = Integer.parseInt(_durationText.getText().toString().substring(3,5));
  115.  
  116. _totalTime = (40/((float)(_min + _sec))) * 60;
  117. _wpm = Math.round(_totalTime - countError());
  118.  
  119.  
  120. TextView _error,_elspedTime,_worldCount;
  121. final Dialog _dialog = new Dialog(Qualify.this,android.R.style.Theme_Translucent_NoTitleBar);
  122. _dialog.setContentView(R.layout.qualifyresult);
  123.  
  124. _error = (TextView)_dialog.findViewById(R.id.errorVal);
  125. _error.setText(""+countError());
  126. _elspedTime = (TextView)_dialog.findViewById(R.id.elaspedTimeVal);
  127. _elspedTime.setText(_durationText.getText());
  128. _worldCount = (TextView)_dialog.findViewById(R.id.wpmVal);
  129. if(_wpm < 0)
  130. _worldCount.setText("0");
  131. else
  132. _worldCount.setText(""+_wpm);
  133. _dialog.show();
  134. _goround2=(Button)_dialog.findViewById(R.id.round2);
  135. _goround2.setOnClickListener(new OnClickListener(){
  136.  
  137. @Override
  138. public void onClick(View v) {
  139.  
  140. Editor _edit = _pref.edit();
  141. _edit.putFloat("Qualify1",_wpm);
  142. _edit.commit();
  143. Intent i=new Intent(Qualify.this,Qualify1.class);
  144. startActivity(i);
  145. _dialog.cancel();
  146. Qualify.this.finish();
  147.  
  148. }});
  149.  
  150. }
  151. if(s.length() < _preText.length()){
  152.  
  153. int index = s.length() - 1;
  154.  
  155. if(before == 0){
  156.  
  157. if(s.charAt(index) == _preText.charAt(index)){
  158.  
  159. _spanText.setSpan(new BackgroundColorSpan(0xff00ff00), index,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  160. }
  161. else{
  162. _spanText.setSpan(new BackgroundColorSpan(0xFFFF0000), s.length() - 1,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  163. mSoundManager.playSound(1);
  164. }
  165. }
  166. else{
  167.  
  168. _spanText.setSpan(new BackgroundColorSpan(0xFFFFFF), s.length(),s.length() + 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  169. }
  170. }
  171. else{
  172.  
  173. }
  174. }
  175.  
  176. private int countError(){
  177.  
  178. int _errorCounter = 0;
  179.  
  180. String _textViewText = _text.getText().toString();
  181. String _editViewText = _edit.getText().toString();
  182.  
  183. for(int i = 0 ; i < _textViewText.length();i++){
  184.  
  185. if(_textViewText.charAt(i) != _editViewText.charAt(i)){
  186.  
  187. _errorCounter++;
  188. }
  189.  
  190. }
  191. return _errorCounter;
  192.  
  193. }
  194.  
  195. @Override
  196. protected void onStop() {
  197. super.onStop();
  198. }
  199.  
  200. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.