Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/backbone.js/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android:通过微调器可以延迟消息_Android_Spinner_Delay - Fatal编程技术网

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

Android:通过微调器可以延迟消息,android,spinner,delay,Android,Spinner,Delay,用户从4个选项中选择延迟在微调器中的时间(例如,15、30、60秒或无延迟),然后当他单击发送按钮时,延迟将在消息真正发送之前生效,然后当消息发送时,将有一个祝酒词通知用户消息已发送 问题是,如何在微调器上实现延迟?它会被选中吗?那就推迟到那里?还是别的什么 代码如下: public class KAHTextApp extends Activity { Button btnRecipient; Button btnSend; EditText editTextRecip

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

问题是,如何在微调器上实现延迟?它会被选中吗?那就推迟到那里?还是别的什么

代码如下:

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();
            }
        });        
    }
公共类应用程序扩展活动{
按钮BTN接受者;
按钮BTN发送;
编辑文本编辑文本接收者;
编辑文本编辑新消息;
纺纱机;纺纱机;时间延迟;
专用静态最终int触点\u选择器\u结果=1001;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.compose_消息);
btnRecipient=(按钮)findViewById(R.id.Button\u recipient\u picker);
btnSend=(按钮)findviewbyd(R.id.Button\u send);
editTextRecipient=(EditText)findViewById(R.id.EditText\u recipient);
editTextNewMessage=(EditText)findViewById(R.id.EditText\u new\u message);
spinnerTimeDelay=(Spinner)findViewById(R.id.Spinner\u delay);
ArrayAdapter=ArrayAdapter.createFromResource(
这个,R.array.delay\u数组,android.R.layout.simple\u微调器\u项);
setDropDownViewResource(android.R.layout.simple\u微调器\u下拉菜单\u项);
喷丝头延时设置适配器(适配器);
spinnerTimeDelay.setOnItemSelectedListener(新的TimeDelay());
btnRecipient.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
意向意向=新意向(Intent.ACTION\u PICK,Contacts contract.Contacts.CONTENT\u URI);
startActivityForResult(意向、联系、挑选人、结果);
}
});
}
@凌驾
ActivityResult上的公共无效(int-reqCode、int-resultCode、意图数据){
super.onActivityResult(请求代码、结果代码、数据);
开关(reqCode){
案例(联系\u选取者\u结果):
if(resultCode==Activity.RESULT\u确定){
StringBuilder sb=新的StringBuilder();
Uri contactData=data.getData();
游标contactsCursor=managedQuery(contactData,
空,空,空,空);
如果(contactsUrsor.moveToFirst()){
String id=contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(Contacts.Contacts.\u ID));
String name=contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(Contacts.Contacts.DISPLAY_NAME));
String hasPhoneNumber=contactsCursor.getString(contactsCursor
.getColumnIndexOrThrow(Contacts.Contacts.HAS_电话号码));
某人(姓名);
if(Integer.parseInt(hasPhoneNumber)>0){
Uri myPhoneUri=Uri.withAppendedPath(
ContactsContract.CommonDataTypes.Phone.CONTENT\u URI,id);
游标phoneCursor=managedQuery(
myPhoneUri,null,null,null,null);
对于(phoneCursor.moveToFirst();!phoneCursor.isAfterLast();phoneCursor.moveToNext()){
String phoneNumber=phoneCursor.getString(phoneCursor
.getColumnIndexOrThrow(ContactsContract.CommonDataTypes.Phone.NUMBER));
某人附加(电话号码);
}
}否则{
sb.追加(“该联系人没有电话号码”);
}
editTextRecipient.setText(sb.toString());
}
}
打破
}
btnSend.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图v){
字符串phoneNo=editTextRecipient.getText().toString();
String message=editTextNewMessage.getText().toString();
if(phoneNo.length()>0&&message.length()>0)
Toast.makeText(getBaseContext(),
“消息已发送!”,
吐司。长度(短)。show();
/*发送短信(电话号码,信息);*/
其他的
Toast.makeText(getBaseContext(),
“请同时输入电话号码和信息。”,
吐司。长度(短)。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.
        }
    }
公共类延时实现了MSelectedListener{
已选择公共无效项(AdapterView父项,
视图,内部位置,长id){
Toast.makeText(parent.getContext(),“延迟为”+
parent.getItemAtPosition(pos.toString(),Toast.LENGTH_LONG.show();
}
未选择公共无效(AdapterView父级){
//什么也不做。
}
}
使用计时器(处理程序)进行延迟,意味着在其中编写发送和发送消息的代码,并根据延迟时间调用该函数(从微调器中选择),只需从微调器中选择一个值。使用该值调用函数(延迟)

这就是函数

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) {
        }
    });
ArrayAdapter fontArrayAdapter=新的ArrayAdapter(此,
R.layout.spinner_布局,fontArray);
fontArrayAdapter.setDropDownViewResource(R.layout.dropdown\U微调器);
font_spinner.setAdapter(fontArrayAdapter);
font_spinner.setOnItemSe
 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) {
        }
    });