Android:可以通过微调器延迟消息

发布于 2024-12-12 01:52:20 字数 5135 浏览 0 评论 0原文

用户在旋转器中选择延迟多长时间(例如,15、30、60 秒或无延迟),然后当他单击发送按钮时,延迟将在消息真正发送之前生效,然后在消息真正发送时生效。发送时会有一个 toast 通知用户他的消息已发送。

问题是,如何在微调器上实现延迟?它会出现在 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.
        }
    }

The user chooses among the 4 choices on how long the delay is in a spinner (e.g, 15, 30, 60 seconds or no delay) then when he clicks on the send button the delay will take effect before the message really sends then when it sends there'll be a toast to notify the user that his message is sent.

The question is, how do I implement the delay on the spinner? Is it going to be in OnitemSelected? then delay it there? or whatsoever?

Here's the code:

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();
            }
        });        
    }

and here's the code for the spinner:

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.
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

白云悠悠 2024-12-19 01:52:20

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

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;
} 

微调器

 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) {
        }
    });

use the timer's (Handlers) for delay, means write the code for sending and toast message in that and call that function depending on the delay time ( selected from spinner), just select a value from the spinner. by using this value call the function (delay).

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

this is the function

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) {
        }
    });
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文