網站首頁 編程語言 正文
本文實例為大家分享了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+"");
?? ??? ?
?? ?}
?
}
原文鏈接:https://blog.csdn.net/zhu_hua_jie/article/details/10071667
相關推薦
- 2022-04-05 expected a string (for built-in components) or a c
- 2022-10-07 React中父組件如何獲取子組件的值或方法_React
- 2022-06-16 原生實現C#與Lua相互調用方法(Unity3D可用)_C#教程
- 2022-04-17 uniapp 實現無感刷新token, 適應大多數項目
- 2021-12-13 C語言輸出唯一的子串_C 語言
- 2022-11-14 mac 常用終端命令
- 2022-08-06 winform把Office轉成PDF文件_C#教程
- 2022-10-04 C語言零基礎徹底掌握預處理上篇_C 語言
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支