首先还是修改res/values/strings.xml,加入字符串资源:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Demo1</string>
<string name="hello_world">Hello world!</string>
<string name="action_settings">Settings</string>
<string name="action_openactivity">打开另一个</string>
<string name="action_phoneCall">打电话</string>
<string name="action_sendSMSl">发短信</string>
</resources>
在res/layout/activity_main.xml中加入button2,button3:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
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="maxwoods.demo1.MainActivity" >
<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="@string/hello_world" />
<!-- 加入button1 -->
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/text1"
android:text="@string/action_openactivity" />
<!-- 加入button2 -->
<Button
android:id="@+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:text="@string/action_phoneCall" />
<!-- 加入button3 -->
<Button
android:id="@+id/button3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button2"
android:text="@string/action_sendSMS" />
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="maxwoods.demo1"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<!-- 加入权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-permission android:name="android.permission.SEND_SMS"/>
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="maxwoods.demo1.MainActivity"
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="maxwoods.demo1.MyActivity"
android:label="@string/app_name" >
</activity>
</application>
</manifest>
修改src/maxwoods/demo1/MainActivity.java,在OnClick中处理对应的按钮事件:
package maxwoods.demo1;
import maxwoods.demo1.R;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity implements OnClickListener
{
@Override
public void onClick(View v)
{
Intent intent;
Uri uri;
switch(v.getId())
{
case R.id.button1:
intent=new Intent();
intent.setClass(this, MyActivity.class);
startActivity(intent);
break;
case R.id.button2:
uri=Uri.parse("tel:1008611");
intent=new Intent(Intent.ACTION_CALL,uri);
startActivity(intent);
break;
case R.id.button3:
uri=Uri.parse("smsto:1008611");
intent=new Intent(Intent.ACTION_SENDTO,uri);
startActivity(intent);
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View v=findViewById(R.id.button1);
v.setOnClickListener(this);
v=findViewById(R.id.button2);
v.setOnClickListener(this);
v=findViewById(R.id.button3);
v.setOnClickListener(this);
}
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
}
三个按钮功能都是通过对应Intent来实现,即可以调用自己实现的Activity,还可以调用系统的: