Monday, October 7, 2013

Entering text and pausing for android

This is a test of calling the default input scheme,  whatever it is, and obtaining a "keyboard", i.e., alpha numeric, input for future use.  Also, we look into timing, sequencing, and pausing. This code was stolen from http://stackoverflow.com/questions/4111905/how-do-you-have-the-code-pause-for-a-couple-of-seconds-in-android. Other code was stolen from http://stackoverflow.com/questions/12622742/get-value-from-dialogfragment.


package com.mycompany.test1;

import android.app.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import android.content.Intent;
import android.widget.EditText;



public class MainActivity extends Activity
{

public final static String EXTRA_MESSAGE = "com.wpd0101.firstapp.MESSAGE";
/** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
/*System.out.println("hello");*/
    }
/** Called when the user clicks the Send button */
public void sendMessage(View view)
{
Intent intent = new Intent(this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.edit_message);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
}

Comments:
1)  /*System.out.println("hello");*/ is a commented out statement to show us that the program started,. LogCat needs to be viewed in order to see this comment.
2)  Intent intent = new Intent(this, DisplayMessageActivity.class); This defines an "intent". DisplayMessageActivity is what used to be called an external subroutine. It's code can be found below. It is held in a separate file in the same directory as the source (above).


package com.mycompany.test1;
/*
http://stackoverflow.com/questions/4111905/how-do-you-have-the-code-pause-for-a-couple-of-seconds-in-android
*/
import android.app.*;
import android.content.*;
import android.os.*;
import android.view.*;
import android.widget.*;
public class DisplayMessageActivity extends Activity
{
private Handler handler = new Handler();
    @Override
    protected void onCreate(Bundle savedInstanceState)
{
        super.onCreate(savedInstanceState);

// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

// Create the text view
final TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);

// Set the text view as the activity layout
setContentView(textView);
CharSequence text = message;
int duration = Toast.LENGTH_LONG;

Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
text = text + " 2nd message";
toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
handler.postDelayed(new Runnable() {
public void run()
{
// doStuff();
textView.append(", delay");
}
}, 5000);
handler.postDelayed(new Runnable() {
public void run()
{
// doStuff();
textView.setText("try 2");
setContentView(textView);
}
}, 5000);
}

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
{
        switch (item.getItemId())
{
case android.R.id.home:
return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
Here we use "toast" to put messages on the screen. Playing around with the delays and messages might help to clarify what's going on.

No comments:

Post a Comment