优化:自定义模板

自定义模板时,将标签插入光标位置或替换选中的文本
pull/103/head
Naccl 3 years ago
parent 10ec4329b1
commit ad89482b4f

@ -374,49 +374,49 @@ public class RuleActivity extends AppCompatActivity {
buttonInsertSender.setOnClickListener(view -> { buttonInsertSender.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{来源号码}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{来源号码}}");
}); });
Button buttonInsertContent = view1.findViewById(R.id.bt_insert_content); Button buttonInsertContent = view1.findViewById(R.id.bt_insert_content);
buttonInsertContent.setOnClickListener(view -> { buttonInsertContent.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{短信内容}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{短信内容}}");
}); });
Button buttonInsertSenderApp = view1.findViewById(R.id.bt_insert_sender_app); Button buttonInsertSenderApp = view1.findViewById(R.id.bt_insert_sender_app);
buttonInsertSenderApp.setOnClickListener(view -> { buttonInsertSenderApp.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{APP包名}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{APP包名}}");
}); });
Button buttonInsertContentApp = view1.findViewById(R.id.bt_insert_content_app); Button buttonInsertContentApp = view1.findViewById(R.id.bt_insert_content_app);
buttonInsertContentApp.setOnClickListener(view -> { buttonInsertContentApp.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{通知内容}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{通知内容}}");
}); });
Button buttonInsertExtra = view1.findViewById(R.id.bt_insert_extra); Button buttonInsertExtra = view1.findViewById(R.id.bt_insert_extra);
buttonInsertExtra.setOnClickListener(view -> { buttonInsertExtra.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{卡槽信息}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{卡槽信息}}");
}); });
Button buttonInsertTime = view1.findViewById(R.id.bt_insert_time); Button buttonInsertTime = view1.findViewById(R.id.bt_insert_time);
buttonInsertTime.setOnClickListener(view -> { buttonInsertTime.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{接收时间}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{接收时间}}");
}); });
Button buttonInsertDeviceName = view1.findViewById(R.id.bt_insert_device_name); Button buttonInsertDeviceName = view1.findViewById(R.id.bt_insert_device_name);
buttonInsertDeviceName.setOnClickListener(view -> { buttonInsertDeviceName.setOnClickListener(view -> {
textSmsTemplate.setFocusable(true); textSmsTemplate.setFocusable(true);
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
textSmsTemplate.append("{{设备名称}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{设备名称}}");
}); });
//正则替换 //正则替换
@ -433,6 +433,12 @@ public class RuleActivity extends AppCompatActivity {
} }
private void insertOrReplaceText2Cursor(EditText editText, String str) {
int start = Math.max(editText.getSelectionStart(), 0);
int end = Math.max(editText.getSelectionEnd(), 0);
editText.getText().replace(Math.min(start, end), Math.max(start, end), str, 0, str.length());
}
//当更新选择的字段的时候,更新之下各个选项的状态 //当更新选择的字段的时候,更新之下各个选项的状态
// 如果设置了转发全部,禁用选择模式和匹配值输入 // 如果设置了转发全部,禁用选择模式和匹配值输入
// 如果设置了多重规则,选择模式置为是 // 如果设置了多重规则,选择模式置为是

@ -356,24 +356,30 @@ public class SettingActivity extends AppCompatActivity {
textSmsTemplate.requestFocus(); textSmsTemplate.requestFocus();
switch (v.getId()) { switch (v.getId()) {
case R.id.bt_insert_sender: case R.id.bt_insert_sender:
textSmsTemplate.append("{{来源号码}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{来源号码}}");
return; return;
case R.id.bt_insert_content: case R.id.bt_insert_content:
textSmsTemplate.append("{{短信内容}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{短信内容}}");
return; return;
case R.id.bt_insert_extra: case R.id.bt_insert_extra:
textSmsTemplate.append("{{卡槽信息}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{卡槽信息}}");
return; return;
case R.id.bt_insert_time: case R.id.bt_insert_time:
textSmsTemplate.append("{{接收时间}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{接收时间}}");
return; return;
case R.id.bt_insert_device_name: case R.id.bt_insert_device_name:
textSmsTemplate.append("{{设备名称}}"); insertOrReplaceText2Cursor(textSmsTemplate, "{{设备名称}}");
return; return;
default: default:
} }
} }
private void insertOrReplaceText2Cursor(EditText editText, String str) {
int start = Math.max(editText.getSelectionStart(), 0);
int end = Math.max(editText.getSelectionEnd(), 0);
editText.getText().replace(Math.min(start, end), Math.max(start, end), str, 0, str.length());
}
//恢复初始化配置 //恢复初始化配置
public void initSetting(View view) { public void initSetting(View view) {

Loading…
Cancel
Save