主要是练习布局——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();
}
}