Scheming with the Coco and Android

This Example responds to double <enter-key>s. In addition to the three Android classes used in the previous Example, it uses a MotionEvent.

EmptyEnter.java

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v4.content.LocalBroadcastManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.inputmethod.EditorInfo;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.EditText;

public class EmptyEnter extends Activity {
  EditText editText;
  boolean lastCharWasEnter;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    editText=(EditText)findViewById(R.id.editText);
    lastCharWasEnter=false;
    
    editText.setOnTouchListener(new View.OnTouchListener() {
      @Override
      public boolean onTouch(View view, MotionEvent event) {
        lastCharWasEnter=false;
        return false;
      }
    });
    
    editText.addTextChangedListener(new TextWatcher() {
      public void beforeTextChanged
      (CharSequence s, int start, int count, int after) {
      }
      public void onTextChanged
      (CharSequence s, int start, int before, int count) {
        //check if exactly one char was entered and it was an 
        if (before==0 && count==1 && s.charAt(start)=='\n') {
          lastCharWasEnter=!lastCharWasEnter;
          if (lastCharWasEnter) return;
          Integer startInteger=new Integer(start);
          Intent intent=new Intent("enter");
          intent.putExtra("Start", startInteger.toString());
          mySendBroadcast(intent);
        }
        else lastCharWasEnter=false;
      }
      public void afterTextChanged(Editable s) {
      }
    });
  }

  private void mySendBroadcast(Intent intent) {
    //Can't instantiate LocalBroadcastMananager in Anonymous context
    LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
  }
  
  @Override
  public void onResume() {
    super.onResume();
    // Register messageReceiver to receive messages.
    LocalBroadcastManager.getInstance(this).registerReceiver
    (startReceiver, new IntentFilter("enter"));
  }

  // handler for received Intents for the "enter" event 
  private BroadcastReceiver startReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
      // Extract data included in the Intent
      int start=Integer.parseInt(intent.getStringExtra("Start"));
      //replace the <enter>s
      editText.getText().replace(start-1, start+1,"");
      //respond to the double <enter-key>
      editText.getText().replace(start-1, start-1,"<>");
    }
  };
  
  @Override
  protected void onPause() {
    // Unregister since the activity is not visible
    LocalBroadcastManager.getInstance(this).unregisterReceiver(startReceiver);
    super.onPause();
  }
}
§670 · April 24, 2013 · Android · · [Print]

Leave a Reply