一、电话拨号器
效果图:
布局:
<TextView android:id="@+id/tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="请输入电话号码:" /> <EditText android:id="@+id/et_num" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tv" android:layout_below="@+id/tv" android:inputType="phone" /> <Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/et_num" android:layout_below="@+id/et_num" android:text="拨号" />
代码:
package com.test.call; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button btn_call; private EditText et_num; String num; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_call = (Button) findViewById(R.id.btn_call); et_num = (EditText) findViewById(R.id.et_num); btn_call.setOnClickListener(new MyOnClickListener()); } private class MyOnClickListener implements OnClickListener{ @Override public void onClick(View v) { num = et_num.getText().toString().trim(); //intent对象 intent = new Intent(); //动作 intent.setAction(Intent.ACTION_CALL); //数据 intent.setData(Uri.parse("tel:"+num)); //开启 startActivity(intent); } } }
注意:别忘了在清单文件中添加权限
AndroidManifest.xml->Permissions->Add->Users Permission->Name:android.permission.CALL_PHONE
二、Android点击事件的四种写法
1.自定义一个点击事件监听类(代码同上)
2.采用匿名内部类的形式
package com.test.call; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private Button btn_call; private EditText et_num; String num; Intent intent; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_call = (Button) findViewById(R.id.btn_call); et_num = (EditText) findViewById(R.id.et_num); btn_call.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { num = et_num.getText().toString().trim(); intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:" + num)); startActivity(intent); } }); } }
3.使用当前Activity实现OnClickListener
package com.test.call; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity implements OnClickListener{ Button btn_call; EditText et_num; Intent intent; String num; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_call = (Button)findViewById(R.id.btn_call); btn_call.setOnClickListener(this); } @Override public void onClick(View v) { switch(v.getId()){ case R.id.btn_call: call(); break; default: break; } } private void call() { et_num = (EditText) findViewById(R.id.et_num); num = et_num.getText().toString().trim(); intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+num)); startActivity(intent); } }
4.布局文件显示制定OnClick属性
<Button android:id="@+id/btn_call" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/et_num" android:onClick="click" android:text="拨打" />
package com.test.call; import android.app.Activity; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { Button btn_call; EditText et_num; Intent intent; String num; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_call = (Button)findViewById(R.id.btn_call); } public void click(View v){ et_num = (EditText)findViewById(R.id.et_num); num = et_num.getText().toString().trim(); intent = new Intent(); intent.setAction(Intent.ACTION_CALL); intent.setData(Uri.parse("tel:"+num)); startActivity(intent); } }
三、短信发送器
<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/tv_input_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:text="@string/please_input_number" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/et_number" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/tv_input_number" android:ems="11" android:inputType="phone" > </EditText> <TextView android:id="@+id/tv_input_content" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/et_number" android:text="@string/please_input_content" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/et_content" android:lines="5" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@id/tv_input_content" android:ems="10" /> <Button android:id="@+id/btn_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@id/et_content" android:text="@string/send" /> </RelativeLayout>
package com.test.sms; import java.util.ArrayList; import android.app.Activity; import android.os.Bundle; import android.telephony.SmsManager; import android.text.TextUtils; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.EditText; import android.widget.Toast; public class MainActivity extends Activity implements OnClickListener { private Button btn_send; private EditText et_number; private EditText et_content; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn_send = (Button) findViewById(R.id.btn_send); et_number = (EditText) findViewById(R.id.et_number); et_content = (EditText) findViewById(R.id.et_content); btn_send.setOnClickListener(this); } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_send: String number = et_number.getText().toString().trim(); String content = et_content.getText().toString().trim(); if (TextUtils.isEmpty(number) || TextUtils.isEmpty(content)) { Toast.makeText(this, "号码或内容不能为空", Toast.LENGTH_SHORT).show(); return; } SmsManager smsManager = SmsManager.getDefault(); //若短信过长,则分割成多部分发送 ArrayList<String> contents = smsManager.divideMessage(content); for (String str : contents) { smsManager.sendTextMessage(number, null, str, null, null); } break; default: break; } } }
四、Android布局
相对布局: 可以以,值为true/false,的一种属性: android:layout_centerInParent="true" 在父窗体的中间
android:layout_centerHorizontal="true" 在父窗体水平居中 android:layout_centerVertical="true" 在父窗体内垂直居中
可以以,值为300dip等,为值的一种属性: android:layout_marginLeft="50dip" android:layout_marginRight="50dip" android:layout_marginTop="50dip" android:layout_marginBottom="50dip" 距离上下左右的大小 可以以,值为控件ID的,为值的一种属性: android:layout_above="@id/center" 在指定空间的上方, 注意ID android:layout_below="@id/center" 在指定控件的下方 android:layout_toRightOf="@id/center" 在指定控件的右方 android:layout_toLeftOf="@id/center" 在指定控件的左方 android:layout_alignLeft="@id/left" 和指定空间左对其 android:layout_alignRight="@id/right" 与指定控件右对齐 android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" 和父窗体上下左右对齐