[android] 手机卫士保存安全号码

2019-09-10 14:41:03 浏览数 (1)

调用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

代码语言:javascript复制
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

代码语言:javascript复制
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();
            }
        });
    }
}

0 人点赞