android使用intent传递参数实现乘法计算

 更新时间:2022年04月24日 11:16:19   作者:被遗忘的秋天  
这篇文章主要为大家详细介绍了android使用intent传递参数实现乘法计算,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

本文实例为大家分享了android使用intent传递参数实现乘法计算的具体代码,供大家参考,具体内容如下

主界面上是两个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+"");
        
    }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • Flutter实现旋转扫描效果

    Flutter实现旋转扫描效果

    这篇文章主要介绍了通过Flutter RotationTransition实现雷达旋转扫描的效果,文中的示例代码讲解详细,感兴趣的同学可以动手试一试
    2022-01-01
  • flutter的导航和路由使用示例详解

    flutter的导航和路由使用示例详解

    这篇文章主要为大家介绍了flutter的导航和路由使用示例详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2022-12-12
  • android预置默认的语音信箱号码具体实现

    android预置默认的语音信箱号码具体实现

    在此介绍以xml的方式预置VM number的方法,以及如何允许用户去修改并能够记住用户的选择
    2013-06-06
  • Android开发RecyclerView性能优化之异步预加载

    Android开发RecyclerView性能优化之异步预加载

    这篇文章主要介绍了Android开发RecyclerView性能优化之异步预加载实现解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-06-06
  • Android手机屏幕同步工具asm.jar

    Android手机屏幕同步工具asm.jar

    今天小编就为大家分享一篇关于Android手机屏幕同步工具asm.jar的文章,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧
    2018-10-10
  • Android Studio 3.0被调方法参数名提示的取消方法

    Android Studio 3.0被调方法参数名提示的取消方法

    这篇文章主要介绍了去掉android studio 3.0被调方法参数名提示的解决方法,在文章末尾给大家补充介绍了Android Studio 3.0 gradle提示太老的解决方法,非常不错,需要的朋友可以参考下
    2017-11-11
  • Android开发之imageView图片按比例缩放的实现方法

    Android开发之imageView图片按比例缩放的实现方法

    这篇文章主要介绍了Android开发之imageView图片按比例缩放的实现方法,较为详细的分析了Android中ImageView控件的scaleType属性控制图片缩放的具体用法,需要的朋友可以参考下
    2016-01-01
  • Android 软键盘弹出时把原来布局顶上去的解决方法

    Android 软键盘弹出时把原来布局顶上去的解决方法

    本文主要介绍了Android软键盘弹出时把原来布局顶上去的解决方法。具有一定的参考作用,下面跟着小编一起来看下吧
    2017-01-01
  • android上实现0.5px线条的原理分析

    android上实现0.5px线条的原理分析

    这篇文章主要介绍了android上实现0.5px线条的原理分析,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2021-01-01
  • Android中PathMeasure仿支付宝支付动画

    Android中PathMeasure仿支付宝支付动画

    这篇文章主要为大家详细介绍了Android中PathMeasure仿支付宝支付动画,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2017-08-08

最新评论