2013-02-12 35 views

回答

1
public class ContactActivity extends Activity implements OnClickListener { 

    ContactUtils contactUtils; 
    private EditText phoneEditText, nameEditText; 
    private Button saveButton; 
    private Spinner mContactPhoneTypeSpinner; 
    private ArrayList<Integer> mContactPhoneTypes; 
    private String phoneType; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    phoneEditText = (EditText) findViewById(R.id.phoneNumberEditText); 
    nameEditText = (EditText) findViewById(R.id.nameEditText); 
    mContactPhoneTypeSpinner = (Spinner) findViewById(R.id.contactPhoneTypeSpinner); 
    mContactPhoneTypes = new ArrayList<Integer>(); 
    mContactPhoneTypes 
     .add(ContactsContract.CommonDataKinds.Phone.TYPE_HOME); 
    mContactPhoneTypes 
     .add(ContactsContract.CommonDataKinds.Phone.TYPE_WORK); 
    mContactPhoneTypes 
     .add(ContactsContract.CommonDataKinds.Phone.TYPE_MOBILE); 
    mContactPhoneTypes 
     .add(ContactsContract.CommonDataKinds.Phone.TYPE_OTHER); 

    // Populate list of account types for phone 
    ArrayAdapter<String> adapter; 
    adapter = new ArrayAdapter<String>(this, 
     android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
    Iterator<Integer> iter; 
    iter = mContactPhoneTypes.iterator(); 
    while (iter.hasNext()) { 
     adapter.add(ContactsContract.CommonDataKinds.Phone.getTypeLabel(
      this.getResources(), iter.next(), 
      getString(R.string.undefinedTypeLabel)).toString() 
    ); 
    } 
    mContactPhoneTypeSpinner.setAdapter(adapter); 
    mContactPhoneTypeSpinner.setPrompt(getString(R.string.selectLabel)); 

    mContactPhoneTypeSpinner.setOnItemSelectedListener(new OnItemSelectedListener() { 

     @Override 
     public void onItemSelected(AdapterView<?> arg0, View arg1, 
      int arg2, long arg3) { 
     // TODO Auto-generated method stub 
     phoneType = "" + mContactPhoneTypes.get(arg2); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> arg0) { 
     // TODO Auto-generated method stub 
     } 
    }); 

    saveButton = (Button) findViewById(R.id.saveButton); 
    saveButton.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
    // TODO Auto-generated method stub 
    switch (v.getId()) { 
    case R.id.saveButton: 
     String phoneNumber = phoneEditText.getText().toString().trim(); 
     String name = nameEditText.getText().toString().trim(); 
     if (phoneNumber.length() < 0) { 
     Toast.makeText(this, getString(R.string.phoneNumberValidation), 
      Toast.LENGTH_SHORT).show(); 
     } else if (name.length() < 0) { 
     Toast.makeText(this, getString(R.string.nameValidation), 
      Toast.LENGTH_SHORT).show(); 
     } else { 
     contactUtils = new ContactUtils(this, phoneNumber, name, 
      phoneType); 
     } 
     break; 
    default: 
     break; 
    } 
    } 

    @Override 
    protected void onDestroy() { 
    // TODO Auto-generated method stub 
    super.onDestroy(); 
    } 
} 
相關問題