Adds interactive user interface for android.
This commit is contained in:
parent
3492bb7744
commit
f6e7aebbd9
2 changed files with 116 additions and 10 deletions
|
|
@ -0,0 +1,72 @@
|
||||||
|
<?xml version="1.0" encoding="utf-8"?>
|
||||||
|
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||||
|
android:id="@+id/RelativeLayout1"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="fill_parent"
|
||||||
|
android:orientation="vertical" >
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/title"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
android:layout_alignParentTop="true"
|
||||||
|
android:layout_centerHorizontal="true"
|
||||||
|
android:text="Crossplatform Nimrod calculator"
|
||||||
|
android:textSize="20dip" >
|
||||||
|
|
||||||
|
</TextView>
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/value_a"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/title"
|
||||||
|
android:text="Value A: " >
|
||||||
|
</TextView>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/edit_text_a"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_below="@+id/title"
|
||||||
|
android:ems="10"
|
||||||
|
android:inputType="number" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/value_b"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_below="@+id/edit_text_a"
|
||||||
|
android:text="Value B: " >
|
||||||
|
</TextView>
|
||||||
|
|
||||||
|
<EditText
|
||||||
|
android:id="@+id/edit_text_b"
|
||||||
|
android:layout_width="wrap_content"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentRight="true"
|
||||||
|
android:layout_below="@+id/edit_text_a"
|
||||||
|
android:ems="10"
|
||||||
|
android:inputType="number" />
|
||||||
|
|
||||||
|
<Button
|
||||||
|
android:id="@+id/add_button"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
android:layout_below="@+id/edit_text_b"
|
||||||
|
android:scrollbarAlwaysDrawVerticalTrack="false"
|
||||||
|
android:selectAllOnFocus="false"
|
||||||
|
android:text="Add!"
|
||||||
|
android:visibility="visible" />
|
||||||
|
|
||||||
|
<TextView
|
||||||
|
android:id="@+id/result_text"
|
||||||
|
android:layout_width="fill_parent"
|
||||||
|
android:layout_height="wrap_content"
|
||||||
|
android:layout_alignParentLeft="true"
|
||||||
|
android:layout_below="@+id/add_button" />
|
||||||
|
|
||||||
|
</RelativeLayout>
|
||||||
|
|
@ -1,27 +1,61 @@
|
||||||
package com.github.nimrod.crosscalculator;
|
package com.github.nimrod.crosscalculator;
|
||||||
|
|
||||||
import android.app.Activity;
|
import android.app.Activity;
|
||||||
import android.widget.TextView;
|
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.View;
|
||||||
|
import android.widget.Button;
|
||||||
|
import android.widget.EditText;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
public class CrossCalculator extends Activity
|
public class CrossCalculator extends Activity
|
||||||
{
|
{
|
||||||
|
private static final String TAG = "CrossCalculator";
|
||||||
|
private TextView result_text;
|
||||||
|
private EditText edit_text_a, edit_text_b;
|
||||||
/** Called when the activity is first created. */
|
/** Called when the activity is first created. */
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(Bundle savedInstanceState)
|
public void onCreate(Bundle savedInstanceState)
|
||||||
{
|
{
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
|
setContentView(R.layout.cross_calculator);
|
||||||
|
|
||||||
/* Create a TextView and set its content.
|
final Button button = (Button)findViewById(R.id.add_button);
|
||||||
* the text is retrieved by calling a native
|
button.setOnClickListener(new View.OnClickListener() {
|
||||||
* function.
|
public void onClick(View v) { addButtonClicked(); } });
|
||||||
*/
|
|
||||||
TextView tv = new TextView(this);
|
result_text = (TextView)findViewById(R.id.result_text);
|
||||||
final int a = 4;
|
edit_text_a = (EditText)findViewById(R.id.edit_text_a);
|
||||||
final int b = 18;
|
edit_text_b = (EditText)findViewById(R.id.edit_text_b);
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Handles clicks on the addition button.
|
||||||
|
* Reads the values form the input fields and performs the calculation.
|
||||||
|
*/
|
||||||
|
private void addButtonClicked()
|
||||||
|
{
|
||||||
|
int a = 0, b = 0;
|
||||||
|
String errors = "";
|
||||||
|
final String a_text = edit_text_a.getText().toString();
|
||||||
|
final String b_text = edit_text_b.getText().toString();
|
||||||
|
try {
|
||||||
|
a = Integer.valueOf(a_text, 10);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
errors += "Can't parse a value '" + a_text + "'. ";
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
b = Integer.valueOf(b_text, 10);
|
||||||
|
} catch (NumberFormatException e) {
|
||||||
|
errors += "Can't parse b value '" + b_text + "'";
|
||||||
|
}
|
||||||
final int c = myAdd(a, b);
|
final int c = myAdd(a, b);
|
||||||
tv.setText("myAdd(" + a + ", " + b + ") = " + c);
|
result_text.setText("myAdd(" + a + ", " + b + ") = " + c);
|
||||||
setContentView(tv);
|
|
||||||
|
if (errors.length() > 0) {
|
||||||
|
Log.e(TAG, errors);
|
||||||
|
Toast.makeText(this, errors, Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* A native method that is implemented by the
|
/* A native method that is implemented by the
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue