Return to Snippet

Revision: 45741
at May 6, 2011 17:38 by arunmishra07


Initial Code
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 String _preText;
	private Chronometer _durationText;
	private Spannable _spanText;
	private ImageView backBtn;
	private Button _goround2;
	private SharedPreferences _pref;
	private String PREFNAME = "ThumbUpChamp";
    private InputMethodManager imm;
    private int _min,_sec;
	private float _totalTime,_wpm;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.qualify);
		mSoundManager = new SoundManager();
        mSoundManager.initSounds(getBaseContext());
        mSoundManager.addSound(1, R.raw.sound);
		_pref = getSharedPreferences(PREFNAME,Context.MODE_PRIVATE);
		//_pref.getFloat(Qualify1,0.0);
		_durationText = (Chronometer)findViewById(R.id.durationTextVal);
        _durationText.start();
        _text = (TextView)findViewById(R.id.text1);
        
        Random random = new Random();
        int val = random.nextInt(15);
        
        _text.setText(ThumbsUpData._text.get(val));
        _spanText = (Spannable) _text.getText();
        _edit = (EditText)findViewById(R.id.edit1);
        
        imm = (InputMethodManager)
        Qualify.this.getSystemService(Context.INPUT_METHOD_SERVICE);

        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
			public void onClick(View arg0) {
				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();
			
			_min = Integer.parseInt(_durationText.getText().toString().substring(0,2)) * 60; 
			_sec = Integer.parseInt(_durationText.getText().toString().substring(3,5));
		
			_totalTime = (40/((float)(_min + _sec))) * 60;
			_wpm = Math.round(_totalTime - countError());
			
			
			TextView _error,_elspedTime,_worldCount;
			final Dialog _dialog = new Dialog(Qualify.this,android.R.style.Theme_Translucent_NoTitleBar);
			_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=(Button)_dialog.findViewById(R.id.round2);
	        _goround2.setOnClickListener(new OnClickListener(){

				@Override
				public void onClick(View v) {

					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;
		
		String _textViewText = _text.getText().toString();
		String _editViewText = _edit.getText().toString();
		
		for(int i = 0 ; i < _textViewText.length();i++){
			
			if(_textViewText.charAt(i) != _editViewText.charAt(i)){
				
				_errorCounter++;
			}
			
		}
		return _errorCounter;
		
	}

	@Override
	protected void onStop() {
		super.onStop();
	}

}

Initial URL


Initial Description


Initial Title
editText and TextView On FrameLayout

Initial Tags
android

Initial Language
Java