完全披露:来自我的个人博客
android中的加法程序
android中两个数字相加的XML和Java代码
Mainactivity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private EditText editText,editText2,editText4;
int v1,v2,v3;
String s="ans";
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
editText=(EditText)findViewById(R.id.editText);
editText2=(EditText)findViewById(R.id.editText2);
editText4=(EditText)findViewById(R.id.editText4);
v1=Integer.parseInt(editText.getText().toString());
v2=Integer.parseInt(editText2.getText().toString());
v3=v1+v2;
s=String.valueOf(v3);
editText4.setText(s);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
活动主.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">
<EditText
android:layout_width="500dp"
android:layout_height="40dp"
android:id="@+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="60dp" />
<EditText
android:layout_width="500dp"
android:layout_height="40dp"
android:id="@+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="@+id/editText2" />
<EditText
android:layout_width="800dp"
android:layout_height="40dp"
android:id="@+id/editText4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="@+id/editText" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="@+id/button"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>