Android开发实现输入两个数求和显示

任务目标

输入两个数,点击“=”后进行求和显示,点击系统“返回键”后返回到输入界面。效果图如下:
Android开发实现输入两个数求和显示_第1张图片
Android开发实现输入两个数求和显示_第2张图片
完成该任务需要创建两个Activity实现,对应需要编写两个页面布局文件。

第一main.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="fill_parent"
    android:background="#FFFFFF">
    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@drawable/bg_edit" >

        <EditText
            android:id="@+id/et1"
            android:layout_width="70sp"
            android:layout_height="50sp"
            android:textColor="#0000FF"
            android:background="@drawable/bg_edit"
            android:inputType="number"
            android:gravity = "center"
            android:layout_marginLeft="10dp"
            android:layout_marginTop="50dp"/>

        <TextView
            android:id="@+id/tv1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="+"
            android:textColor="#0000FF"
            android:textSize="50sp" 
            android:layout_toRightOf="@+id/et1"
            android:layout_alignTop="@+id/et1"
            android:layout_marginLeft="15dp"/>
        <EditText
            android:id="@+id/et2"
            android:layout_width="70sp"
            android:layout_height="50sp"
            android:textColor="#0000FF"
            android:background="@drawable/bg_edit"
            android:inputType="number"
            android:gravity = "center"
            android:layout_toRightOf="@+id/tv1"
            android:layout_alignTop="@+id/et1"
            android:layout_marginLeft="20dp">
        </EditText>

        <Button
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/bg_edit"
            android:text="="
            android:textColor="#0000FF"
            android:textSize="30sp"
            android:layout_toRightOf="@+id/et2"
            android:layout_alignTop="@+id/et1"
            android:layout_marginLeft="30sp"/>
    </RelativeLayout>

</LinearLayout>

上述代码中的drawable/bg_edit是一个在res下新建drawable中编写的蓝色边框背景,具体创建步骤为:右击文件夹drawable-new-other-Android xml file-file框中选择shape。be_edit.xml文件中的代码如下:


 - <?xml version="1.0" encoding="utf-8"?> <shape
   xmlns:android="http://schemas.android.com/apk/res/android" >
       <solid android:color="#FFFFFF"/> 	<corners android:radius="3dip"/> 	<stroke 
   	    android:width="3dip"
   	    android:color="#0000FF"/>

第二个main1.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="fill_parent"
    android:background="#FFFFFF">

    <TextView
        android:id="@+id/tv2"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/bg_edit"
        android:textColor="#0000FF"
        android:textSize="30sp"
        android:gravity="center" 
        android:layout_marginTop="30sp"/>
</LinearLayout>

第一个Activitiy中的代码如下:

package zt.sum;

import android.app.Activity;
import android.app.DownloadManager.Request;
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;
import android.widget.Toast;

public class SumActivity extends Activity {
    /** Called when the activity is first created. */
	private EditText et1,et2;
	private Button bt1;
	private float n1,n2;
	private int requestCode=1;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        et1 = (EditText)findViewById(R.id.et1);
        et2 = (EditText)findViewById(R.id.et2);
        bt1 = (Button)findViewById(R.id.bt1);
        
        bt1.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				// TODO Auto-generated method stub
				n1=Float.parseFloat(et1.getText().toString());
				n2=Float.parseFloat(et2.getText().toString());
				
				Bundle bd1 = new Bundle();
				bd1.putFloat("n1", n1);
				bd1.putFloat("n2", n2);
				
				Intent it1 = new Intent();
				it1.putExtras(bd1);
				it1.setClass(SumActivity.this, SecondActivity.class);
				
				startActivityForResult(it1, requestCode);

			}
		}); 
    }
	/* (non-Javadoc)
	 * @see android.app.Activity#onActivityResult(int, int, android.content.Intent)
	 */
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		if (requestCode == resultCode) {
			et1.setText("");
			et2.setText("");
		}else {
			Toast.makeText(SumActivity.this, "error", 30).show();
		}
	}
    
}

新建第二个Activity,操作为右击第一个Activity所在的packge点击-new-class-在super class一栏点击browse,输入activity,选择activty-在name中输入SecondActivity。
Android开发实现输入两个数求和显示_第3张图片

第二个Activity中的代码如下:

package zt.sum;

import android.R.string;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class SecondActivity extends Activity {
    /* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	private float n1,n2,n3;
	private TextView tv2;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main1);
		tv2 = (TextView)findViewById(R.id.tv2);
		
		Intent it2 = new Intent();
		it2 = this.getIntent();
		
		Bundle bd2 = new Bundle();
		bd2 = it2.getExtras();
		n1 = bd2.getFloat("n1", n1);
		n2 = bd2.getFloat("n2", n2);
		n3 = n1+n2;
		CharSequence cs1=String.valueOf(n1);
		CharSequence cs2=String.valueOf(n2);
		CharSequence cs3=String.valueOf(n3);
		tv2.setText(cs1+"+"+cs2+"="+cs3);
		
		setResult(1);
	}
	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	@Override
	public void onBackPressed() {
		// TODO Auto-generated method stub
		super.onBackPressed();
		finish();
	}

	
}

最后需要在AndroidManifest.xml中声明两个Activity类。如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="zt.sum"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="15" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".SumActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
         <activity
            android:name=".SecondActivity">            
        </activity>
    </application>

</manifest>

项目文件夹百度网盘下载:
链接:https://pan.baidu.com/s/1zKx8hb7u3jR24K-QKa0zdA
提取码:8uyr

你可能感兴趣的:(Android)