看mars的视频,写了一个乘法计算,只要涉及到多个Activity的通信问题,现总结如下:
多个activity的通信靠的是Intent(Aanroid提供的避免多组建耦合的一个消息机制,翻译叫意图)
基本通信原理如下:
另外罗列下Intent几个比较重要的方法
getIntent()-----创建一个Intent;
putExtra()------发送Intent消息内容
getExtra(String name, Object defaultValue)---得到Intent消息内容
setClass(activity_01,activity_02)----设置互相通信对象
现在上代码:
package com.android.test_001.button;
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;
import android.widget.TextView;
public class buttonActivity extends Activity {
/** Called when the activity is first created. */
private EditText factorOne,factorTwo;
private TextView symbol_x;
private Button caculate_x;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorOne=(EditText)findViewById(R.id.fanctorOne);
factorTwo=(EditText)findViewById(R.id.fanctorTow);
symbol_x=(TextView)findViewById(R.id.symbol_x);
caculate_x=(Button)findViewById(R.id.caculate);
//symbol_x.setText(R.string.symbol_x);
caculate_x.setText(R.string.caculate_x);
caculate_x.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String factorOneValue=factorOne.getText().toString();
String factorTwoValue=factorTwo.getText().toString();
Intent intent=getIntent();
intent.putExtra("factorOne", factorOneValue);
intent.putExtra("factorTwo", factorTwoValue);
intent.setClass(buttonActivity.this, resultActivity.class);
buttonActivity.this.startActivity(intent);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test_001.button"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".hellowordActivity" >
</activity>
<activity
android:label="@string/caculate"
android:name=".buttonActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/result"
android:name=".resultActivity" >
</activity>
</application>
</manifest>
package com.android.test_001.button;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class resultActivity extends Activity{
/* (non-Javadoc)
* @see android.app.Activity#onOptionsItemSelected(android.view.MenuItem)
*/
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
if(item.getItemId()==1){
finish();
}
return super.onOptionsItemSelected(item);
}
/* (non-Javadoc)
* @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
*/
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0, 1, 1, R.string.exit);
menu.add(0, 2, 2, R.string.about);
return super.onCreateOptionsMenu(menu);
}
/* (non-Javadoc)
* @see android.app.Activity#onCreate(android.os.Bundle)
*/
private TextView resultText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
resultText=(TextView)findViewById(R.id.result);
Intent intent=getIntent();
String factorOne =intent.getStringExtra("factorOne");
String factorTow =intent.getStringExtra("factorTwo");
//System.out.println("f1="+factorOne+"/f2="+factorTow);
int intfactorOne=Integer.parseInt(factorOne);
int intfactorTow =Integer.parseInt(factorTow);
int result=intfactorOne*intfactorTow;
resultText.setText(result+"");
}
}
<?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:orientation="vertical"
android:background="@drawable/pic1">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<TextView
android:id="@+id/myXmlHello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/myhello"/>
<TextView
android:id="@+id/myjavahello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myurllink_01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myurllink_02"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/fanctorOne"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/fanctorTow"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/symbol_x"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/caculate"
android:layout_width="100px"
android:layout_height="50px"
/>
</LinearLayout>
<?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:orientation="vertical"
android:background="@drawable/pic1">
<TextView
android:id="@+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">my first_smm helloword app</string>
<string name="app_name">Hi smm i miss you</string>
<string name="myhello">my first hello for android-this is defined by xml</string>
<string name="symbol_x">X</string>
<string name="caculate_x">计算</string>
<string name="caculate">乘法计算</string>
<string name="result">计算</string>
<string name="exit">退出</string>
<string name="about">关于</string>
<color name="red">#FF0000</color>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.android.test_001.button"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="4" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".hellowordActivity" >
</activity>
<activity
android:label="@string/caculate"
android:name=".buttonActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/result"
android:name=".resultActivity" >
</activity>
</application>
</manifest>
我在做的过程中碰到许多问题:
1、因为多个Activity的话,要再androidManifest.xml里面注册,而我注册的时候写错了:
<activity
android:label="@string/caculate"
android:name=".buttonActivity.java" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
红色部分写错了,不应该有.java,报的是logcat报的错误是NotFoundClassException error
2、在buttonActivity.java里面一定要加一下语句:
buttonActivity.this.startActivity(intent);
启动消息发送
其他的都是一些小问题,不再仔细讲解了,基本功太差,还是得多练
结果图: