以下代码实现拨打电话的功能,其中涉及到点击按钮,我用四种方法实现。
首先,我设计好了相应的布局文件main_activity.xml,只是用不同的方法实现拨打电话的点击事件。
main_activity.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" >
<TextView
android:id="@+id/textView1"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:text = "@string/phone"
/>
<EditText
android:id="@+id/editText"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:layout_below = "@+id/textView1"
android:hint="@string/tip"
/>
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/editText"
android:text="@string/dail"
/>
</RelativeLayout>
1.让单独的类实现接口OnClickListener
package com.example.onclick_1;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button myButtom ;
private EditText editText ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButtom = (Button)findViewById(R.id.button);
editText = (EditText)findViewById(R.id.editText);
myButtom.setOnClickListener(new myOnClickListener());
}
/*我们需要给Button添加setOnClickListener(),setOnClickListener()需要参数android.view.View.OnClickListener接口的实现的实例。既然是接口,那就单独让一个类实现就好了。
*/
private class myOnClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = editText.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(MainActivity.this,"请输入号码!",Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:"+number);
intent.setData(data);
startActivity(intent);
}
}
}
}
2.匿名内部类
package com.example.onclick_3;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity {
private Button myButton ;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button)findViewById(R.id.button);
editText = (EditText)findViewById(R.id.editText);
myButton.setOnClickListener(new OnClickListener (){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = editText.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(MainActivity.this, "请输入号码", Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:"+number);
intent.setData(data);
startActivity(intent);
}
}
});
}
}
3.让MainActivity实现OnClickListener
package com.example.onclick_2;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.app.Activity;
import android.content.Intent;
public class MainActivity extends Activity implements OnClickListener {
private Button myButton ;
private EditText editText ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button)findViewById(R.id.button);
editText = (EditText)findViewById(R.id.editText);
myButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = editText.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(this, "请输入号码", Toast.LENGTH_SHORT).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:"+number);
intent.setData(data);
startActivity(intent);
}
}
}
4.XML中添加属性
在main_activity.xml中的Button按钮中,要添加 android:οnclick= “call”属性,而且在call()方法的参数必须是(View v)
package com.example.onclick_4;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.text.TextUtils;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button myButton;
private EditText myEditText;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button)findViewById(R.id.button);
myEditText =(EditText)findViewById(R.id.editText);
}
public void call(View v){
String number = myEditText.getText().toString().trim();
if(TextUtils.isEmpty(number)){
Toast.makeText(MainActivity.this, "请输入号码", Toast.LENGTH_LONG).show();
}else{
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
Uri data = Uri.parse("tel:"+number);
intent.setData(data);
startActivity(intent);
}
}
}
一定要记住添加打电话权限
完毕