当前位置: 代码迷 >> Android >> Android起动-简单的乘法
  详细解决方案

Android起动-简单的乘法

热度:15   发布时间:2016-05-01 13:34:49.0
Android起步--简单的乘法
在网上看了android的相关视频,本人也试着写了一些代码。现在我在这里贴出来,用来见证我学习android的历程。
先布局:
main.xml
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><EditText	android:id="@+id/num1"	android:layout_width="fill_parent"	android:layout_height="wrap_content"/><TextView  	android:id="@+id/symbol"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="@string/hello"    /><EditText	android:id="@+id/num2"	android:layout_width="fill_parent"	android:layout_height="wrap_content"/><Button	android:id="@+id/buttonresult"	android:layout_width="fill_parent"	android:layout_height="wrap_content"/></LinearLayout>

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


strings.xml
<?xml version="1.0" encoding="utf-8"?><resources>	<string name="hello">Hello World, Activity03!</string>	<string name="app_name">Activity03</string>	<string name="symbol">乘以</string>	<string name="buttonresult">计算</string>	<string name="resultLable">result</string>	<string name="exit">退出</string>	<string name="about">关于</string>	<style name="Text">		<item name="android:textSize">18sp</item>	</style>	<style name="SpecialText" parent="@style/Text">		<item name="android:textSize">18sp</item>		<item name="android:textColor">#008</item>	</style></resources>

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="org.hs"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="4" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".Activity03"                  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=".ResultActivity" android:label="@string/resultLable"></activity>    </application></manifest>


Activity03.java
package org.hs;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;public class Activity03 extends Activity {    /** Called when the activity is first created. */    private EditText num1;    private EditText num2;    private TextView symbol;    private Button buttonresult;	@Override    public void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.main);        num1 = (EditText) findViewById(R.id.num1);        num2 = (EditText) findViewById(R.id.num2);        symbol = (TextView) findViewById(R.id.symbol);        buttonresult = (Button) findViewById(R.id.buttonresult);        //        symbol.setText("乘以");//        buttonresult.setText("计算");        symbol.setText(R.string.symbol);        buttonresult.setText(R.string.buttonresult);        buttonresult.setOnClickListener(new CalculateListener());    }			@Override	public boolean onCreateOptionsMenu(Menu menu) {			menu.add(0,1,1,R.string.exit);		menu.add(0,2,2,R.string.about);				return super.onCreateOptionsMenu(menu);	}		@Override	public boolean onOptionsItemSelected(MenuItem item) {		//菜单响应		super.onOptionsItemSelected(item);		switch (item.getItemId()) {		case 1:						break;		case 2:			finish();			break;		default:			break;		}		return true;	}		class CalculateListener implements OnClickListener{		public void onClick(View v) {			String num1Str = num1.getText().toString();//获取值			String num2Str = num2.getText().toString();			//将两个值存放在Intent对象中			Intent intent=new Intent();			intent.putExtra("one", num1Str);			intent.putExtra("two", num2Str);			intent.setClass(Activity03.this,ResultActivity.class );			//使用这个Intent对象来启动ResultActivity			Activity03.this.startActivity(intent);		}			}}

ResultActivity.java
package org.hs;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.widget.TextView;public class ResultActivity extends Activity {	private TextView resultView;	@Override	protected void onCreate(Bundle savedInstanceState) {		// TODO Auto-generated method stub		super.onCreate(savedInstanceState);		setContentView(R.layout.result);		resultView = (TextView) findViewById(R.id.result);		Intent intent = getIntent();		String num1Str = intent.getStringExtra("one");		String num2Str = intent.getStringExtra("two");		int num1 = Integer.parseInt(num1Str);		int num2 = Integer.parseInt(num2Str);		int result = num1*num2;		resultView.setText(result+"");//setText只接受字符串	}}
  相关解决方案