/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button next = (Button) findViewById(R.id.Button01);
next.setonclickListener(new View.onclickListener() {
public void onclick(View view) {
Intent myIntent = new Intent(view.getContext(), Activity2.class);
startActivityForResult(myIntent, 0);
in my AndroidFinal.java or "Activity1.java" program.
package edu.isaacfinal;
import java.util.ArrayList;
import java.util.Collections;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.graphics.Color;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class AndroidFinal extends Activity
{
private static final String TAG = "Midterm";
private static final int DIALOG_QUIT_ID = 0;
private static final int NEXT_BUTTON = 1;
private static final int SHOW_BUTTON = 2;
private static final int GUESS_BUTTON = 3;
private static final int DEFINE_BUTTON = 4;
private static final int ABOUT_BUTTON = 5;
private static final int QUIT_BUTTON = 6;
private String[] terms;
private String[] definitions;
private int termsUsed;
ArrayList<Word> words;
private MediaPlayer rightMediaPlayer;
private MediaPlayer wrongMediaPlayer;
private Button nextButton;
private Button showButton;
private Button guessButton;
private Button defineButton;
private Button aboutButton;
private Button quitButton;
private TextView termArea;
private TextView definitionArea;
private EditText guessText;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.v( TAG, "Starting up the project ..." );
// read the data from the XML
Resources res = getResources();
terms = res.getStringArray( R.array.terms );
definitions = res.getStringArray( R.array.definitions );
// Just checking that the 2 arrays match
Log.v( TAG, "terms-length" + terms.length );
Log.v( TAG, "definitions-length" + definitions.length );
// load up the ArrayList of Words
words = new ArrayList<Word>( terms.length );
for ( int i = 0; i < terms.length; i++ )
{
words.add( new Word( terms[i], definitions[i] ) );
}
// get the objects for the UI widgets
nextButton = (Button) findViewById( R.id.next );
showButton = (Button) findViewById( R.id.show );
guessButton = (Button) findViewById( R.id.guess );
defineButton = (Button) findViewById( R.id.define );
aboutButton = (Button) findViewById(R.id.about);
quitButton = (Button) findViewById(R.id.quit);
termArea = (TextView) findViewById( R.id.term_area );
definitionArea = (TextView) findViewById( R.id.definition_area );
guessText = (EditText) findViewById( R.id.guess_text );
// set up the button click listeners for each button (by ID)
nextButton.setonclickListener( new ButtonclickListener( NEXT_BUTTON ) );
showButton.setonclickListener( new ButtonclickListener( SHOW_BUTTON ) );
guessButton.setonclickListener( new ButtonclickListener( GUESS_BUTTON ) );
defineButton.setonclickListener( new ButtonclickListener( DEFINE_BUTTON ) );
aboutButton.setonclickListener(new ButtonclickListener(ABOUT_BUTTON));
quitButton.setonclickListener(new ButtonclickListener(QUIT_BUTTON));
// set up the sound effects
rightMediaPlayer = MediaPlayer.create( getApplicationContext(), R.raw.zoop );
wrongMediaPlayer = MediaPlayer.create( getApplicationContext(), R.raw.screech );
// initialize the variables
clearUsed();
// and start with a word
newWord();
}
/*
* termsUsed keeps track of when the whole set of words has been used
* This methods starts it over and shuffles the words anew
*/
public void clearUsed()
{
termsUsed = 0;
Collections.shuffle( words );
}
/*
* Set up a new word: clear the guess and definition area and
* get the scrambled word and show it in red text.
*/
private void newWord()
{
definitionArea.setText( "" );
guessText.setText( "" );
termArea.setText( words.get(termsUsed).getScrambledWord() );
termArea.setTextColor( Color.RED );
}
/*
* Show the unscrambled word in green and its definition
*/
private void unscrambleWord()
{
termArea.setText( words.get(termsUsed).getWord() );
termArea.setTextColor( Color.GREEN );
definitionArea.setText( words.get(termsUsed).getDefinition() );
}
/*
** This method is called when the dialogs are first shown (showDialog)
** We only have a quit dialog
*/
@Override
protected Dialog onCreateDialog( int id )
{
Dialog dialog = null;
// all these dialogs can be built using the AlertDialog builder
// only the about dialog is described in xml, the other 2 are built
// directly in the code. AlertDialog has some set styles and button
// options - look them up.
AlertDialog.Builder builder = new AlertDialog.Builder( this );
switch( id )
{
case DIALOG_QUIT_ID:
// the simplest dialog style just has a message and 2 buttons
builder.setMessage( R.string.quit_question );
builder.setCancelable( false );
builder.setPositiveButton( R.string.yes,
new DialogInterface.onclickListener()
{
// set what to do if the user says -yes- to quitting
public void onclick( DialogInterface dialog, int id )
{
AndroidFinal.this.finish();
}
}
);
builder.setNegativeButton( R.string.no, null );
dialog = builder.create();
break;
}
return dialog;
}
/*
* A method of activity for creating the Options Menu the first time
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu( Menu menu )
{
super.onCreateOptionsMenu(menu);
// use the inflater to build from the XML
MenuInflater inflater = getMenuInflater();
inflater.inflate( R.menu.options_menu, menu);
return true;
}
/*
* A method of Activity for handling the Options Menu - ie the main app menu
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected( MenuItem item )
{
// This is automatically called when a menu option is selected - take the right action
switch( item.getItemId() )
{
case R.id.quit:
showDialog( DIALOG_QUIT_ID );
break;
}
return true;
}
@Override
protected void onresume()
{
super.onresume();
rightMediaPlayer = MediaPlayer.create( getApplicationContext(), R.raw.zoop );
wrongMediaPlayer = MediaPlayer.create( getApplicationContext(), R.raw.screech );
}
@Override
protected void onpause()
{
super.onpause();
rightMediaPlayer.release();
wrongMediaPlayer.release();
}
private class ButtonclickListener implements View.onclickListener
{
int button_id;
public ButtonclickListener( int id )
{
// Tell which button you are in the beginning
this.button_id = id;
}
public void onclick( View view )
{
// Then we know which button you are every time
// termsUsed is counting and keeping track of which word we are on
switch ( button_id )
{
case DEFINE_BUTTON:
// Display the definition of the current word
definitionArea.setText( words.get(termsUsed).getDefinition() );
break;
case SHOW_BUTTON:
// Display the unscrambled current word
// why not show the definition too?
unscrambleWord();
break;
case GUESS_BUTTON:
// if the word is correct show it, otherwise beep
String guess = ( (TextView) guessText).getText().toString();
if ( words.get( termsUsed ).getWord().equals( guess ) )
{
rightMediaPlayer.start();
unscrambleWord();
}
else
{
wrongMediaPlayer.start();
}
break;
case NEXT_BUTTON:
// Move to the next word
termsUsed++;
// Rescramble the word list if we've reached the end of it
if ( termsUsed == terms.length )
clearUsed();
// show the next word and clear the other fields
newWord();
break;
case ABOUT_BUTTON:
{
Button next = (Button) findViewById(R.id.about);
next.setonclickListener(new View.onclickListener() {
public void onclick(View view) {
Intent myIntent = new Intent(view.getContext(), AboutActivity.class);
startActivityForResult(myIntent, 0);
}});}
case QUIT_BUTTON:
finish();
default:
break;
}
{
}
}}}

New Topic/Question
Reply



MultiQuote





|