一、截图
二、XML文件
activity_main.xml
<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" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="91dp"
android:text="@string/button1" />
<Button
android:id="@+id/btdial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="31dp"
android:text="@string/btdial" />
</RelativeLayout>
<span style="font-family: Arial, Helvetica, sans-serif;"><span style="font-size:12px;">activity_second.xml</span></span>
<pre name="code" class="html"><span style="font-size:10px;"><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=".SecondActivity" >
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="148dp"
android:text="@string/button2" />
</span><span style="font-size:14px;">
</span><span style="font-size:10px;"></RelativeLayout></span><span style="font-size:14px;">
</span>
三、.java文件
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);">MainActivity.java</span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java">package com.example.task;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity
{
private Button button1;
private Button btdial;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
Intent intent=new Intent(MainActivity.this,SecondActivity.class);
startActivity(intent);
}
});
btdial=(Button)findViewById(R.id.btdial);
btdial.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
Intent intent=new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:"));
startActivity(intent);
}
});
}<pre name="code" class="java">
SecondActivity.java
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name="code" class="java">package com.example.task;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class SecondActivity extends Activity
{
private Button button2;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
button2=(Button) findViewById(R.id.button2);
button2.setOnClickListener(new OnClickListener()
{
public void onClick(View view){
Intent intent=new Intent(SecondActivity.this,MainActivity.class);
startActivity(intent);
}
});
}
}
四、出现问题
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;"> 根据老师讲的内容和书上的例子,写完程序在虚拟机上运行时按钮Dial没有反应,查找也无错,纠结了好久,最后发现虚拟机上没有打电话的选项=_=!!!然后在手机上运行了一下,结果正常,如上截图所示。</span></span>
<span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"><span style="font-size:18px;">
</span></span>