mirror of
https://github.com/nim-lang/Nim.git
synced 2026-02-13 06:43:52 +00:00
Adds interactive user interface for android.
This commit is contained in:
@@ -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;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.widget.TextView;
|
||||
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
|
||||
{
|
||||
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. */
|
||||
@Override
|
||||
public void onCreate(Bundle savedInstanceState)
|
||||
{
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.cross_calculator);
|
||||
|
||||
/* Create a TextView and set its content.
|
||||
* the text is retrieved by calling a native
|
||||
* function.
|
||||
*/
|
||||
TextView tv = new TextView(this);
|
||||
final int a = 4;
|
||||
final int b = 18;
|
||||
final Button button = (Button)findViewById(R.id.add_button);
|
||||
button.setOnClickListener(new View.OnClickListener() {
|
||||
public void onClick(View v) { addButtonClicked(); } });
|
||||
|
||||
result_text = (TextView)findViewById(R.id.result_text);
|
||||
edit_text_a = (EditText)findViewById(R.id.edit_text_a);
|
||||
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);
|
||||
tv.setText("myAdd(" + a + ", " + b + ") = " + c);
|
||||
setContentView(tv);
|
||||
result_text.setText("myAdd(" + a + ", " + b + ") = " + c);
|
||||
|
||||
if (errors.length() > 0) {
|
||||
Log.e(TAG, errors);
|
||||
Toast.makeText(this, errors, Toast.LENGTH_SHORT).show();
|
||||
}
|
||||
}
|
||||
|
||||
/* A native method that is implemented by the
|
||||
|
||||
Reference in New Issue
Block a user