今天想把之前的android基础好好整理一下,没想到写这么简单的代码居然也会出问题
1.首先来个布局文件:main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number"
android:id="@+id/numberlabel"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:phoneNumber="true"
android:layout_toRightOf="@id/numberlabel"
android:layout_alignTop="@id/numberlabel"
android:hint="请输入号码"
android:paddingLeft="60dp"
android:layout_marginLeft="8dp"
android:id="@+id/number"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/number"
android:textSize="26sp"
android:textColor="#0066CC"
android:paddingLeft="2dp"
android:layout_alignTop="@id/number"
android:layout_alignLeft="@id/number"
android:layout_alignBottom="@id/number"
/>
</RelativeLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/content"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="3"
android:id="@+id/content"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button"
android:id="@+id/button"
/>
</LinearLayout>
2.清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="cn.ccy.com"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".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>
<uses-sdk android:minSdkVersion="8" />
<!-- 发短信权限 -->
<uses-permission android:name="android.permission.SEND_SMS"/>
</manifest>
3 MainActivity.java
package cn.ccy.com;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.telephony.SmsManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends Activity {
/** Called when the activity is first created. */
EditText et_number;
EditText et_sms;
Button btn_send;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et_number = (EditText) findViewById(R.id.et_number);
et_sms = (EditText) findViewById(R.id.et_sms);
btn_send = (Button) findViewById(R.id.btn_send);
btn_send.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
String number = et_number.getText().toString();// 发送的电话号码
String sms = et_sms.getText().toString();// 短信内容
SmsManager manager = SmsManager.getDefault();
//因为一条短信有字数限制,因此要将长短信拆分
ArrayList<String> divideMessage = manager.divideMessage(sms);
for (String text : divideMessage) {
manager.sendTextMessage(number, null, text, null, null);
}
Toast.makeText(MainActivity.this, "发送成功",Toast.LENGTH_SHORT).show();
}
});
}
}
String.xml文件我就不传了
今天这个程序运行不起来,项目上老有红叉,说是*.dex文件无法访问,折腾了两个小时,才知道,是360杀毒把这个bin文件下的*.dex文件给隔离了。恢复后才能远行
运行后,我改了一下界面,结果又跑不起来,还是360给弄的。后来直截关了360杀毒。才运行正常 。