So, here is the code (and note that my numbers make it invalid):
package rat.cat; [1]
import static java.lang.System.out; [2]
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
[3]
public class RatCatActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttoff = [4] (Button)findViewById(R.id.button1);
buttoff.setOnClickListener(this); [4a]
}
public void onClick(View v){
String inputNumber = " ";
String matchString = " ";
RatCatApp app = (RatCatApp)getApplication();
TextView text = (TextView)findViewById(R.id.LineOne);
EditText formText = (EditText)this.findViewById(R.id.entry);
if (app.currNumber < (app.maxNumber))
{
app.currNumber++;
matchString = getMatchTxt(app.currNumber);
inputNumber = formText.getText().toString();
if (!matchString.equals(inputNumber)){
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} else {
app.currNumber++;
formText.setText("");
text.setText(getMatchTxt(app.currNumber));
}
} /* All turns until the last one go thru here */
if (app.currNumber == app.maxNumber) {
if (matchString.equals(inputNumber)) {
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp(); /* Correct last turn, let's end here */
}
else {
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* Goofed on the last turn */
}
if (app.currNumber >(app.maxNumber))
{
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* should only get here if an odd number for maxNumber is set */
}
public static String getMatchTxt(int P1){
int mod = P1%15;
if (mod == 0){ return "RATCAT";}
if (mod%5 == 0) {return "CAT";}
if (mod%3 == 0) {return "RAT";}
return Integer.toString(P1);
}
public void restartMeUp() {
Toast.makeText(this, "Starting Over...", Toast.LENGTH_SHORT).show();
RatCatApp app = (RatCatApp)getApplication();
app.currNumber = 1;
EditText formTxt = (EditText)this.findViewById(R.id.entry);
TextView txt = (TextView)findViewById(R.id.LineOne);
txt.setText("1");
formTxt.setText("");
}
}
import static java.lang.System.out; [2]
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
[3]
public class RatCatActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttoff = [4] (Button)findViewById(R.id.button1);
buttoff.setOnClickListener(this); [4a]
}
public void onClick(View v){
String inputNumber = " ";
String matchString = " ";
RatCatApp app = (RatCatApp)getApplication();
TextView text = (TextView)findViewById(R.id.LineOne);
EditText formText = (EditText)this.findViewById(R.id.entry);
if (app.currNumber < (app.maxNumber))
{
app.currNumber++;
matchString = getMatchTxt(app.currNumber);
inputNumber = formText.getText().toString();
if (!matchString.equals(inputNumber)){
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} else {
app.currNumber++;
formText.setText("");
text.setText(getMatchTxt(app.currNumber));
}
} /* All turns until the last one go thru here */
if (app.currNumber == app.maxNumber) {
if (matchString.equals(inputNumber)) {
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp(); /* Correct last turn, let's end here */
}
else {
Toast.makeText(this, "You Loose!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* Goofed on the last turn */
}
if (app.currNumber >(app.maxNumber))
{
Toast.makeText(this, "You Beat Me!", Toast.LENGTH_SHORT).show();
restartMeUp();
} /* should only get here if an odd number for maxNumber is set */
}
public static String getMatchTxt(int P1){
int mod = P1%15;
if (mod == 0){ return "RATCAT";}
if (mod%5 == 0) {return "CAT";}
if (mod%3 == 0) {return "RAT";}
return Integer.toString(P1);
}
public void restartMeUp() {
Toast.makeText(this, "Starting Over...", Toast.LENGTH_SHORT).show();
RatCatApp app = (RatCatApp)getApplication();
app.currNumber = 1;
EditText formTxt = (EditText)this.findViewById(R.id.entry);
TextView txt = (TextView)findViewById(R.id.LineOne);
txt.setText("1");
formTxt.setText("");
}
}
[1] Note that this is package rat.cat. When you create an application in Eclipse, you have to specify a name, and the name must contain a period. This name is going to become important; the name of other objects will depend on it.
[2] Next, we must import a number of methods and classes. Ctrl-Shift-0 will automagically import any missing items. Of course, sometimes one has to keep adding new imports, because it isn't until item-A gets imported that we find we need item-B
[3] This method gets created by Eclipse when the project gets created. AFAIK, every Android App will have some variation of this method.
Now, the basic class (what Eclipse creates) looks like this:
public class RelativeLayoutsActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
And, a few notes here:
1) The first thing Android does when it starts up an app is call the onCreate method. Since every app does something unique from all other apps, they will need a more-or-less unique onCreate class. So, the native onCreate method must be overridden, and Eclipse throws in the @Override directive to let the compiler know that you want to do this.
2) The main thing which this stock onCreate does is to call setContentView to put our layout (defined in layout.main) on the screen. This may be a good place to put an image of the app:
[4] Note that the main thing we have done to the onCreate method is to add a button, more specifically, a Push Button, to distinguish it from Radio Buttons and Checkboxes. This is a subclass of TextView. It gets placed in the onCreate method because the button must, in general, be ready to roll when the application launches.
Line [4] and [4a] are just about stock, that is, they look just about like every example I have seen. With some exceptions. The
"cannonical" example looks like this:
Button button = (Button)findViewById(R.id.button_id);
button.setOnClickListener(this);
The typical examples use the object types as the names of the instantiated objects. I have found it instructive to use a different name, to make it obvious to me which is which. So in the code I have created a button named 'buttoff', whose look-and-feel is defined in my layout XML as 'button1'.
findViewById is a method which returns the view created by the onCreate method which corresponds to the ID in it's argument list. In other words, it put a handle to the Button view 'button1' into the view variable 'buttoff'. And the next statement sets up a listener for this button. Puzzle over this long enough and it almost makes sense.
O.K., that sentence reads like the kind of documentation IBM became famous for: Syntactically correct, semantically accurate, and totally void of any useful information. After talking with someone who understands this better than me, I will now offer the following account:
setOnClickListener is a method which takes an object as an argument. The expected type of object is an activity. 'This' refers to the current activity. So, it wants to 'wire' a listenter between the button and something which can handle clicks in the current activity. And, the classic something for doing that is the onClick method. And we give Android a clue by stating that our activity (RatCatActivity) is extending onClickListener.
Much easier in Javascript. But then I've been doing this there a lot longer. Rather than run on and on, I will pause here and continue tomorrow.