欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算

 更新時(shí)間:2022年04月24日 11:16:19   作者:被遺忘的秋天  
這篇文章主要為大家詳細(xì)介紹了android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android使用intent傳遞參數(shù)實(shí)現(xiàn)乘法計(jì)算的具體代碼,供大家參考,具體內(nèi)容如下

主界面上是兩個(gè)EditText和一個(gè)按鈕。用于輸入兩個(gè)數(shù)字參數(shù)。

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="計(jì)算"
? ? ? ? />
?
</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);
?? ?}
?? ?
}
}

計(jì)算結(jié)果的界面: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>

接收兩個(gè)數(shù)字參數(shù)并顯示結(jié)果的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");
?? ??? ?//將字符串轉(zhuǎn)換為整形
?? ??? ?int factoryInt1=Integer.parseInt(factoryStr1);
?? ??? ?int factoryInt2=Integer.parseInt(factoryStr2);
?? ??? ?int result=factoryInt1*factoryInt2;
?? ??? ?resultView.setText("結(jié)果是:"+result+"");
?? ??? ?
?? ?}
?
}

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論