Skip to content

Commit 4de8e0d

Browse files
committed
1.重新改写,不再需要传入parent,去掉scrollview
2.fix bug:issues #14
1 parent a58f19d commit 4de8e0d

File tree

12 files changed

+388
-268
lines changed

12 files changed

+388
-268
lines changed

app/src/main/AndroidManifest.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@
1818
<category android:name="android.intent.category.LAUNCHER"/>
1919
</intent-filter>
2020
</activity>
21+
<activity android:name=".FirstActivity"
22+
android:windowSoftInputMode="stateAlwaysHidden">
23+
</activity>
24+
<activity android:name=".SecondActivity"
25+
android:windowSoftInputMode="stateAlwaysHidden">
26+
</activity>
2127
</application>
2228

2329
</manifest>
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package com.stomhong.customkeyboard;
2+
3+
import android.os.Bundle;
4+
import android.view.KeyEvent;
5+
import android.widget.EditText;
6+
7+
import androidx.appcompat.app.AppCompatActivity;
8+
9+
import com.stomhong.library.KeyboardTouchListener;
10+
import com.stomhong.library.KeyboardUtil;
11+
12+
public class FirstActivity extends AppCompatActivity {
13+
14+
private EditText normalEd;
15+
private EditText specialEd;
16+
private KeyboardUtil keyboardUtil;
17+
18+
@Override
19+
protected void onCreate(Bundle savedInstanceState) {
20+
super.onCreate(savedInstanceState);
21+
setContentView(R.layout.activity_first);
22+
23+
normalEd = (EditText) findViewById(R.id.normal_ed);
24+
specialEd = (EditText) findViewById(R.id.special_ed);
25+
26+
initMoveKeyBoard();
27+
28+
29+
}
30+
31+
@Override
32+
public boolean onKeyDown(int keyCode, KeyEvent event) {
33+
//处理返回键
34+
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) {
35+
if(keyboardUtil.isShow){
36+
keyboardUtil.hideSystemKeyBoard();
37+
keyboardUtil.hideAllKeyBoard();
38+
keyboardUtil.hideKeyboardLayout();
39+
}else {
40+
return super.onKeyDown(keyCode, event);
41+
}
42+
43+
return false;
44+
} else
45+
return super.onKeyDown(keyCode, event);
46+
}
47+
48+
private void initMoveKeyBoard() {
49+
keyboardUtil = new KeyboardUtil(this);
50+
keyboardUtil.setOtherEdittext(normalEd);
51+
// monitor the KeyBarod state
52+
keyboardUtil.setKeyBoardStateChangeListener(new KeyBoardStateListener());
53+
// monitor the finish or next Key
54+
keyboardUtil.setInputOverListener(new inputOverListener());
55+
specialEd.setOnTouchListener(new KeyboardTouchListener(keyboardUtil, KeyboardUtil.INPUTTYPE_ABC, -1));
56+
}
57+
58+
class KeyBoardStateListener implements KeyboardUtil.KeyBoardStateChangeListener {
59+
60+
@Override
61+
public void KeyBoardStateChange(int state, EditText editText) {
62+
// System.out.println("state" + state);
63+
// System.out.println("editText" + editText.getText().toString());
64+
}
65+
}
66+
67+
class inputOverListener implements KeyboardUtil.InputFinishListener {
68+
69+
@Override
70+
public void inputHasOver(int onclickType, EditText editText) {
71+
// System.out.println("onclickType" + onclickType);
72+
// System.out.println("editText" + editText.getText().toString());
73+
}
74+
}
75+
}
Lines changed: 15 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,81 +1,40 @@
11
package com.stomhong.customkeyboard;
22

3+
import android.content.Intent;
34
import android.os.Bundle;
45
import androidx.appcompat.app.AppCompatActivity;
56
import android.view.KeyEvent;
7+
import android.view.View;
68
import android.widget.EditText;
9+
import android.widget.FrameLayout;
710
import android.widget.LinearLayout;
811
import android.widget.ScrollView;
912

1013
import com.stomhong.library.KeyboardTouchListener;
1114
import com.stomhong.library.KeyboardUtil;
1215

13-
public class MainActivity extends AppCompatActivity {
16+
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
1417

15-
private LinearLayout rootView;
16-
private ScrollView scrollView;
17-
private EditText normalEd;
18-
private EditText specialEd;
19-
private KeyboardUtil keyboardUtil;
2018

2119
@Override
2220
protected void onCreate(Bundle savedInstanceState) {
2321
super.onCreate(savedInstanceState);
2422
setContentView(R.layout.activity_main);
2523

26-
rootView = (LinearLayout) findViewById(R.id.root_view);
27-
scrollView = (ScrollView) findViewById(R.id.sv_main);
28-
29-
normalEd = (EditText) findViewById(R.id.normal_ed);
30-
specialEd = (EditText) findViewById(R.id.special_ed);
31-
32-
initMoveKeyBoard();
33-
34-
35-
}
36-
37-
@Override
38-
public boolean onKeyDown(int keyCode, KeyEvent event) {
39-
if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0 ) {
40-
if(keyboardUtil.isShow){
41-
keyboardUtil.hideSystemKeyBoard();
42-
keyboardUtil.hideAllKeyBoard();
43-
keyboardUtil.hideKeyboardLayout();
44-
}else {
45-
return super.onKeyDown(keyCode, event);
46-
}
47-
48-
return false;
49-
} else
50-
return super.onKeyDown(keyCode, event);
24+
findViewById(R.id.btn_test_activity).setOnClickListener(this);
25+
findViewById(R.id.btn_test_fragment).setOnClickListener(this);
5126
}
5227

53-
private void initMoveKeyBoard() {
54-
keyboardUtil = new KeyboardUtil(this, rootView, scrollView);
55-
keyboardUtil.setOtherEdittext(normalEd);
56-
// monitor the KeyBarod state
57-
keyboardUtil.setKeyBoardStateChangeListener(new KeyBoardStateListener());
58-
// monitor the finish or next Key
59-
keyboardUtil.setInputOverListener(new inputOverListener());
60-
specialEd.setOnTouchListener(new KeyboardTouchListener(keyboardUtil, KeyboardUtil.INPUTTYPE_ABC, -1));
61-
}
62-
63-
class KeyBoardStateListener implements KeyboardUtil.KeyBoardStateChangeListener {
64-
65-
@Override
66-
public void KeyBoardStateChange(int state, EditText editText) {
67-
// System.out.println("state" + state);
68-
// System.out.println("editText" + editText.getText().toString());
69-
}
70-
}
71-
72-
class inputOverListener implements KeyboardUtil.InputFinishListener {
7328

74-
@Override
75-
public void inputHasOver(int onclickType, EditText editText) {
76-
// System.out.println("onclickType" + onclickType);
77-
// System.out.println("editText" + editText.getText().toString());
29+
@Override
30+
public void onClick(View v) {
31+
switch (v.getId()) {
32+
case R.id.btn_test_activity:
33+
startActivity(new Intent(this,FirstActivity.class));
34+
break;
35+
case R.id.btn_test_fragment:
36+
startActivity(new Intent(this,SecondActivity.class));
37+
break;
7838
}
7939
}
80-
8140
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.stomhong.customkeyboard;
2+
3+
import android.os.Bundle;
4+
import android.widget.FrameLayout;
5+
6+
import androidx.annotation.Nullable;
7+
import androidx.appcompat.app.AppCompatActivity;
8+
9+
public class SecondActivity extends AppCompatActivity {
10+
11+
private FrameLayout mFrameLayout;
12+
13+
@Override
14+
protected void onCreate(@Nullable Bundle savedInstanceState) {
15+
super.onCreate(savedInstanceState);
16+
setContentView(R.layout.activity_second);
17+
18+
}
19+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.stomhong.customkeyboard;
2+
3+
import android.os.Bundle;
4+
import android.view.KeyEvent;
5+
import android.view.LayoutInflater;
6+
import android.view.View;
7+
import android.view.ViewGroup;
8+
import android.widget.EditText;
9+
10+
import androidx.annotation.NonNull;
11+
import androidx.annotation.Nullable;
12+
import androidx.fragment.app.Fragment;
13+
14+
import com.stomhong.library.KeyboardTouchListener;
15+
import com.stomhong.library.KeyboardUtil;
16+
17+
public class TestFragment extends Fragment {
18+
19+
private EditText normalEd;
20+
private EditText specialEd;
21+
private KeyboardUtil keyboardUtil;
22+
private View view;
23+
24+
@Nullable
25+
@Override
26+
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
27+
view = inflater.inflate(R.layout.fragment_test, container, false);
28+
return view;
29+
}
30+
31+
@Override
32+
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
33+
super.onActivityCreated(savedInstanceState);
34+
normalEd = (EditText) view.findViewById(R.id.normal_ed);
35+
specialEd = (EditText) view.findViewById(R.id.special_ed);
36+
37+
initMoveKeyBoard();
38+
}
39+
40+
private void initMoveKeyBoard() {
41+
keyboardUtil = new KeyboardUtil(getActivity());
42+
keyboardUtil.setOtherEdittext(normalEd);
43+
// monitor the KeyBarod state
44+
keyboardUtil.setKeyBoardStateChangeListener(new KeyBoardStateListener());
45+
// monitor the finish or next Key
46+
keyboardUtil.setInputOverListener(new inputOverListener());
47+
specialEd.setOnTouchListener(new KeyboardTouchListener(keyboardUtil, KeyboardUtil.INPUTTYPE_ABC, -1));
48+
}
49+
50+
class KeyBoardStateListener implements KeyboardUtil.KeyBoardStateChangeListener {
51+
52+
@Override
53+
public void KeyBoardStateChange(int state, EditText editText) {
54+
// System.out.println("state" + state);
55+
// System.out.println("editText" + editText.getText().toString());
56+
}
57+
}
58+
59+
class inputOverListener implements KeyboardUtil.InputFinishListener {
60+
61+
@Override
62+
public void inputHasOver(int onclickType, EditText editText) {
63+
// System.out.println("onclickType" + onclickType);
64+
// System.out.println("editText" + editText.getText().toString());
65+
}
66+
}
67+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
7+
<LinearLayout
8+
android:layout_width="match_parent"
9+
android:layout_height="wrap_content"
10+
android:orientation="vertical">
11+
12+
<EditText
13+
android:id="@+id/normal_ed"
14+
android:layout_width="match_parent"
15+
android:layout_height="wrap_content"
16+
android:hint="普通键盘" />
17+
18+
<EditText
19+
android:id="@+id/special_ed"
20+
android:layout_width="match_parent"
21+
android:layout_height="wrap_content"
22+
android:hint="特殊键盘" />
23+
24+
<TextView
25+
android:id="@+id/other_test"
26+
android:layout_width="match_parent"
27+
android:layout_height="wrap_content"
28+
android:text="@string/content"
29+
android:textSize="14sp"/>
30+
</LinearLayout>
31+
32+
</ScrollView>
Lines changed: 13 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,18 @@
11
<?xml version="1.0" encoding="utf-8"?>
22
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3-
android:layout_width="match_parent"
4-
android:layout_height="match_parent"
5-
android:id="@+id/root_view"
6-
android:orientation="vertical">
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
76

8-
<ScrollView
9-
android:id="@+id/sv_main"
10-
android:layout_width="match_parent"
11-
android:layout_height="0dp"
12-
android:layout_weight="1">
7+
<Button
8+
android:id="@+id/btn_test_activity"
9+
android:layout_width="wrap_content"
10+
android:layout_height="wrap_content"
11+
android:text="testActivity"/>
1312

14-
<LinearLayout
15-
android:id="@+id/all_ed"
16-
android:layout_width="match_parent"
17-
android:layout_height="wrap_content"
18-
android:orientation="vertical">
19-
<EditText
20-
android:id="@+id/normal_ed"
21-
android:layout_width="match_parent"
22-
android:layout_height="wrap_content"
23-
android:hint="普通键盘" />
24-
25-
<EditText
26-
android:id="@+id/special_ed"
27-
android:layout_width="match_parent"
28-
android:layout_height="wrap_content"
29-
android:hint="特殊键盘" />
30-
31-
<TextView
32-
android:id="@+id/other_test"
33-
android:layout_width="match_parent"
34-
android:text="Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors.Atom is a hackable text editor for the 21st century, built on Electron, and based on everything we love about our favorite editors."
35-
android:layout_height="wrap_content" />
36-
</LinearLayout>
37-
38-
</ScrollView>
13+
<Button
14+
android:id="@+id/btn_test_fragment"
15+
android:layout_width="wrap_content"
16+
android:layout_height="wrap_content"
17+
android:text="testFragment"/>
3918
</LinearLayout>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
3+
android:layout_width="match_parent"
4+
android:layout_height="match_parent"
5+
android:orientation="vertical">
6+
<fragment
7+
android:id="@+id/test_fragment"
8+
android:name="com.stomhong.customkeyboard.TestFragment"
9+
android:layout_width="match_parent"
10+
android:layout_height="match_parent"/>
11+
</FrameLayout>

0 commit comments

Comments
 (0)