Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android開發 >> 關於android開發 >> [android] 手機衛士保存安全號碼,android衛士

[android] 手機衛士保存安全號碼,android衛士

編輯:關於android開發

[android] 手機衛士保存安全號碼,android衛士


 

調用ListView對象的setOnItemClickListener()方法,設置條目的點擊事件,參數:OnItemClickListener對象

使用匿名內部類實現,重寫onClick()方法,傳遞進來的參數:ListView,當前View,position,id

根據索引位置把當前這條電話號碼傳回去

調用List對象的get()方法,得到Map,調用get(key)方法,得到這條電話

調用setResult()方法,返回去數據,參數:響應碼,Intent對象

獲取Intent對象,new出來

調用Intent對象的putExtra(key,val),把電話放進去

調用finish()方法關閉當前頁面

 

開啟這個的activity使用的是startActivityForResult()

重寫onActivityResult()方法,傳遞進來Intent對象

判斷Intent對象是否為null

調用Intent對象的getStringExtra()方法,獲取到電話號碼

把電話放在Edittext上,當點擊下一步的時候保存到SP中

activity

 

package com.qingguow.mobilesafe;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class SecGuide3Activity extends BaseSecGuideActivity {
    private EditText et_sec_phone;
    private SharedPreferences sp;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lost_find3);
        et_sec_phone = (EditText) findViewById(R.id.et_sec_phone);
        sp = getSharedPreferences("config", MODE_PRIVATE);
        String phone = sp.getString("secphone", "");
        et_sec_phone.setText(phone);
    }

    public void nextStep(View v) {
        showNext();
    }

    public void preStep(View v) {
        showPre();
    }

    @Override
    public void showPre() {

        Intent intent = new Intent(this, SecGuide2Activity.class);
        startActivity(intent);
        finish();
    }

    @Override
    public void showNext() {
        String phone = et_sec_phone.getText().toString().trim();

        if (TextUtils.isEmpty(phone)) {
            Toast.makeText(this, "請填寫安全號碼", 1).show();
            return;
        }

        Editor editor = sp.edit();
        editor.putString("secphone", phone);
        editor.commit();
        Intent intent = new Intent(this, SecGuide4Activity.class);
        startActivity(intent);
        finish();
    }

    /**
     * 選擇聯系人
     */
    public void selectContacts(View v) {
        Intent intent = new Intent(this, SelectContactsActivity.class);
        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null) {
            String phone = data.getStringExtra("phone").replace("-", "")
                    .replace(" ", "");
            et_sec_phone.setText(phone);
        }
    }
}

 

SelectContactsActivity

package com.qingguow.mobilesafe;

import java.util.List;
import java.util.Map;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.qingguow.mobilesafe.utils.PhoneContactsUtil;
/**
 * 選擇聯系人
 * @author taoshihan
 *
 */
public class SelectContactsActivity extends Activity {
    private ListView lv_select_contacts;
    private List<Map<String,String>> contacts;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_select_contacts);
        lv_select_contacts=(ListView) findViewById(R.id.lv_select_contacts);
        contacts=PhoneContactsUtil.getContacts(this);
        lv_select_contacts.setAdapter(new SimpleAdapter(this, contacts, R.layout.select_contacts_item, new String[]{"name","phone"}, new int[]{R.id.tv_contact_name,R.id.tv_contact_phone}));
        lv_select_contacts.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position,
                    long arg3) {
                String phone=contacts.get(position).get("phone");
                Intent data=new Intent();
                data.putExtra("phone", phone);
                setResult(0, data);
                finish();
            }
        });
    }
}

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved