禁用按钮一段时间

时间:2015-05-03 08:53:11

标签: java android button sms

我已经编写了以下代码来向个人发送短信...

Button but = (Button) a;
but.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        String messageToSend = "message";
        LoginDataBaseAdapter loginDataBaseAdapter = HomeActivity.loginDataBaseAdapter;
        car = ((EditText) b ).getText().toString();

        String phoneNum=loginDataBaseAdapter.getNum(car);
        if(phoneNum !="not") {
            SmsManager.getDefault().sendTextMessage(phoneNum, null, messageToSend, null, null);
            Toast.makeText(view.getContext(), "MESSAGE SUCCESSFULLY SENT",Toast.LENGTH_SHORT).show();
        }
        else {
            Toast.makeText(view.getContext(), "No Car Number found",Toast.LENGTH_SHORT).show();
        }
    }
});

发送邮件时,我想阻止用户在短时间内向同一用户发送另一条短信,例如禁用but按钮。谁能帮助我了解如何在一段时间内禁用按钮。

1 个答案:

答案 0 :(得分:0)

以下是如何实现此目的的简单方法:

 ArrayList<String> sentList = new ArrayList<>();

单击该按钮时,您可以使用方法

将数字或名称添加到ArrayList
sentList.add("Number");

再次点击按钮时,请检查:

for (String string : sentList){
    if (!string.equalsIgnoreCase(numberToCheck)) {
        //is new number - perform the action
    }
}

希望这就是你要找的东西。

修改

ArrayList<String> sentNumbers = new ArrayList<>();
Button but = (Button) a;
but.setOnClickListener(new View.OnClickListener() {
      public void onClick(View v) {
         String messageToSend = "message";
         LoginDataBaseAdapter loginDataBaseAdapter = HomeActivity.loginDataBaseAdapter;
        car = ((EditText) b ).getText().toString();
        String phoneNum = loginDataBaseAdapter.getNum(car);
        if(!sentNumbers.isEmpty(){
            for (String string : sentList){
                if (!string.equalsIgnoreCase(phoneNum)) {
                    SmsManager.getDefault().sendTextMessage(phoneNum, null, messageToSend, null, null);
                    sentNumbers.add(phoneNum);
                    Toast.makeText(view.getContext(), "MESSAGE SUCCESSFULLY SENT",Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(view.getContext(), "No Car Number found",Toast.LENGTH_SHORT).show();
                }
            }
        }else{
            SmsManager.getDefault().sendTextMessage(phoneNum, null, messageToSend, null, null);
            sentNumbers.add(phoneNum);
        }
    }
});

清除已使用号码的清单(我想有些事件必须发生?):

sentList.clear();