首页 > 代码库 > Android电话拨号器
Android电话拨号器
android电话拨号器的设计
具体的运行界面如上图所示。
具体操作过程:
1、新建一个Android项目。在Eclipse中依次单击“File”->“NEW”->"Android Project"
2、编写string.xml文件。具体代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <resources> 3 4 <string name="app_name">Phone拨号器</string> 5 <string name="hello_world">Hello world!</string> 6 <string name="action_settings">Settings</string> 7 <string name="mobile">请输入手机号码:</string> 8 <string name="button">拨打电话</string> 9 10 </resources>
3、编写main.xml文件。具体代码如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:paddingBottom="@dimen/activity_vertical_margin" 6 android:paddingLeft="@dimen/activity_horizontal_margin" 7 android:paddingRight="@dimen/activity_horizontal_margin" 8 android:paddingTop="@dimen/activity_vertical_margin" 9 tools:context="com.example.phone.MainActivity$PlaceholderFragment" >10 <LinearLayout android:orientation="vertical"11 android:layout_width="fill_parent"12 android:layout_height="fill_parent">13 <TextView14 android:layout_width="fill_parent"15 android:layout_height="wrap_content"16 android:text="@string/mobile"17 />18 <EditText 19 android:layout_width="fill_parent"20 android:layout_height="wrap_content"21 android:id="@+id/phoneNum"/>22 <Button 23 android:layout_width="wrap_content"24 android:layout_height="wrap_content"25 android:text="@string/button"26 android:id="@+id/btnPhone"/>27 </LinearLayout>28 </RelativeLayout>
4、处理按钮的单击事件。具体代码如下:
1 package com.example.phone; 2 3 import android.app.Activity; 4 import android.app.Fragment; 5 import android.content.Intent; 6 import android.net.Uri; 7 import android.os.Bundle; 8 import android.view.LayoutInflater; 9 import android.view.Menu;10 import android.view.MenuItem;11 import android.view.View;12 import android.view.ViewGroup;13 import android.widget.Button;14 import android.widget.EditText;15 16 public class MainActivity extends Activity {17 18 @Override19 protected void onCreate(Bundle savedInstanceState) {20 super.onCreate(savedInstanceState);21 setContentView(R.layout.activity_main);22 23 if (savedInstanceState == null) {24 getFragmentManager().beginTransaction()25 .add(R.id.container, new PlaceholderFragment()).commit();26 }27 }28 29 @Override30 public boolean onCreateOptionsMenu(Menu menu) {31 32 // Inflate the menu; this adds items to the action bar if it is present.33 getMenuInflater().inflate(R.menu.main, menu);34 return true;35 }36 37 @Override38 public boolean onOptionsItemSelected(MenuItem item) {39 // Handle action bar item clicks here. The action bar will40 // automatically handle clicks on the Home/Up button, so long41 // as you specify a parent activity in AndroidManifest.xml.42 int id = item.getItemId();43 if (id == R.id.action_settings) {44 return true;45 }46 return super.onOptionsItemSelected(item);47 }48 49 /**50 * A placeholder fragment containing a simple view.51 */52 public static class PlaceholderFragment extends Fragment {53 54 public PlaceholderFragment() {55 }56 57 @Override58 public View onCreateView(LayoutInflater inflater, ViewGroup container,59 Bundle savedInstanceState) {60 View rootView = inflater.inflate(R.layout.fragment_main, container,61 false);62 63 final EditText phoneText=(EditText)rootView.findViewById(R.id.phoneNum);64 Button btnPhone=(Button)rootView.findViewById(R.id.btnPhone);65 btnPhone.setOnClickListener(new View.OnClickListener() {66 67 @Override68 public void onClick(View v) {69 // TODO Auto-generated method stub70 String phoneNum=phoneText.getText().toString();71 if(phoneNum!=null&&(!"".equals(phoneNum.trim())))72 {73 Intent intent=new Intent(Intent.ACTION_CALL,Uri.parse("tel:"+phoneNum));74 startActivity(intent);75 }76 }77 });78 return rootView;79 }80 }81 82 }
5、申请拨号权限。我们需要在功能清单文件AndroidManifest.xml中申请权限,在</application>标签后面加上<uses-permission>具体代码如下:
1 <?xml version="1.0" encoding="utf-8"?> 2 <manifest xmlns:android="http://schemas.android.com/apk/res/android" 3 package="com.example.phone" 4 android:versionCode="1" 5 android:versionName="1.0" > 6 7 <uses-sdk 8 android:minSdkVersion="14" 9 android:targetSdkVersion="19" />10 11 <application12 android:allowBackup="true"13 android:icon="@drawable/ic_launcher"14 android:label="@string/app_name"15 android:theme="@style/AppTheme" >16 <activity17 android:name="com.example.phone.MainActivity"18 android:label="@string/app_name" >19 <intent-filter>20 <action android:name="android.intent.action.MAIN" />21 22 <category android:name="android.intent.category.LAUNCHER" />23 </intent-filter>24 </activity>25 </application>26 <uses-permission android:name="android.permission.CALL_PHONE"/>27 </manifest>
Android电话拨号器
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。