Skip to content

Commit 932897b

Browse files
committed
2018.11.16
完成详情和编辑时文字的行间距设置 修复Demo中的空指针异常
1 parent 143a44f commit 932897b

File tree

7 files changed

+71
-58
lines changed

7 files changed

+71
-58
lines changed

app/src/main/java/com/sendtion/xrichtextdemo/ui/NewActivity.java

Lines changed: 31 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,6 @@
5050
* 新建笔记
5151
*/
5252
public class NewActivity extends BaseActivity {
53-
private static final String TAG = "NewActivity";
54-
5553
private static final int REQUEST_CODE_CHOOSE = 23;//定义请求码常量
5654

5755
private EditText et_new_title;
@@ -156,32 +154,36 @@ public void onRtImageClick(String imagePath) {
156154
Intent intent = getIntent();
157155
flag = intent.getIntExtra("flag", 0);//0新建,1编辑
158156
if (flag == 1){//编辑
157+
setTitle("编辑笔记");
159158
Bundle bundle = intent.getBundleExtra("data");
160159
note = (Note) bundle.getSerializable("note");
161160

162-
myTitle = note.getTitle();
163-
myContent = note.getContent();
164-
myNoteTime = note.getCreateTime();
165-
Group group = groupDao.queryGroupById(note.getGroupId());
166-
myGroupName = group.getName();
167-
168-
loadingDialog = new ProgressDialog(this);
169-
loadingDialog.setMessage("数据加载中...");
170-
loadingDialog.setCanceledOnTouchOutside(false);
171-
loadingDialog.show();
172-
173-
setTitle("编辑笔记");
174-
tv_new_time.setText(note.getCreateTime());
175-
tv_new_group.setText(myGroupName);
176-
et_new_title.setText(note.getTitle());
177-
et_new_content.post(new Runnable() {
178-
@Override
179-
public void run() {
180-
//showEditData(note.getContent());
181-
et_new_content.clearAllLayout();
182-
showDataSync(note.getContent());
161+
if (note != null) {
162+
myTitle = note.getTitle();
163+
myContent = note.getContent();
164+
myNoteTime = note.getCreateTime();
165+
Group group = groupDao.queryGroupById(note.getGroupId());
166+
if (group != null){
167+
myGroupName = group.getName();
168+
tv_new_group.setText(myGroupName);
183169
}
184-
});
170+
171+
loadingDialog = new ProgressDialog(this);
172+
loadingDialog.setMessage("数据加载中...");
173+
loadingDialog.setCanceledOnTouchOutside(false);
174+
loadingDialog.show();
175+
176+
tv_new_time.setText(note.getCreateTime());
177+
et_new_title.setText(note.getTitle());
178+
et_new_content.post(new Runnable() {
179+
@Override
180+
public void run() {
181+
//showEditData(note.getContent());
182+
et_new_content.clearAllLayout();
183+
showDataSync(note.getContent());
184+
}
185+
});
186+
}
185187
} else {
186188
setTitle("新建笔记");
187189
if (myGroupName == null || "全部笔记".equals(myGroupName)) {
@@ -200,7 +202,7 @@ public void run() {
200202
private void closeSoftKeyInput(){
201203
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
202204
//boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
203-
if (imm.isActive()){
205+
if (imm != null && imm.isActive() && getCurrentFocus() != null){
204206
imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(),
205207
InputMethodManager.HIDE_NOT_ALWAYS);
206208
//imm.hideSoftInputFromInputMethod();//据说无效
@@ -216,11 +218,11 @@ private void closeSoftKeyInput(){
216218
private void openSoftKeyInput(){
217219
InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
218220
//boolean isOpen=imm.isActive();//isOpen若返回true,则表示输入法打开
219-
if (!imm.isActive()){
221+
if (imm != null && !imm.isActive() && et_new_content != null){
220222
et_new_content.requestFocus();
221223
//第二个参数可设置为0
222224
//imm.showSoftInput(et_content, InputMethodManager.SHOW_FORCED);//强制显示
223-
imm.showSoftInputFromInputMethod(getCurrentFocus().getWindowToken(),
225+
imm.showSoftInputFromInputMethod(et_new_content.getWindowToken(),
224226
InputMethodManager.SHOW_FORCED);
225227
}
226228
}
@@ -299,7 +301,7 @@ protected void showEditData(ObservableEmitter<String> emitter, String html) {
299301
*/
300302
private String getEditData() {
301303
List<RichTextEditor.EditData> editList = et_new_content.buildEditData();
302-
StringBuffer content = new StringBuffer();
304+
StringBuilder content = new StringBuilder();
303305
for (RichTextEditor.EditData itemData : editList) {
304306
if (itemData.inputStr != null) {
305307
content.append(itemData.inputStr);
@@ -324,7 +326,7 @@ private void saveNoteData(boolean isBackground) {
324326
if (noteTitle.length() == 0 ){//如果标题为空,则截取内容为标题
325327
if (noteContent.length() > cutTitleLength){
326328
noteTitle = noteContent.substring(0,cutTitleLength);
327-
} else if (noteContent.length() > 0 && noteContent.length() <= cutTitleLength){
329+
} else if (noteContent.length() > 0){
328330
noteTitle = noteContent;
329331
}
330332
}

app/src/main/java/com/sendtion/xrichtextdemo/ui/NoteActivity.java

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,11 @@ protected void onCreate(Bundle savedInstanceState) {
6666

6767
private void initView() {
6868
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_note);
69+
toolbar.setTitle("笔记详情");
6970
setSupportActionBar(toolbar);
70-
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
71+
if (getSupportActionBar() != null) {
72+
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
73+
}
7174
//toolbar.setNavigationIcon(R.drawable.ic_dialog_info);
7275
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
7376
@Override
@@ -120,23 +123,26 @@ public void onRtImageClick(String imagePath) {
120123
Bundle bundle = intent.getBundleExtra("data");
121124
note = (Note) bundle.getSerializable("note");
122125

123-
myTitle = note.getTitle();
124-
myContent = note.getContent();
125-
Group group = groupDao.queryGroupById(note.getGroupId());
126-
myGroupName = group.getName();
127-
128-
tv_note_title.setText(myTitle);
129-
tv_note_content.post(new Runnable() {
130-
@Override
131-
public void run() {
132-
//showEditData(myContent);
133-
tv_note_content.clearAllLayout();
134-
showDataSync(myContent);
126+
if (note != null) {
127+
myTitle = note.getTitle();
128+
myContent = note.getContent();
129+
Group group = groupDao.queryGroupById(note.getGroupId());
130+
if (group != null) {
131+
myGroupName = group.getName();
132+
tv_note_group.setText(myGroupName);
135133
}
136-
});
137-
tv_note_time.setText(note.getCreateTime());
138-
tv_note_group.setText(myGroupName);
139-
setTitle("笔记详情");
134+
135+
tv_note_title.setText(myTitle);
136+
tv_note_content.post(new Runnable() {
137+
@Override
138+
public void run() {
139+
//showEditData(myContent);
140+
tv_note_content.clearAllLayout();
141+
showDataSync(myContent);
142+
}
143+
});
144+
tv_note_time.setText(note.getCreateTime());
145+
}
140146

141147
}
142148

app/src/main/res/layout/content_new.xml

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:background="@color/grey_100"
99
android:orientation="vertical"
1010
app:layout_behavior="@string/appbar_scrolling_view_behavior"
11-
tools:context="com.sendtion.xrichtextdemo.ui.NewActivity"
11+
tools:context=".ui.NewActivity"
1212
tools:showIn="@layout/activity_new">
1313

1414
<EditText
@@ -34,22 +34,26 @@
3434
android:layout_height="wrap_content"
3535
android:textSize="@dimen/text_size_14"
3636
android:textColor="@color/grey_500"
37-
android:layout_alignParentLeft="true"/>
37+
android:layout_alignParentLeft="true"
38+
android:layout_alignParentStart="true"
39+
tools:text="2018-11-16"/>
3840

3941
<TextView
4042
android:id="@+id/tv_new_group"
4143
android:layout_width="wrap_content"
4244
android:layout_height="wrap_content"
4345
android:textSize="@dimen/text_size_14"
4446
android:textColor="@color/grey_500"
45-
android:layout_alignParentRight="true"/>
47+
android:layout_alignParentRight="true"
48+
android:layout_alignParentEnd="true"
49+
tools:text="默认笔记"/>
4650
</RelativeLayout>
4751

4852
<com.sendtion.xrichtext.RichTextEditor
4953
android:id="@+id/et_new_content"
5054
android:layout_width="match_parent"
5155
android:layout_height="match_parent"
52-
app:rt_editor_text_line_space="14dp"
56+
app:rt_editor_text_line_space="6dp"
5357
app:rt_editor_image_height="500"
5458
app:rt_editor_image_bottom="10"
5559
app:rt_editor_text_init_hint="在这里输入内容"

app/src/main/res/layout/content_note.xml

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
android:background="@color/grey_100"
99
android:orientation="vertical"
1010
app:layout_behavior="@string/appbar_scrolling_view_behavior"
11-
tools:context="com.sendtion.xrichtextdemo.ui.NoteActivity"
11+
tools:context=".ui.NoteActivity"
1212
tools:showIn="@layout/activity_note">
1313

1414
<TextView
@@ -33,22 +33,24 @@
3333
android:layout_height="wrap_content"
3434
android:textSize="@dimen/text_size_14"
3535
android:textColor="@color/grey_500"
36-
android:layout_alignParentLeft="true"/>
36+
android:layout_alignParentLeft="true"
37+
android:layout_alignParentStart="true" />
3738

3839
<TextView
3940
android:id="@+id/tv_note_group"
4041
android:layout_width="wrap_content"
4142
android:layout_height="wrap_content"
4243
android:textSize="@dimen/text_size_14"
4344
android:textColor="@color/grey_500"
44-
android:layout_alignParentRight="true"/>
45+
android:layout_alignParentRight="true"
46+
android:layout_alignParentEnd="true" />
4547
</RelativeLayout>
4648

4749
<com.sendtion.xrichtext.RichTextView
4850
android:id="@+id/tv_note_content"
4951
android:layout_width="match_parent"
5052
android:layout_height="match_parent"
51-
app:rt_editor_text_line_space="14dp"
53+
app:rt_view_text_line_space="6dp"
5254
app:rt_view_image_height="0"
5355
app:rt_view_image_bottom="10"
5456
app:rt_view_text_size="16sp"

xrichtext/src/main/java/com/sendtion/xrichtext/RichTextEditor.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,9 @@ public void insertImage(Bitmap bitmap, String imagePath) {
387387
public void hideKeyBoard() {
388388
InputMethodManager imm = (InputMethodManager) getContext()
389389
.getSystemService(Context.INPUT_METHOD_SERVICE);
390-
imm.hideSoftInputFromWindow(lastFocusEdit.getWindowToken(), 0);
390+
if (imm != null && lastFocusEdit != null) {
391+
imm.hideSoftInputFromWindow(lastFocusEdit.getWindowToken(), 0);
392+
}
391393
}
392394

393395
/**

xrichtext/src/main/res/layout/rich_edittext.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,5 @@
44
android:layout_height="wrap_content"
55
android:background="@null"
66
android:cursorVisible="true"
7-
android:lineSpacingExtra="8dp"
87
android:textSize="16sp"
98
android:textColor="#616161" />

xrichtext/src/main/res/layout/rich_textview.xml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
android:layout_width="match_parent"
44
android:layout_height="wrap_content"
55
android:background="@null"
6-
android:cursorVisible="true"
7-
android:lineSpacingExtra="8dp"
86
android:textIsSelectable="true"
97
android:textSize="16sp"
108
android:textColor="#616161" />

0 commit comments

Comments
 (0)