<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="com.example.pengtuanyuan.myapplication.MainActivity">
<EditText
android:id="@+id/A"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/symbol"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:id="@+id/B"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/calculate"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
-------------------------------------------------------
package com.example.pengtuanyuan.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private EditText a;
private EditText b;
private TextView symbol;
private Button calculate;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a =(EditText)findViewById(R.id.A);
b =(EditText)findViewById(R.id.B);
symbol =(TextView)findViewById(R.id.symbol);
calculate =(Button)findViewById(R.id.calculate);
symbol.setText(R.string.symbol);
calculate.setText(R.string.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String num1=a.getText().toString();
String num2=b.getText().toString();
Intent intent=new Intent(MainActivity.this,Activity2.class);
intent.putExtra("1",num1);
intent.putExtra("2",num2);
startActivity(intent);
}
});
}
}
-------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?><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" tools:context="com.example.pengtuanyuan.myapplication.Activity2"> <TextView android:id="@+id/resultTextView" android:layout_width="match_parent" android:layout_height="wrap_content" /></RelativeLayout>
-------------------------------------------------------
package com.example.pengtuanyuan.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class Activity2 extends AppCompatActivity {
private TextView result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
result=(TextView)findViewById(R.id.resultTextView);
Intent intent=getIntent();
String num1=intent.getStringExtra("1");
String num2=intent.getStringExtra("2");
int intnum1=Integer.parseInt(num1);
int intnum2=Integer.parseInt(num2);
int resultnum=intnum1*intnum2;
result.setText(resultnum+"");
}
}
![](https://img.haomeiwen.com/i2956701/b42b4fd3a5bb690e.png)
FED604E1-E395-4472-ACFC-BE44B9898714.png
网友评论