我正在JavaFX8中开发一个SMS发送应用程序。 我需要帮助调整我的短信计数器功能。
一些背景信息:
一条短信可以包含160个字符。如果超过160个字符,则它将成为多部分SMS。所以第二条SMS可以包含146个字符,第三个包含153个字符。从第三条短信开始,每条短信可以包含153个字符。
所以它会像160 - > 146 - > 153 - > 153 - > 153 ......
现在我想从160倒数到0,然后从146倒数到0再到153倒数。
这是我到目前为止所做的:
smsComposeArea.textProperty().addListener((ObservableValue<? extends String> observable, String oldValue, String newValue) -> {
//Replace all multiple whitspaces with one whitespace
smsComposeArea.setText(smsComposeArea.getText().replaceAll("[^\\S\\r|\\n|\\r\\n]+", " "));
//GSM 03.38 Extended charset ^{}\[~]|€ take up 2 characters - count them as 2 chars. Replace multiple tabs, newlines, whitespaces with one.
int charCount = smsComposeArea.getText().replaceAll("[\\^{}\\\\\\[~\\]|€]{1}", "$0$0").replaceAll("(\\r|\\n|\\r\\n|\\s)+", " ").length();
int countDown;
if(smsComposeArea.getText() == null && smsComposeArea.getText().trim().isEmpty()) {
lblCharCounter.setText("160");
}
else if(charCount >= 1 && charCount <= 160) {
countDown = 160 - charCount;
lblCharCounter.setText(Integer.toString(countDown));
}
else if(charCount >= 160 && charCount <= 306) {
countDown = 146 - (charCount - 160);
lblCharCounter.setText(String.format("2 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 306 && charCount <= 459) {
countDown = 153 - (charCount - 306);
lblCharCounter.setText(String.format("3 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 459 && charCount <= 612) {
countDown = 153 - (charCount - 459);
lblCharCounter.setText(String.format("4 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 612 && charCount <= 765) {
countDown = 153 - (charCount - 612);
lblCharCounter.setText(String.format("5 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 765 && charCount <= 918) {
countDown = 153 - (charCount - 765);
lblCharCounter.setText(String.format("6 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 918 && charCount <= 1071) {
countDown = 153 - (charCount - 918);
lblCharCounter.setText(String.format("7 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1071 && charCount <= 1224) {
countDown = 153 - (charCount - 1071);
lblCharCounter.setText(String.format("8 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1224 && charCount <= 1377) {
countDown = 153 - (charCount - 1224);
lblCharCounter.setText(String.format("9 SMS %s", Integer.toString(countDown)));
}
else if(charCount >= 1377 && charCount <= 1530) {
countDown = 153 - (charCount - 1377);
lblCharCounter.setText(String.format("10 SMS %s", Integer.toString(countDown)));
}
}
);
它按预期工作,但我认为应该有更复杂或动态的方式来显示倒计时。在我看来,IF语句太多了。
感谢任何帮助!
答案 0 :(得分:1)
https://github.com/messente/sms-length-calculator提供免费的短信长度计算库,用于检测编码,并在多部分短信中为扩展的gsm 7bit字符集处理尾随转义字符。
您还可以在http://messente.com/documentation/sms-length-calculator
学习和试验在线短信长度计算器答案 1 :(得分:0)
我对SMS协议一无所知,但假设您的背景信息正确,您可以这样做:
IntegerBinding textLength = Bindings.length(smsComposeArea.textProperty());
IntegerBinding numberOfSms = Bindings.createIntegerBinding(() -> {
if (textLength.get() <= 160) {
return 1 ;
} else if (textLength.get() <= 306) {
return 2 ;
} else {
return 3 + (textLength.get() - 307) / 153 ;
}
}, textLength);
IntegerBinding remainingChars = Bindings.createIntegerBinding(() -> {
if (numberOfSms.get() == 1) {
return 160 - textLength.get() ;
} else if (numberOfSms.get() == 2) {
return 306 - textLength.get() ;
} else {
return 306 + (numberOfSms.get() - 2) * 153 - textLength.get() ;
}
}, numberOfSms, textLength);
lblCharCounter.textProperty().bind(Bindings.format("%d SMS %d", numberOfSms, remainingChars));
如果@ RealSkeptic的答案中的计算是正确的,那么你可以
IntegerBinding textLength = Bindings.length(smsComposeArea.textProperty());
IntegerBinding numberOfSms = Bindings.createIntegerBinding(() -> {
if (textLength.get() <= 160) {
return 1 ;
} else {
return 1 + (textLength.get() - 1) / 153 ;
}
}, textLength);
IntegerBinding remainingChars = Bindings.createIntegerBinding(() -> {
if (numberOfSms.get() == 1) {
return 160 - textLength.get() ;
} else {
return numberOfSms.get() * 153 - textLength.get() ;
}
}, numberOfSms, textLength);
lblCharCounter.textProperty().bind(Bindings.format("%d SMS %d", numberOfSms, remainingChars));
或者可能(总共有if
个陈述)
IntegerBinding textLength = Bindings.length(smsComposeArea.textProperty());
BooleanBinding singleSMS = textLength.lessThanOrEqualTo(160);
IntegerBinding numberOfSms = Bindings.when(singleSMS).then(1).otherwise(
textLength.subtract(1).divide(153).add(1));
IntegerBinding remainingChars = Bindings.when(singleSMS).then(textLength.subtract(160).multiply(-1)).otherwise(
numberOfSms.multiply(153).subtract(textLength));
lblCharCounter.textProperty().bind(Bindings.format("%d SMS %d", numberOfSms, remainingChars));
取决于您喜欢的风格。
答案 2 :(得分:0)
假设您使用8位SMS编码,其中每个字符占用一个字节,会发生什么:
这意味着您根本不应该有很多“if”语句:
if ( charCount <= 160 ) {
nSegments = 1;
countDown = 160 - charCount;
} else if ( charCount % 153 == 0 ) {
nSegments = charCount / 153;
countDown = 0;
} else {
nSegments = charCount / 153 + 1;
countDown = nSegments * 153 - charCount;
}
lblCharCounter.setText(String.format("%d SMS %d", nSegments, countDown) );