Demo:
1)action_task1.xml:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:orientation="vertical"
>
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Enter a
Number"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/color_black"
/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="1st Number" >
<requestFocus />
</EditText>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Enter another
Number"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/color_black"
/>
<EditText
android:id="@+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="2nd
Number" />
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Sub"
/>
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add"
/>
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mul"
/>
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Div"
/>
</LinearLayout>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="@color/color_black"
/>
</LinearLayout>
2)Activity:
package
com.venky.gvrtasks;
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;
public class
ActionTask1Activity extends Activity implements OnClickListener
{
/** Called when the activity is first
created. */
EditText edtNumber1, edtNumber2;
Button btnSub, btnAdd, btnMul, btnDiv;
TextView tv1;
int number1 = 0, number2 = 0, res = 0;
@Override
public void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.action_task1);
edtNumber1 = (EditText)
findViewById(R.id.editText1);
edtNumber2 = (EditText)
findViewById(R.id.editText2);
tv1 = (TextView) findViewById(R.id.textView3);
btnSub = (Button) findViewById(R.id.button1);
btnAdd = (Button) findViewById(R.id.button2);
btnMul = (Button) findViewById(R.id.button3);
btnDiv = (Button) findViewById(R.id.button4);
btnSub.setOnClickListener(this);
btnAdd.setOnClickListener(this);
btnMul.setOnClickListener(this);
btnDiv.setOnClickListener(this);
}
public void onClick(View v)
{
// TODO
Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
number1 = Integer.parseInt(edtNumber1.getText().toString());
number2 = Integer.parseInt(edtNumber2.getText().toString());
res = number1 - number2;
tv1.setText("Subtraction
= " + res);
edtNumber1.setText("");
edtNumber2.setText("");
res = 0;
break;
case R.id.button2:
number1 = Integer.parseInt(edtNumber1.getText().toString());
number2 = Integer.parseInt(edtNumber2.getText().toString());
res = number1 + number2;
tv1.setText("Addition
= " + res);
edtNumber1.setText("");
edtNumber2.setText("");
res = 0;
break;
case R.id.button3:
number1 = Integer.parseInt(edtNumber1.getText().toString());
number2 = Integer.parseInt(edtNumber2.getText().toString());
res = number1 * number2;
tv1.setText("Multipication
= " + res);
edtNumber1.setText("");
edtNumber2.setText("");
res = 0;
break;
case R.id.button4:
number1 = Integer.parseInt(edtNumber1.getText().toString());
number2 = Integer.parseInt(edtNumber2.getText().toString());
res = number1 / number2;
tv1.setText("Division
= " + res);
edtNumber1.setText("");
edtNumber2.setText("");
res = 0;
break;
}
}
}
No comments:
Post a Comment