前言:之前有看过关于intent的视频,以为自己会了,但是到真正做东西的时候,却发现自己连最基本的东西都没有掌握,或者这是看视频带来的自以为会的错觉吧,眼高手低不是一个优秀的程序员应有的习惯。所以自己做了一个简单的计算器,来回顾下intent类的用法。
(一)、界面如图:
(二)、layout代码:
activity_main.xml:(主界面layout)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textViewId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请输入两个数字"/>
<EditText
android:id="@+id/firstNumber"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="@id/textViewId" />
<TextView
android:id="@+id/signId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textViewId"
android:layout_toRightOf="@+id/firstNumber"
android:text="+" />
<EditText
android:id="@+id/secondId"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_below="@id/textViewId"
android:layout_toRightOf="@id/signId"/>
<Button
android:id="@+id/jianSign"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:text="-"
android:layout_below="@id/firstNumber"/>
<Button
android:id="@+id/chengSign"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@+id/firstNumber"
android:layout_toRightOf="@+id/firstNumber"
android:text="*" />
<Button
android:id="@+id/chuSign"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_below="@+id/secondId"
android:layout_toRightOf="@+id/secondId"
android:text="/" />
<Button
android:id="@+id/jiaSign"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_above="@+id/resultId"
android:layout_alignParentRight="true"
android:text="+" />
<Button
android:id="@+id/resultId"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/jianSign"
android:text="等于" />
</RelativeLayout>
result_activity:(显示结果界面)
<?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/resultView"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="计算错误" />
</LinearLayout>
(三)、主要程序代码:
MainAcrivity:
package com.dengchao.simplecount;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button jianSign;
private Button jiaSign;
private Button chengSign;
private Button chuSign;
private TextView myView;
private Button resultButton;
private EditText firstNumber;
private EditText secondNumber;
private String sign;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myView =(TextView)findViewById(R.id.signId);
jiaSign =(Button)findViewById(R.id.jiaSign);
jianSign=(Button)findViewById(R.id.jianSign);
chengSign=(Button)findViewById(R.id.chengSign);
chuSign =(Button)findViewById(R.id.chuSign);
resultButton=(Button)findViewById(R.id.resultId);
firstNumber =(EditText)findViewById(R.id.firstNumber);
secondNumber =(EditText)findViewById(R.id.secondId);
//-------------设置监听器-------------
jiaSign.setOnClickListener(new jiaButtonListener());
jianSign.setOnClickListener(new jianButtonListener());
chengSign.setOnClickListener(new chengButtonListener());
chuSign.setOnClickListener(new chuButtonListener());
resultButton.setOnClickListener(new resultButtonListener());
}
//------------------监听器函数------------
//加法符号监听器
class jiaButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.setText("+");
sign ="+";
}
}
//减法符号监听器
class jianButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//把符号改为"-"
myView.setText("-");
sign ="-";
}
}
//乘法符号监听器
class chengButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.setText("*");
sign ="*";
}
}
//除法符号监听器
class chuButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
myView.setText("/");
sign ="/";
}
}
//结果符号监听器
class resultButtonListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//获取输入的字符
String OneNumber =firstNumber.getText().toString();
String TwoNumber =secondNumber.getText().toString();
//建立intent对象
Intent intent = new Intent();
//把oneNumber 和TwoNumber 的值传给one 和two
intent.putExtra("one", OneNumber);
intent.putExtra("two", TwoNumber);
//把sign传参
intent.putExtra("sign", sign);
//在MainActivity.this中打开OtherActivity.class
intent.setClass(MainActivity.this, ResultActivity.class);
MainActivity.this.startActivity(intent);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
ResultActivity:
package com.dengchao.simplecount;
import android.R.integer;
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_activity);
resultView =(TextView)findViewById(R.id.resultView);
//创建intent对象来接受数据
Intent intent = getIntent();
//获取one和two和sign传来的参数的值
String FirstNumber = intent.getStringExtra("one");
String SecondNumber =intent.getStringExtra("two");
String signr =intent.getStringExtra("sign");
//把FirstNumber和SecondNumber转化为整型
int OneInt =Integer.parseInt(FirstNumber);
int TwoInt = Integer.parseInt(SecondNumber);
//逻辑判断,根据符号进行计算
//注意!:java里判断字符串相等不是用的“==”而是equals();自己就是因为这点才发生错误
//当然这跟自己java的基础差有关系,因为自己是边学java边学android的
if(signr.equals("-"))
{int result=OneInt-TwoInt;
resultView.setText(result+"");
}
else if(signr.equals("*"))
{
int result=OneInt*TwoInt;
resultView.setText(result+"");
}
else if(signr.equals("/"))
{
int result=OneInt/TwoInt;
resultView.setText(result+"");
}
else if(signr.equals("+")){
int result=OneInt+TwoInt;
resultView.setText(result+"");
}
}
}
(四)分析:
上面的代码少了一个在Manifest.xml中声明activity的代码,但是我相信,只要是了解intent的童鞋都不会没有这个常识。总结下intent的用法:
(1)、什么是intent:Intent(意图)主要是解决Android应用的各项组件之间的通讯。
它既可以用于应用之间各个activity的消息传递(比如上面例子中的传参),还能通过调用系统函数来与其他应用进行消息传递,(例如发短信:)
(2)、常用函数调用:
1,调用web浏览器
Java代码
Uri myBlogUri = Uri.parse("http://kuikui.javaeye.com");
returnIt = new Intent(Intent.ACTION_VIEW, myBlogUri);
2,地图
Java代码
Uri mapUri = Uri.parse("geo:38.899533,-77.036476");
returnIt = new Intent(Intent.ACTION_VIEW, mapUri);
3,调拨打电话界面
Java代码
Uri telUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_DIAL, telUri);
4,直接拨打电话
Java代码
Uri callUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_CALL, callUri);
5,卸载
Java代码
Uri uninstallUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_DELETE, uninstallUri);
6,安装
Java代码
Uri installUri = Uri.fromParts("package", "xxx", null);
returnIt = new Intent(Intent.ACTION_PACKAGE_ADDED, installUri);
7,播放
Java代码
Uri playUri = Uri.parse("file:///sdcard/download/everything.mp3");
returnIt = new Intent(Intent.ACTION_VIEW, playUri);
8,调用发邮件
Java代码
Uri emailUri = Uri.parse("mailto:shenrenkui@gmail.com");
returnIt = new Intent(Intent.ACTION_SENDTO, emailUri);
9,发邮件
Java代码
returnIt = new Intent(Intent.ACTION_SEND);
String[] tos = { "shenrenkui@gmail.com" };
String[] ccs = { "shenrenkui@gmail.com" };
returnIt.putExtra(Intent.EXTRA_EMAIL, tos);
returnIt.putExtra(Intent.EXTRA_CC, ccs);
returnIt.putExtra(Intent.EXTRA_TEXT, "body");
returnIt.putExtra(Intent.EXTRA_SUBJECT, "subject");
returnIt.setType("message/rfc882");
Intent.createChooser(returnIt, "Choose Email Client");
10,发短信
Java代码
Uri smsUri = Uri.parse("tel:100861");
returnIt = new Intent(Intent.ACTION_VIEW, smsUri);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.setType("vnd.android-dir/mms-sms");
11,直接发邮件
Java代码
Uri smsToUri = Uri.parse("smsto://100861");
returnIt = new Intent(Intent.ACTION_SENDTO, smsToUri);
returnIt.putExtra("sms_body", "shenrenkui");
12,发彩信
Java代码
Uri mmsUri = Uri.parse("content://media/external/images/media/23");
returnIt = new Intent(Intent.ACTION_SEND);
returnIt.putExtra("sms_body", "shenrenkui");
returnIt.putExtra(Intent.EXTRA_STREAM, mmsUri);
returnIt.setType("image/png");
用获取到的Intent直接调用startActivity(returnIt)就ok了。
参考资料:点击打开链接