打电话的应用程序源码以及Android基础-----Android生命周期
16lz
2021-01-24
激活状态(运行状态)---->暂停状态---->停止状态。
活动生命周期的方法:onCreate(),onRestart(),onStart(),onResume(),onPause(),onStop(),onDestory().
打电话的应用程序源码如下:
Activity中的代码如下:
package com.cgz.callphone;
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.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText editText;
Button callbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//选择界面
setContentView(R.layout.activity_main);
//查找组件---输入框
editText=(EditText)this.findViewById(R.id.editText1);
//得到按钮
callbtn=(Button)this.findViewById(R.id.button1);
//设置监听
callbtn.setOnClickListener(new MyListener());
}
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//获取用户输入的电话号码
String callNum = editText.getText().toString();
//创建意图对象
Intent intent=new Intent();
//设置意图动作
intent.setAction("android.intent.action.CALL");
//设置电话号码
intent.setData(Uri.parse("tel:"+callNum));
//启动意图
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;
}
}
注册页面中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cgz.callphone"
android:versionCode="1"
android:versionName="1.0" >
<!-- 注册权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.cgz.callphone.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>
Layout中的布局页面代码如下:
<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="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="请输入你的手机号码:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:text="拨号" />
</RelativeLayout>
活动生命周期的方法:onCreate(),onRestart(),onStart(),onResume(),onPause(),onStop(),onDestory().
打电话的应用程序源码如下:
Activity中的代码如下:
package com.cgz.callphone;
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.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText editText;
Button callbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//选择界面
setContentView(R.layout.activity_main);
//查找组件---输入框
editText=(EditText)this.findViewById(R.id.editText1);
//得到按钮
callbtn=(Button)this.findViewById(R.id.button1);
//设置监听
callbtn.setOnClickListener(new MyListener());
}
class MyListener implements View.OnClickListener{
@Override
public void onClick(View v) {
//获取用户输入的电话号码
String callNum = editText.getText().toString();
//创建意图对象
Intent intent=new Intent();
//设置意图动作
intent.setAction("android.intent.action.CALL");
//设置电话号码
intent.setData(Uri.parse("tel:"+callNum));
//启动意图
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;
}
}
注册页面中的代码如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.cgz.callphone"
android:versionCode="1"
android:versionName="1.0" >
<!-- 注册权限 -->
<uses-permission android:name="android.permission.CALL_PHONE"/>
<uses-sdk
android:minSdkVersion="10"
android:targetSdkVersion="10" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.cgz.callphone.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>
Layout中的布局页面代码如下:
<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="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="请输入你的手机号码:" />
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView1"
android:layout_below="@+id/textView1"
android:ems="10"
android:inputType="phone" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="14dp"
android:text="拨号" />
</RelativeLayout>
更多相关文章
- 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
- Android封装类似微信的顶部TitleBar弹出的PopupWindow代码
- Android多媒体学习十五:使用在内置的Camera录制视频
- [android Studio] unhandled exception: android.os.RemoteExcep
- WebRTC Android(安卓)源码编译
- Android的系统服务一览 .
- Android(安卓)获取window状态栏和标题栏的高度
- rk3326 android 8.1 上传sftp时候jsch类报错
- Android(安卓)集成 Zxing 条码扫描器