package com.example.phone;
import android.net.Uri;
import android.nfc.Tag;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
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 {//implements OnClickListener
private TextView textView;
private Button button;
private EditText editText;
private final static String TAG = "MainActivity";
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)this.findViewById(R.id.textView1);
button = (Button)this.findViewById(R.id.button1);
editText = (EditText)this.findViewById(R.id.editText1);
// button.setOnClickListener(new View.OnClickListener() {
//
// @Override
// public void onClick(View v) {
// // TODO Auto-generated method stub
// String phonenumberString = editText.getText().toString();
//
// Log.i(TAG, phonenumberString);
// Log.i(TAG, "匿名内部类实现点击事件");
// Intent intent = new Intent();
//
// intent.setAction(Intent.ACTION_CALL);
// intent.setData(Uri.parse("tel:"+phonenumberString));
//
// startActivity(intent);
// }
// });
/* button.setOnClickListener(new phoneOnClickListener());*/
// button.setOnClickListener(this);
}
/*
private class phoneOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String phonString = editText.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phonString));
startActivity(intent);
}
}*/
// 点击事件源多的情况下使用这种请方式添加监听器
/*
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
String phonString = editText.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phonString));
startActivity(intent);
break;
// 这个方法不能用在文本框上
// case R.id.textView1:
// System.out.println("文本框被点击了");
// break;
default:
break;
}
}*/
// xml注册的点击事件必须使用public
// 方法名要一直buttonclick
public void buttonclick(View view) {
String phonString = editText.getText().toString();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phonString));
startActivity(intent);
}
public void textclick(View view) {
System.out.println("文本框被点击了");
}
}
xml文件
<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/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="电话拨号器"
android:onClick="textclick"
android:clickable="true"/>
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numeric="integer"
android:inputType="phone"
android:hint="请输入电话号码">
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="拨号器"
android:onClick="buttonclick"/>
</LinearLayout>
拨打电话一定要添加权限<uses-permission android:name="android.permission.CALL_PHONE"/>
在给intent添加动作即action时Intent.ACTION_DIAL是带有比好键盘的类似于我们的拨号器