android程式設計實例入門 -Sample改寫分享 (2014/11/04)
此範例為程式碼(08\Sample07)的改寫,建立兩個Activity,利用Intent在兩個Activity互傳訊息,程式碼如下所示:
package com.jashsample;
import java.io.File; import java.io.InputStream; import java.text.SimpleDateFormat; import java.util.ArrayList; 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.AlertDialog; 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.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.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; /* * 建立兩個Activity,利用Intent在兩個Activity互傳訊息。 */ /* * <activity android:name=".SampleInput" /> */ public class MainActivity extends Activity { public static int SAMPLE_APP = 1; ListView lv; Button bt1, bt2; ArrayList<HashMap<String, String>> data; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); setContentView(ll); lv = new ListView(this); bt1 = new Button(this); bt2 = new Button(this); bt1.setText("新建"); bt2.setText("刪除"); data = new ArrayList<HashMap<String, String>>(); String[] str = {"Title", "Todo"}; int[] ids = {android.R.id.text1, android.R.id.text2}; SimpleAdapter ad = new SimpleAdapter(this, data, android.R.layout.simple_list_item_single_choice, str,ids); lv.setAdapter(ad); lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE); ll.addView(bt1); ll.addView(bt2); ll.addView(lv); lv.setOnItemClickListener(new SampleItemClickListener()); bt1.setOnClickListener(new SampleClickListener());//import android.view.View.OnClickListener; bt2.setOnClickListener(new SampleClickListener());//import android.view.View.OnClickListener; } class SampleClickListener implements OnClickListener { public void onClick(View v) { if(v == bt1) { Intent it = new Intent(getApplicationContext(), SampleInput.class); int pos = data.size(); String title = "標題"; String todo = "TODO"; it.putExtra("Title", title); it.putExtra("Todo", todo); it.putExtra("Pos", pos); startActivityForResult(it, SAMPLE_APP); } else if(v == bt2) { int pos = lv.getCheckedItemPosition(); if(pos >= 0) { data.remove(pos); SimpleAdapter ad = (SimpleAdapter) lv.getAdapter(); ad.notifyDataSetChanged(); lv.setItemChecked(-1, true); } } } } class SampleItemClickListener implements OnItemClickListener { public void onItemClick(AdapterView<?>v, View iv, int pos, long id) { Intent it = new Intent(getApplicationContext(), SampleInput.class); HashMap<String, String> hm = data.get(pos); String title = hm.get("Title"); String todo = hm.get("Todo"); it.putExtra("Title", title); it.putExtra("Todo", todo); it.putExtra("Pos", pos); startActivityForResult(it, SAMPLE_APP); } } public void onActivityResult(int reqcode, int result, Intent it) { if(reqcode == SAMPLE_APP && result == RESULT_OK) { String title = it.getStringExtra("Title"); String todo = it.getStringExtra("Todo"); int pos = it.getIntExtra("Pos", 0); HashMap<String, String> hm= new HashMap<String, String>(); hm.put("Title", title); hm.put("Todo", todo); if(pos == data.size()) { data.add(pos, hm); } else { data.set(pos, hm); } SimpleAdapter ad = (SimpleAdapter) lv.getAdapter(); ad.notifyDataSetChanged(); } } }
|
package com.jashsample;
import com.jashsample.MainActivity.SampleClickListener;
import android.app.Activity; import android.view.View.OnClickListener; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout;
public class SampleInput extends Activity { EditText et1, et2; Button bt; String title, todo; int pos; Intent it; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout ll = new LinearLayout(this); ll.setOrientation(LinearLayout.VERTICAL); setContentView(ll); et1 = new EditText(this); et2 = new EditText(this); bt = new Button(this); bt.setText("OK"); it = getIntent(); title = it.getStringExtra("Title"); todo = it.getStringExtra("Todo"); pos = it.getIntExtra("Pos", 0); et1.setText(title); et2.setText(todo); ll.addView(et1); ll.addView(et2); ll.addView(bt); bt.setOnClickListener(new SampleClickListener());//import android.view.View.OnClickListener; } class SampleClickListener implements OnClickListener { public void onClick(View v) { it.putExtra("Title", et1.getText().toString()); it.putExtra("Todo", et2.getText().toString()); it.putExtra("Pos", pos); setResult(RESULT_OK, it); finish(); } } }
|
沒有留言:
張貼留言