android实现乘法计算

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

android实现乘法计算

被遗忘的秋天   2022-06-03 我要评论

主界面上是两个EditText和一个按钮。用于输入两个数字参数。

calcute.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="wrap_content"
    android:orientation="vertical" 
    android:gravity="center">
    
    <LinearLayout 
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:gravity="center"
        >
        
        <EditText 
            android:id="@+id/factory1"
            android:layout_height="wrap_content"
            android:layout_width="100dip"
            />
        <TextView 
            android:layout_width="50dip"
            android:layout_height="wrap_content"
            android:text="X"
            android:layout_marginLeft="30dip"
            />
         <EditText 
             android:id="@+id/factory2"
             android:layout_height="wrap_content"
             android:layout_width="100dip"
             />
    </LinearLayout>
    <Button 
        android:id="@+id/calute"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="计算"
        />
 
</LinearLayout>

处理calcute的java程序

CaluteMain.java:

package com.example.wenandroid;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
 
public class CaluteMain extends Activity {
private EditText factory1;
private EditText factory2;
private Button calute;
@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.calcute);
    factory1=(EditText)findViewById(R.id.factory1);
    factory2=(EditText)findViewById(R.id.factory2);
    calute=(Button)findViewById(R.id.calute);
    calute.setOnClickListener(new MyOnClickListener());
}
class MyOnClickListener implements OnClickListener{
 
    @Override
    public void onClick(View v) {
        String factoryStr1=factory1.getText().toString();
        String factoryStr2=factory2.getText().toString();
        Intent intent=new Intent(CaluteMain.this,CaluteResult.class);
        intent.putExtra("one", factoryStr1);
        intent.putExtra("two", factoryStr2);
        startActivity(intent);
    }
    
}
}

计算结果的界面:caluteresult.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/result"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        />
</LinearLayout>

接收两个数字参数并显示结果的Activity。CaluteResult.java:

package com.example.wenandroid;
 
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
 
public class CaluteResult extends Activity {
private TextView resultView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.caluteresult);
        resultView=(TextView)findViewById(R.id.result);
        Intent intent=getIntent();
        String factoryStr1=intent.getStringExtra("one");
        String factoryStr2=intent.getStringExtra("two");
        //将字符串转换为整形
        int factoryInt1=Integer.parseInt(factoryStr1);
        int factoryInt2=Integer.parseInt(factoryStr2);
        int result=factoryInt1*factoryInt2;
        resultView.setText("结果是:"+result+"");
        
    }
 
}

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们