yes, we will demonstract here for conversion, Integer.Parse
package com.andriodproject.helloandriod;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
EditText edt1;
EditText edt2;
int result;
TextView tvresult;
Button btnadd,btnsubstract,btnexit;
int total;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1=(EditText)findViewById(R.id.editText1);
edt2=(EditText)findViewById(R.id.editText2);
tvresult=(TextView)findViewById(R.id.textView3);
btnadd=(Button)findViewById(R.id.btn_add);
btnsubstract=(Button)findViewById(R.id.btn_sub);
btnexit=(Button)findViewById(R.id.btn_exit);
btnadd.setOnClickListener(this);
btnsubstract.setOnClickListener(this);
btnexit.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v==btnexit)
{ finish();
}
else
{
String first=edt1.getText().toString().trim();
String second=edt2.getText().toString().trim();
if(first.length()<=0)
{
tvresult.setText("Enter First No");
edt1.requestFocus();
return;
}
if(second.length()<=0)
{
tvresult.setText("Enter second No");
edt2.requestFocus();
return;
}
try
{
int a=Integer.parseInt(first);
int b=Integer.parseInt(second); if(v==btnadd)
total=a+b;
else if(v==btnsubstract)
total=a-b;
tvresult.setText(String.valueOf(total));
}
catch(Exception ex)
{
tvresult.setText("error occured");
}
}
}
}
package com.andriodproject.helloandriod;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.*;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
EditText edt1;
EditText edt2;
int result;
TextView tvresult;
Button btnadd,btnsubstract,btnexit;
int total;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
edt1=(EditText)findViewById(R.id.editText1);
edt2=(EditText)findViewById(R.id.editText2);
tvresult=(TextView)findViewById(R.id.textView3);
btnadd=(Button)findViewById(R.id.btn_add);
btnsubstract=(Button)findViewById(R.id.btn_sub);
btnexit=(Button)findViewById(R.id.btn_exit);
btnadd.setOnClickListener(this);
btnsubstract.setOnClickListener(this);
btnexit.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
if(v==btnexit)
{ finish();
}
else
{
String first=edt1.getText().toString().trim();
String second=edt2.getText().toString().trim();
if(first.length()<=0)
{
tvresult.setText("Enter First No");
edt1.requestFocus();
return;
}
if(second.length()<=0)
{
tvresult.setText("Enter second No");
edt2.requestFocus();
return;
}
try
{
int a=Integer.parseInt(first);
int b=Integer.parseInt(second); if(v==btnadd)
total=a+b;
else if(v==btnsubstract)
total=a-b;
tvresult.setText(String.valueOf(total));
}
catch(Exception ex)
{
tvresult.setText("error occured");
}
}
}
}
No comments:
Post a Comment