我想制作一个从 EDITTEXT 获取价值的小应用程序,当单击 BUTTON 时在 SPINNER 中执行一个动作(定义为微调器项)...... onclick 动作是 sendMessage(查看视图)......有人可以告诉我如何写这段代码..到目前为止我得到了这么多
public class MainActivity extends Activity implements OnItemSelectedListener{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addItemsOnSpinner();
}
public void addItemsOnSpinner() {
Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
ArrayAdapter<CharSequence> adapter=ArrayAdapter.createFromResource(this, R.array.selection_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void sendMessage(View view) {
EditText edittext=(EditText) findViewById(R.id.edit_message);
String message= edittext.getText().toString();
public void onItemSelected(AdapterView<?> parent, View view,int pos, long id) {
Spinner spinner=(Spinner)findViewById(R.id.selection_spinner);
String select=spinner.getSelectedItem().toString();
if(select.equals("Call")){
System.out.println(select);
// Build the intent(Call)
Uri number = Uri.parse(message);
Intent callIntent = new Intent(Intent.ACTION_CALL, number);
// Verify it resolves
PackageManager packageManager = getPackageManager();
List<ResolveInfo> activities = packageManager.queryIntentActivities(callIntent, 0);
boolean isIntentSafe = activities.size() > 0;
// Start an activity if it's safe
if (isIntentSafe) {
startActivity(callIntent);
}
}
if(select.equalsIgnoreCase("E-Mail to")){
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
startActivityForResult(intent, 0);
}
Toast.makeText(MainActivity.this, "The value selected is" + select, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "The position is" +pos, Toast.LENGTH_LONG).show();
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode== 0){
try{
if(resultCode==Activity.RESULT_OK){
Uri contactData = data.getData();
Cursor c = getContentResolver().query(contactData, null, null, null, null);
// has result
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));
String id = c.getString(c.getColumnIndex(ContactsContract.Contacts._ID));
Toast.makeText(MainActivity.this, "The name is" +name, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "The id is" + id, Toast.LENGTH_LONG).show();
// new query for the mail
Cursor emailCursor = getContentResolver().query(ContactsContract.CommonDataKinds.Email.CONTENT_URI, null, ContactsContract.CommonDataKinds.Email.CONTACT_ID + " = ?", new String[]{id}, null);
//has result
if(emailCursor.moveToFirst()) {
String email = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.DATA));
String emailType = emailCursor.getString(emailCursor.getColumnIndex(ContactsContract.CommonDataKinds.Email.TYPE));
Toast.makeText(MainActivity.this, "The email is" +email, Toast.LENGTH_LONG).show();
Toast.makeText(MainActivity.this, "The emailtype is" +emailType, Toast.LENGTH_SHORT).show();
}
emailCursor.close();
}
c.close();
}
}
catch(Exception e){
e.getCause();
}
}
}