拨号器的实现
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>
相关文章推荐
- [转载]Activity中ConfigChanges属性的用法
- android之定时器AlarmManager
- android上改变listView的选中颜色
- DWZ刷新dialog解决方法
- Android的TextView与Html相结合的具体方法
- Android中实现为TextView添加多个可点击的文本
- MC Dialog js弹出层 完美兼容多浏览器(5.6更新)
- js Dialog 去掉右上角的X关闭功能
- jquery BS,dialog控件自适应大小
- jquery ui dialog实现弹窗特效的思路及代码
- JQUERY dialog的用法详细解析
- Jquery自定义button按钮的几种方法
- 验证控件与Button的OnClientClick事件详细解析
- 基于jQuery的Spin Button自定义文本框数值自增或自减
- jQuery实现dialog设置focus焦点的方法
- 关于asp.net button按钮的OnClick和OnClientClick事件
- Locate a File Using a File Open Dialog Box
- 关于WPF使用MultiConverter控制Button状态的详细介绍
- Save a File Using a File Save Dialog Box
- button没写type=button会导致点击时提交