主要是练习布局——LinearLayout(线性布局)

AbsoluteLayout 绝对布局 TableLayout表格布局 FrameLayout布局

================ 拨号器 =====================

1、AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.peter.phone" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.peter.phone.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-permission android:name="android.permission.CALL_PHONE"/>
</manifest>

------------------------------------------------------------------------------------------------------------

2、activity_main.xml

LinearLayout的android:orientation="vertical"必不可少......

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" >
<TextView android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="@string/phone" />
<EditText android:layout_width="match_parent" android:layout_height="wrap_content"
android:id="@+id/phone_num"/>
<Button android:layout_width="match_parent" android:layout_height="wrap_content"
android:text="@string/dial_btn" android:id="@+id/dial_btn"/>
</LinearLayout>

---------------------------------------------------------------------------------------------------------------

3、MainActivity.java

public class MainActivity extends Activity {
private EditText phone_num = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.dial_btn);
/*button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
phone_num = (EditText)findViewById(R.id.phone_num);
Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:"+phone_num.getText().toString()));
startActivity(intent);
//方法内部会自动为Intent添加类别:android.intent.category.DEFAULT
}
});*/
button.setOnClickListener(new ButtonOnClickListener());
}
/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}*/
public final class ButtonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
phone_num = (EditText)findViewById(R.id.phone_num);
/*Intent intent = new Intent(Intent.ACTION_CALL,
Uri.parse("tel:"+phone_num.getText().toString())); **/
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:"+phone_num.getText().toString()));
startActivity(intent);
}
}
}

================短信发送器 =====================

/AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.peter.sms" android:versionCode="1" android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" android:targetSdkVersion="8" />
<application
android:allowBackup="true" android:icon="@drawable/ic_launcher"
android:label="@string/app_name" android:theme="@style/AppTheme" >
<activity android:name="com.peter.sms.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-permission android:name="android.permission.SEND_SMS"/>
</manifest>

-----------------------------------------------------------------------------------------------

2、activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical" tools:context=".MainActivity" >

<RelativeLayout android:layout_width="fill_parent" android:layout_height="40dp">
<TextView android:layout_width="wrap_content" android:id="@+id/numberlabel"
android:layout_centerVertical="true"
android:layout_height="wrap_content" android:text="@string/phone_num" />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/phone_num"
android:layout_toRightOf="@id/numberlabel"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
</RelativeLayout>

<TextView android:layout_width="match_parent"
android:layout_height="wrap_content" android:text="@string/content" />
<EditText android:layout_width="match_parent"
android:layout_height="wrap_content" android:id="@+id/content"/>

<Button android:layout_width="wrap_content" android:text="@string/sms_send"
android:layout_height="wrap_content" android:id="@+id/button" />
</LinearLayout>

-------------------------------------------------------------------------------------

3、MainActivity.java

public class MainActivity extends Activity {
EditText phone_num = null;
EditText content = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button)findViewById(R.id.button);
phone_num = (EditText)findViewById(R.id.phone_num);
content = (EditText)findViewById(R.id.content);
button.setOnClickListener(new ButtonOnClickListener());
}

public final class ButtonOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
String mobile = phone_num.getText().toString();
String contentStr = content.getText().toString();
PendingIntent pendingIntent = PendingIntent
.getBroadcast(MainActivity.this, 0, new Intent(), 0);
SmsManager manager = SmsManager.getDefault();
ArrayList<String> texts = manager.divideMessage(contentStr);
for(String text : texts){
manager.sendTextMessage(mobile, null, text, pendingIntent, null);
}
Log.i("MainActivity", "mobile="+mobile+"; content="+contentStr);
Toast.makeText(MainActivity.this,R.string.success,Toast.LENGTH_LONG).show();
}
}

更多相关文章

  1. Android四大组件之activity之间带数据跳转
  2. Android(安卓)使用BroadCast实现强制下线功能
  3. Android(安卓)之 五大布局案例
  4. 【转】 Array ,Simple ,SimpleCursor Adapter 区别
  5. Drawer实现左右侧滑DrawerLayout
  6. Android(安卓)CardView+RecyclerView实现卡片列表
  7. Android(安卓)pulltorefresh上拉下拉刷新加载
  8. Android(安卓)布局之GridLayout
  9. android之switch控件的用法

随机推荐

  1. Android(安卓)Studio之Gradle多渠道打包
  2. Android NDK开发之旅16--NDK--文件拆分与
  3. Android的单线程模型
  4. Android 调用系统播放器,调用系统Camera
  5. Android实现双屏异显
  6. 制作不暴露源码的Android(安卓)Library P
  7. [Android] AOSP 开发并刷入Pixel
  8. 【Android】AS报错:Configuration on dema
  9. Download Android(安卓)Source Code in W
  10. android 官方文档中的一些错误收集