您的位置:首页 > 其它

拨号器的实现

2015-07-22 15:16 295 查看
package cn.edu.fosu.dialer;

import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//找到按钮
Button bt=(Button) findViewById(R.id.bt_call);
//给按钮注册监听器
bt.setOnClickListener(new MyListener());
}
class MyListener implements OnClickListener{

@Override
public void onClick(View v) {
//找到文本输入框
EditText ex=(EditText) findViewById(R.id.ex_call);
//获取文本输入框的值
String phone=ex.getText().toString();
//创建意图
Intent intent=new Intent();
//调用打电话的功能
intent.setAction(Intent.ACTION_CALL);
//拨打的电话号码
intent.setData(Uri.parse("tel:"+phone));
//开启activity
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;
}

}


activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity" >
<!-- 显示“请输入号码” -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/dialer" />
<!-- 显示输入的文本 -->
<EditText
android:id="@+id/ex_call"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- 拨号按钮 -->
<Button
android:id="@+id/bt_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/bt_call"/>
</LinearLayout>

strings.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>

<string name="app_name">电话拨号器</string>
<string name="action_settings">Settings</string>
<string name="hello_world">Hello world!</string>
<string name="dialer">请输入号码:</string>
<string name="bt_call">拨号</string>
</resources>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.edu.fosu.dialer"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="cn.edu.fosu.dialer.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>
</application>

</manifest>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息