Android:通过微调器延迟消息

时间:2011-10-27 06:00:26

标签: android spinner delay

用户在4个选项中选择延迟在微调器中的时间(例如,15,30,60秒或无延迟),然后当他点击发送按钮时,延迟将在消息真正发送之前生效然后当它发送时,会有一个祝酒词,通知用户他的消息已被发送。

问题是,如何实现微调器的延迟?它会在OnitemSelected中吗?那么推迟呢?或者无论如何?

以下是代码:

public class KAHTextApp extends Activity {
    Button btnRecipient;
    Button btnSend;
    EditText editTextRecipient;
    EditText editTextNewMessage;
    Spinner spinnerTimeDelay;

    private static final int CONTACT_PICKER_RESULT = 1001;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.compose_message);

        btnRecipient = (Button) findViewById(R.id.button_recipient_picker);
        btnSend = (Button) findViewById(R.id.button_send);
        editTextRecipient = (EditText) findViewById(R.id.editText_recipient);
        editTextNewMessage = (EditText) findViewById(R.id.editText_new_message);
        spinnerTimeDelay = (Spinner) findViewById(R.id.spinner_delay);

        ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
                this, R.array.delay_array, android.R.layout.simple_spinner_item);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        spinnerTimeDelay.setAdapter(adapter);
        spinnerTimeDelay.setOnItemSelectedListener(new TimeDelay());

        btnRecipient.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
                startActivityForResult(intent, CONTACT_PICKER_RESULT);
            }
        });
    }

    @Override
    public void onActivityResult(int reqCode, int resultCode, Intent data) {
        super.onActivityResult(reqCode, resultCode, data);

        switch (reqCode) {
            case (CONTACT_PICKER_RESULT):

                if (resultCode == Activity.RESULT_OK) {
                    StringBuilder sb = new StringBuilder();
                    Uri contactData = data.getData();
                    Cursor contactsCursor = managedQuery(contactData,
                            null, null, null, null);
                    if (contactsCursor.moveToFirst()) {
                        String id = contactsCursor.getString(contactsCursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts._ID));
                        String name = contactsCursor.getString(contactsCursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts.DISPLAY_NAME));
                        String hasPhoneNumber = contactsCursor.getString(contactsCursor
                                .getColumnIndexOrThrow(ContactsContract.Contacts.HAS_PHONE_NUMBER));
                        sb.append(name);
                        if (Integer.parseInt(hasPhoneNumber) > 0) {
                            Uri myPhoneUri = Uri.withAppendedPath(
                                    ContactsContract.CommonDataKinds.Phone.CONTENT_URI, id);
                            Cursor phoneCursor = managedQuery(
                                    myPhoneUri, null, null, null, null);
                            for (phoneCursor.moveToFirst(); !phoneCursor.isAfterLast(); phoneCursor.moveToNext()) {
                                String phoneNumber = phoneCursor.getString(phoneCursor
                                        .getColumnIndexOrThrow(ContactsContract.CommonDataKinds.Phone.NUMBER));
                                sb.append(phoneNumber);
                            }
                        } else {
                            sb.append("This contact doesn't have a phone number");
                        }
                        editTextRecipient.setText(sb.toString());
                    }
                }
                break;
            }

        btnSend.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                String phoneNo = editTextRecipient.getText().toString();
                String message = editTextNewMessage.getText().toString(); 

                if (phoneNo.length()>0 && message.length()>0)   
                    Toast.makeText(getBaseContext(), 
                            "Message sent!", 
                            Toast.LENGTH_SHORT).show();
                    /*sendSMS(phoneNo, message);  */              
                else
                    Toast.makeText(getBaseContext(), 
                        "Please enter both phone number and message.", 
                        Toast.LENGTH_SHORT).show();
            }
        });        
    }

这里是微调器的代码:

public class TimeDelay implements OnItemSelectedListener {

    public void onItemSelected(AdapterView<?> parent,
            View view, int pos, long id) {
          Toast.makeText(parent.getContext(), "The delay is " +
              parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
        }

        public void onNothingSelected(AdapterView<?> parent) {
          // Do nothing.
        }
    }

1 个答案:

答案 0 :(得分:0)

使用定时器(处理程序)进行延迟,意味着写入发送代码和Toast消息,并根据延迟时间(从微调器中选择)调用该函数,只需从微调器中选择一个值即可。通过使用此值调用函数(延迟)。

handler = new Handler(); 
handler.postDelayed(checkFunction(), n*1000);  // 1000 means 1 second
                                               // n is value selected from spinner. 

这是函数

private Runnable checkFunction(){ 
    tt = new TimerTask() {            
      public void run() { 
          handler.postDelayed(checkFunction(),1000);
              //// write code depending on your requirement..
      }
  };          
    return tt;
} 

spinner

 ArrayAdapter<MyFont> fontArrayAdapter = new ArrayAdapter<MyFont>(this, 
                                        R.layout.spinner_layout, fontArray);
    fontArrayAdapter.setDropDownViewResource(R.layout.dropdown_spinner);
    font_spinner.setAdapter(fontArrayAdapter);
    font_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
        public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
            MyFont f = fontArray[pos];
            String selected_font = f.getValue();
            System.out.println("selected one is "+selected_font);
            int item = pos;
            System.out.println("selected one is "+item);
        }
        public void onNothingSelected(AdapterView<?> parent) {
        }
    });