1、Activity03代码:
package mars.activity03;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
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;
/*
1、声明控件
2、为2个控件设置显示的值
3、创建一个监听器类,监听按下的动作
4、将监听器类的对象,绑定在按钮对象上
*/
public class Activity03 extends Activity {
private EditText factorone=null;
private EditText factortwo=null;
private TextView mytextview=null;
private Button mybutton=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
factorone=(EditText)findViewById(R.id.factorone);
factortwo=(EditText)findViewById(R.id.factortwo);
mytextview=(TextView)findViewById(R.id.myTextView);
mybutton=(Button)findViewById(R.id.myButton);
//mytextview.setText("乘以");
//mybutton.setText("计算");
mytextview.setText(R.string.symbol);//将有关汉语的放在R.string的键值对中
mybutton.setText(R.string.calculate);
mybutton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
String onestr=factorone.getText().toString();
String twostr=factortwo.getText().toString();
Intent intent=new Intent();
intent.putExtra("one", onestr);
intent.putExtra("two",twostr);
intent.setClass(Activity03.this,newActivity.class);
Activity03.this.startActivity(intent);
}
});
}
//点击返回按钮时出现的界面
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(0,1,1,R.string.exit);//(0,itemID,1,显示内容)
menu.add(0,2,2,R.string.about);
menu.add(0,3,3,R.string.fenxiang);
getMenuInflater().inflate(R.menu.activity03, menu); //加载Settings
return super.onCreateOptionsMenu(menu);
}
//菜单项
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()){
case 1:
finish();//关闭程序
break;
case 3:
message();//发短信
break;
default:
break;
}
return false;
}
private void message(){
Uri uri=Uri.parse("smsto://12345678900");
Intent intent=new Intent(Intent.ACTION_SENDTO,uri);
intent.putExtra("sms_body","XXX,你好!");
startActivity(intent);
}
}
2、newActivity代码:
package mars.activity03;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;
/*
1、接受所传递过来的值
2、计算两者的积
3、将结果显示在
* */
public class newActivity extends Activity {
private TextView mytextview=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
mytextview=(TextView)findViewById(R.id.myTextView);
Intent intent=getIntent();
String value1=intent.getStringExtra("one");
String value2=intent.getStringExtra("two");
//乘积要装换类型
int one=Integer.parseInt(value1);
int two=Integer.parseInt(value2);
int result=one*two;
//setText要接受的是字符串,而result为整形,需要转换为字符串。
//(而JAVA中连接符一端为字符串,另一端也为字符串)
mytextview.setText(result+"");
}
}
3、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:orientation="vertical" >
<EditText
android:id="@+id/factorone"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<EditText
android:id="@+id/factortwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<Button
android:id="@+id/myButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
4、result.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:orientation="vertical" >
<TextView
android:id="@+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
5、运行效果: