我一直试图在android中做一个POS应用程序,在该应用程序上,我在连接到该应用程序的蓝牙打印机上打印收据。我可以使用设备随附的HOINSDK打印“大”,“小”和QR码,也可以将“左”和“中心”对齐,但不能将其与“右”对齐。任何帮助表示赞赏。
下面是相应代码:
//For Large Text
cmd[0] = 27;
cmd[1] = 97;
cmd[2] &= 0xEF;
mService.write(cmd);
mService.sendMessage("" + large_txt.getText().toString(), "GBK");
//For Small Text
cmd[0] = 0x1b;
cmd[1] = 0x21;
cmd[2] &= 0xEF;
mService.write(cmd);
mService.sendMessage("" + small_txt.getText().toString(), "GBK");
//For Center Alignment small text
cmd[0] = 27;
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 27;
cmd[1] = 97;
cmd[2] = 1;
mService.write(cmd);
mService.sendMessage("" + center_txt.getText().toString(), "GBK");
//For centre alignment Large Text
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 0x1b;
cmd[1] = 0x21;
cmd[2] = 0x10;
mService.write(cmd);
mService.sendMessage("" + center_txt.getText().toString(), "GBK");
//For Left alignment
cmd[0] = 27;
cmd[1] = 97;
cmd[2] |= 1;
mService.write(cmd);
cmd[0] = 27;
cmd[1] = 97;
cmd[2] = 0;
mService.write(cmd);
mService.sendMessage("" + left_txt.getText().toString(), "GBK");
如果有人可以帮助我提供正确的对齐代码,那将是有帮助的。
答案 0 :(得分:0)
我之前也遇到过同样的问题,但是我没有找到任何解决方案,因此,我使用以下方法作为解决方案,
示例
String largetext = "Title";
String largeFontMessage = getWhiteSpace(24 - largetext.length()) + largetext;
String smalltext = "Message";
String smallFontMessage = getWhiteSpace(42 - smalltext.length()) + smalltext;
// print largeFontMessage;
// print smallFontMessage;
public static String getWhiteSpace(int size) {
StringBuilder builder = new StringBuilder(size);
for (int i = 0; i < size; i++) {
builder.append(' ');
}
return builder.toString();
}
以我为例,小文本包含42个字符,大文本包含24个字符。
谢谢。