首页 > 代码库 > Android(3)
Android(3)
1.电话拨号器:
activity_main.xml
<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" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="phone" > </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_below="@+id/editText1" android:text="拨打电话" />//注意:如果想让这个XML文件没有感叹号,则需要在strings.xml配置 </RelativeLayout>
package com.csdn.duil; import android.net.Uri; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.text.InputFilter.LengthFilter; import android.text.TextUtils; import android.view.Menu; 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 { /** * 点击事件方法 * 1.创建一个内部类定义点击事件 * 2.采用匿名内部类创建点击事件 * 3.接口里面未实现的方法(this)常见 * 4.在布局文件里面绑定一个点击的方法 */ private Button btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); //加载布局文件 setContentView(R.layout.activity_main); //查找到按钮 btn=(Button)this.findViewById(R.id.button1); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { EditText text=(EditText) MainActivity.this.findViewById(R.id.editText1); //获取文本去空格 String number=text.getText().toString().trim(); //判断number是否是空 if(TextUtils.isEmpty(number)){ Toast.makeText(MainActivity.this,"号码不能为空",Toast.LENGTH_SHORT).show(); return; } //意图 想干一件什么事情 Intent intent=new Intent(); //打人 intent.setAction(Intent.ACTION_CALL); //url统一资源定位符 uri更厉害,统一资源标识符 intent.setData(Uri.parse("tel:"+number)); 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; } }
2.短些发送器:
activity_main.xml
<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" > <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText2" android:layout_below="@+id/editText2" android:layout_marginTop="21dp" android:text="发送" /> <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignParentTop="true" android:text="@string/please_input_number" android:textAppearance="?android:attr/textAppearanceLarge" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView1" android:layout_marginTop="23dp" android:ems="10" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/editText1" android:layout_alignRight="@+id/editText1" android:layout_below="@+id/editText1" android:layout_marginTop="25dp" android:text="请输入内容" android:textAppearance="?android:attr/textAppearanceLarge" android:textColor="#ff0000" /> <EditText android:lines="5" android:id="@+id/editText2" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_below="@+id/textView2" android:layout_marginTop="25dp" android:ems="10" android:inputType="textMultiLine" > <requestFocus /> </EditText> </RelativeLayout>
MainActivity.java
package com.csdn.sms;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.telephony.SmsManager;
import android.text.TextUtils;
import android.view.Menu;
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 text1;
private EditText text2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
text1=(EditText) this.findViewById(R.id.editText1);
text2=(EditText) this.findViewById(R.id.editText2);
btn_send=(Button) this.findViewById(R.id.button1);
btn_send.setOnClickListener(this);
}
@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;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button1:
String number=text1.getText().toString().trim();
String context=text2.getText().toString().trim();
if(TextUtils.isEmpty(context)||TextUtils.isEmpty(number)){
Toast.makeText(this, "电话号码或者内容不能为空",Toast.LENGTH_SHORT).show();
return;
}else{
SmsManager smsManager=SmsManager.getDefault();
//彩信
//smsManager.sendTextMessage(number,null, context, null, null);
//发送多条短信
ArrayList<String> contents=smsManager.divideMessage(context);
for(String str:contents){
smsManager.sendTextMessage(number,null, str, null, null);
}
}
break;
default:
break;
}
}
}
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。