Android-2電話應用,短信應用
來源:程序員人生 發布時間:2014-11-13 09:03:58 閱讀次數:2659次
Activity的生命周期

Android的核心組件
1.Viiew :界面 ,組織UI控件
2.Intent :意圖,支持組件之間的通訊
3.Activity:處理界面與UI互動
4.Content Provider:存儲同享數據
5.IntentReceiver:接收信息及時間處理
6.Service:后臺服務(如硬件與驅動的服務 )
7.Notification:消息與通訊
Android的運行
AndroidManifest.xml為程序的入口
R.文件是我們創建的1個目錄文件
------------------------------------------------------------------
建立打電話發短信的利用程序
1.對number對話框定義
<EditText
android:id="@+id/NummberText" ----->在R文件中配置ID地址
android:layout_width="match_parent" ------>充滿布局文件
android:layout_height="wrap_content"
android:layout_alignParentTop="true" ------>頂部對齊
android:layout_centerHorizontal="true" ----->居中
android:textColor="#FF0099" --------->設置文字的字體
android:ems="10"
android:hint="@string/SMS_please"
android:inputType="phone" > ------->默許的陰影提示
<requestFocus />
</EditText>
2.按鈕的設計
<Button
android:id="@+id/Call_otherButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/NummberText" --->相對布局
android:textColor="#9900CC"------>設置文字的字體色彩
android:layout_centerHorizontal="true"
android:text="@string/call_other" />
<Button
android:id="@+id/Call_xiaoyuButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Call_otherButton"
android:textColor="#FF0033"
android:layout_centerHorizontal="true"
android:text="@string/love_xiaoyu" />
<Button
android:id="@+id/Call_mangqi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/Call_xiaoyuButton"
android:layout_centerHorizontal="true"
android:text="@string/call_wangqi" />
3.設置小 layout
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/Call_mangqi"
android:layout_below="@+id/Call_mangqi"
android:textColor="#FF9595"
android:text="@string/Talk_Message" />
------>@String在value文件中加載strings并在其中加載Talk_Message字符串
5.制作文本框

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:lines="8" --->設置函數的大小
android:hint="@string/_love" -->設計默許值得大小
android:ems="10"
android:inputType="textMultiLine" />
6邏輯設計
public class MainActivity extends Activity implements OnClickListener{
Button Call_other;
Button Call_wangqi;
Button Call_xiaoyu;
Button Send_other;
Button Send_wangqi;
Button Send_xiaoyu;
EditText Nummber_edit;
EditText Talk_Edit;
@Override
A-定義控件便于尋覓
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Nummber_edit = (EditText)B-findViewById(R.id.NummberText); 通過findViewById來找到控制控件
Talk_Edit= (EditText)findViewById(R.id.editText1);
Call_other = (Button)findViewById(R.id.Call_otherButton);
Call_wangqi= (Button)findViewById(R.id.Call_mangqi);;
Call_xiaoyu= (Button)findViewById(R.id.Call_xiaoyuButton);;
Send_other= (Button)findViewById(R.id.Send_friend);
Send_wangqi= (Button)findViewById(R.id.Send_wangqi);
Send_xiaoyu= (Button)findViewById(R.id.Send_xiaoyu);
C -public class MainActivity extends Activity implements OnClickListener{
將主類實現OnClickListener接口可以對控件通過1個OnClick控制
D:1.在文件中設計按鈕的事件
private void MyCallOther(){
String num = Nummber_edit.getText().toString();
if(TextUtils.isEmpty(num)){ ---判斷字符中的文字是不是為空
Toast.makeText(MainActivity.this, "電話為空了>.<", 0
--顯示的時間).show();--土司生成
return ;
}else{
Intent intent = new Intent();----打電話的程序重要的1點事必須啟動1個Intent的程序
Toast.makeText(MainActivity.this, "電話打向"+num, 0).show();--必須show出來
intent.setAction(Intent.ACTION_CALL);---設置打電話的設置
intent.setData(Uri.parse("tel:"+num));---設置電環號碼
startActivity(intent);---啟動事件
return;
}
}
private void MyCallxiaoyu(){
Intent intent = new Intent();
Toast.makeText(MainActivity.this, "Love曉宇691526", 0).show();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"691526"));
startActivity(intent);
return ;
}
private void CallWangqi (){
Intent intent = new Intent();
Toast.makeText(MainActivity.this, "打給討厭鬼王琪>.<", 0).show();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"652008"));
startActivity(intent);
}

<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:lines="8" --->設置函數的大小
android:hint="@string/_love" -->設計默許值得大小
android:ems="10"
android:inputType="textMultiLine" />
6邏輯設計
public class MainActivity extends Activity implements OnClickListener{
Button Call_other;
Button Call_wangqi;
Button Call_xiaoyu;
Button Send_other;
Button Send_wangqi;
Button Send_xiaoyu;
EditText Nummber_edit;
EditText Talk_Edit;
@Override
A-定義控件便于尋覓
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Nummber_edit = (EditText)B-findViewById(R.id.NummberText); 通過findViewById來找到控制控件
Talk_Edit= (EditText)findViewById(R.id.editText1);
Call_other = (Button)findViewById(R.id.Call_otherButton);
Call_wangqi= (Button)findViewById(R.id.Call_mangqi);;
Call_xiaoyu= (Button)findViewById(R.id.Call_xiaoyuButton);;
Send_other= (Button)findViewById(R.id.Send_friend);
Send_wangqi= (Button)findViewById(R.id.Send_wangqi);
Send_xiaoyu= (Button)findViewById(R.id.Send_xiaoyu);
C -public class MainActivity extends Activity implements OnClickListener{
將主類實現OnClickListener接口可以對控件通過1個OnClick控制
D:1.在文件中設計按鈕的事件
private void MyCallOther(){
String num = Nummber_edit.getText().toString();
if(TextUtils.isEmpty(num)){ ---判斷字符中的文字是不是為空
Toast.makeText(MainActivity.this, "電話為空了>.<", 0
--顯示的時間).show();--土司生成
return ;
}else{
Intent intent = new Intent();----打電話的程序重要的1點事必須啟動1個Intent的程序
Toast.makeText(MainActivity.this, "電話打向"+num, 0).show();--必須show出來
intent.setAction(Intent.ACTION_CALL);---設置打電話的設置
intent.setData(Uri.parse("tel:"+num));---設置電環號碼
startActivity(intent);---啟動事件
return;
}
}
private void MyCallxiaoyu(){
Intent intent = new Intent();
Toast.makeText(MainActivity.this, "Love曉宇691526", 0).show();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"691526"));
startActivity(intent);
return ;
}
private void CallWangqi (){
Intent intent = new Intent();
Toast.makeText(MainActivity.this, "打給討厭鬼王琪>.<", 0).show();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+"652008"));
startActivity(intent);
}
E:設置發短信的事件
private void SendFriend(){
String number = Nummber_edit.getText().toString();
String sms = Talk_Edit.getText().toString();
if(TextUtils.isEmpty(number)||TextUtils.isEmpty(sms)){---設置發短信的電話號碼和控件
Toast.makeText(MainActivity.this, "信息為空了>.<", 0).show();
return ;
}else{
Toast.makeText(MainActivity.this, "信息發向了"+number, 0).show();
SmsManager message = SmsManager.getDefault();
message.sendTextMessage(number--電話,
null--發信者, sms--內容, null,null);---發短信
Toast.makeText(MainActivity.this, "信息發送成功>.
生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
------分隔線----------------------------
------分隔線----------------------------