/ Published in: Java
Expand |
Embed | Plain Text
Copy this code and paste it in your HTML
package com.test.NewClass; import java.util.Random; import android.app.Activity; import android.app.Dialog; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.text.Editable; import android.text.InputFilter; import android.text.Spannable; import android.text.TextWatcher; import android.text.style.BackgroundColorSpan; import android.util.Log; import android.view.KeyEvent; import android.view.View; import android.view.Window; import android.view.View.OnClickListener; import android.view.inputmethod.InputMethodManager; import android.widget.Button; import android.widget.Chronometer; import android.widget.EditText; import android.widget.ImageView; import android.widget.TextView; import android.widget.Toast; import com.test.NewClass.R; public class Qualify extends Activity implements TextWatcher{ SoundManager mSoundManager; private EditText _edit; private TextView _text; private Chronometer _durationText; private Spannable _spanText; private ImageView backBtn; private SharedPreferences _pref; private InputMethodManager imm; private int _min,_sec; private float _totalTime,_wpm; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.qualify); mSoundManager = new SoundManager(); mSoundManager.initSounds(getBaseContext()); mSoundManager.addSound(1, R.raw.sound); //_pref.getFloat(Qualify1,0.0); _durationText = (Chronometer)findViewById(R.id.durationTextVal); _durationText.start(); _text = (TextView)findViewById(R.id.text1); int val = random.nextInt(15); _text.setText(ThumbsUpData._text.get(val)); _spanText = (Spannable) _text.getText(); _edit = (EditText)findViewById(R.id.edit1); imm = (InputMethodManager) if (imm != null){ imm.toggleSoftInput(InputMethodManager.SHOW_FORCED,0); } _preText = _text.getText().toString(); _edit.addTextChangedListener(this); backBtn=(ImageView)findViewById(R.id.backbtnqualify); backBtn.setOnClickListener(new OnClickListener(){ @Override if (imm != null){ imm.hideSoftInputFromWindow(_edit.getWindowToken(),0); } Qualify.this.finish(); }}); } @Override public void afterTextChanged(Editable arg0) { // TODO Auto-generated method stub } @Override public void beforeTextChanged(CharSequence s, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(_edit.length() == _preText.length()){ InputFilter[] FilterArray = new InputFilter[1]; FilterArray[0] = new InputFilter.LengthFilter(_preText.length()); _edit.setFilters(FilterArray); _durationText.stop(); _totalTime = (40/((float)(_min + _sec))) * 60; TextView _error,_elspedTime,_worldCount; _dialog.setContentView(R.layout.qualifyresult); _error = (TextView)_dialog.findViewById(R.id.errorVal); _error.setText(""+countError()); _elspedTime = (TextView)_dialog.findViewById(R.id.elaspedTimeVal); _elspedTime.setText(_durationText.getText()); _worldCount = (TextView)_dialog.findViewById(R.id.wpmVal); if(_wpm < 0) _worldCount.setText("0"); else _worldCount.setText(""+_wpm); _dialog.show(); _goround2.setOnClickListener(new OnClickListener(){ @Override Editor _edit = _pref.edit(); _edit.putFloat("Qualify1",_wpm); _edit.commit(); Intent i=new Intent(Qualify.this,Qualify1.class); startActivity(i); _dialog.cancel(); Qualify.this.finish(); }}); } if(s.length() < _preText.length()){ int index = s.length() - 1; if(before == 0){ if(s.charAt(index) == _preText.charAt(index)){ _spanText.setSpan(new BackgroundColorSpan(0xff00ff00), index,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } else{ _spanText.setSpan(new BackgroundColorSpan(0xFFFF0000), s.length() - 1,s.length(),Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); mSoundManager.playSound(1); } } else{ _spanText.setSpan(new BackgroundColorSpan(0xFFFFFF), s.length(),s.length() + 1,Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); } } else{ } } private int countError(){ int _errorCounter = 0; for(int i = 0 ; i < _textViewText.length();i++){ if(_textViewText.charAt(i) != _editViewText.charAt(i)){ _errorCounter++; } } return _errorCounter; } @Override protected void onStop() { super.onStop(); } }