Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.kpk.myapplicationfinal;
- import android.content.DialogInterface;
- import android.content.Intent;
- import android.support.v7.app.AlertDialog;
- import android.support.v7.app.AppCompatActivity;
- import android.os.Bundle;
- import android.text.TextUtils;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuItem;
- import android.view.SubMenu;
- import android.view.View;
- import android.widget.AdapterView;
- import android.widget.ArrayAdapter;
- import android.widget.EditText;
- import android.widget.ListView;
- import android.widget.TextView;
- import android.widget.Toast;
- import java.util.Random;
- public class MainActivity extends AppCompatActivity
- implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener {
- String[] aMemo = { // 預設的備忘內容
- "1. 按一下可以編輯備忘",
- "2. 長按可以清除備忘","3.","4.","5.","6.","7.","8.","9.","10.","11.","12.","13.","14.","15." };
- ListView lv; // 顯示備忘錄的 ListView
- ArrayAdapter<String> aa; // ListView 與備忘資料的橋樑
- TextView txv2;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- lv = (ListView)findViewById(R.id.listView);
- aa = new ArrayAdapter<>(this,
- android.R.layout.simple_list_item_1, aMemo);
- lv.setAdapter(aa); //設定 listView1 的內容
- //設定 listView1 被按一下的監聽器
- lv.setOnItemClickListener(this);
- //設定 listView1 被長按的監聽器
- lv.setOnItemLongClickListener(this);
- txv2= (TextView) findViewById(R.id.txv2);
- }
- public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
- Intent it = new Intent(this, tw.com.flag.ch08_memo.Edit.class);
- it.putExtra("備忘", aMemo[pos]); //附加編號
- // it.putExtra("備忘", aMemo[pos]); //附加備忘項目的內容
- startActivityForResult(it, pos); //啟動 Edit 並以項目位置為識別碼
- }
- public boolean onItemLongClick(AdapterView<?> a, View v, int pos, long id) {
- aMemo[pos] = (pos+1) + "."; //將內容清除 (只剩編號)
- aa.notifyDataSetChanged(); //通知 Adapter 要更新陣列內容
- return true; //傳回 true 表示此事件已處理
- }
- protected void onActivityResult(int requestCode, int resultCode, Intent it) {
- if(resultCode == RESULT_OK) {
- aMemo[requestCode] = it.getStringExtra("備忘"); // 使用傳回的資料更新陣列內容
- aa.notifyDataSetChanged(); // 通知 Adapter 陣列內容有更新
- }
- }
- public boolean onCreateOptionsMenu(Menu menu) {
- menu.add(Menu.NONE, Menu.FIRST, Menu.NONE, "抽獎");
- menu.add(Menu.NONE, Menu.FIRST + 1, Menu.NONE, "離開程式");
- return super.onCreateOptionsMenu(menu);
- }
- public boolean onOptionsItemSelected(MenuItem item) { //處理按下Menu Item以後的動作
- switch(item.getItemId()){
- case Menu.FIRST:
- // lv.getAdapter().getItem(new Random().nextInt(lv.getCount()));
- // lv.getAdapter().getItem(new Random().nextInt(lv.getCount()))).toString());
- AlertDialog.Builder bdr = new AlertDialog.Builder(this);
- bdr.setMessage((lv.getAdapter().getItem(new Random().nextInt(lv.getCount()))).toString());
- bdr.setTitle("您抽中了"); // 加入標題
- bdr.setIcon(android.R.drawable.btn_star_big_on); // 加入圖示
- bdr.setCancelable(true); // 允許按返回鍵關閉交談窗
- bdr.setPositiveButton("ok",null);
- bdr.show();
- break;
- case Menu.FIRST + 1:
- new AlertDialog.Builder(MainActivity.this)
- .setTitle("離開此程式")
- .setMessage("你確定要離開?")
- .setPositiveButton("是", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- finish();
- }
- })
- .setNegativeButton("否", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- }
- })
- .show();
- break;
- }
- return super.onOptionsItemSelected(item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment