android程式設計實例入門 -Sample改寫分享 (2014/11/18)
此範例為程式碼(11\Sample01)的改寫,建立Button*1+TextView[3]+EditText[3]來實作GUI,利用Intent.ACTION_SENDTO實作寄送郵件(Send Email),程式碼如下所示:
package com.jashsample;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Random;
import android.media.MediaPlayer;
import android.media.MediaPlayer.OnCompletionListener;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.ContactsContract;
import android.app.Activity;
import android.app.AlarmManager;
import android.app.AlertDialog;
import android.app.PendingIntent;
import android.app.SearchManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Camera;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.hardware.Camera.Size;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.speech.RecognizerIntent;
import android.speech.tts.TextToSpeech;
import android.speech.tts.TextToSpeech.OnInitListener;
import android.text.format.DateFormat;
import android.text.method.KeyListener;
import android.view.Display;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.view.animation.AnimationSet;
import android.view.animation.RotateAnimation;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SimpleAdapter;
import android.widget.Spinner;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
/*
* 建立Button*1+TextView[3]+EditText[3],利用Intent.ACTION_SENDTO實作寄送郵件(Send Email)。
*/
public class MainActivity extends Activity {
Button bt;
TextView[] tv = new TextView[3];
EditText[] et = new EditText[3];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout tl = new TableLayout(this);
setContentView(tl);
Button bt = new Button(this);
for(int i=0; i<tv.length; i++)
{
tv[i] = new TextView(this);
}
for(int i=0; i<et.length; i++)
{
et[i] = new EditText(this);
et[i].setWidth(300);
}
tv[0].setText("收件者");
tv[1].setText("標題");
tv[2].setText("本文");
et[2].setLines(5);
bt.setText("傳送");
TableRow[] tr = new TableRow[3];
for(int i=0; i<tr.length; i++)
{
tr[i] = new TableRow(this);
tr[i].addView(tv[i]);
tr[i].addView(et[i]);
tl.addView(tr[i]);
}
tl.addView(bt);
bt.setOnClickListener(new SampleClickListener());
}
class SampleClickListener implements OnClickListener
{
public void onClick(View v)
{
Intent it = new Intent();
it.setAction(Intent.ACTION_SENDTO);
it.setData(Uri.parse( "mailto:" + et[0].getText().toString()));
it.putExtra(Intent.EXTRA_SUBJECT, et[1].getText().toString());
it.putExtra(Intent.EXTRA_TEXT, et[2].getText());
startActivity(it);
}
}
}
沒有留言:
張貼留言