1、xml
<LinearLayout
android:id="@+id/gesture_main_writepad_ll"
android:layout_width="fill_parent"
android:layout_height="330dip"
android:layout_alignParentBottom="true"
android:background="#262626"
android:orientation="vertical"
android:visibility="gone" >
<Gallery
android:id="@+id/gesture_main_writepad_glly"
android:layout_width="fill_parent"
android:layout_height="60dip"
android:spacing="3dip" />
<TextView
android:layout_width="fill_parent"
android:layout_height="1px"
android:background="@color/auto_color"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="230dip" >
<LinearLayout
android:id="@+id/search_no_input"
android:layout_width="fill_parent"
android:layout_height="230dip"
android:background="@android:color/transparent"
android:gravity="center" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="@android:color/transparent" />
</LinearLayout>
<android.gesture.GestureOverlayView
android:id="@+id/gesture_main_writepad_gs"
android:layout_width="fill_parent"
android:layout_height="230dip"
android:fadeOffset="100000"
android:gestureColor="#6B8E23"
android:gestureStrokeType="multiple"
android:gestureStrokeWidth="5" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="40dip"
android:orientation="horizontal" >
<Button
android:id="@+id/gesture_main_hide_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="3dip"
android:layout_weight="2"
android:background="#838B83"
android:text="hide" />
<Button
android:id="@+id/gesture_main_space_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="3dip"
android:layout_weight="1"
android:background="#838B83"
android:text="space" />
<Button
android:id="@+id/gesture_main_del_btn"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="3dip"
android:layout_weight="2"
android:background="#838B83"
android:text="del" />
</LinearLayout>
</LinearLayout>2、android
private View.OnClickListener handwritelistener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.gesture_main_handwrite_btn:
gesture_main_writepad_ll.setVisibility(View.VISIBLE);
gesture_main_handwrite_btn.setVisibility(View.GONE);
handInputLay.setVisibility(VISIBLE);
TextView tv=(TextView)handInputLay.getChildAt(0);
tv.setText(cont.getString(R.string.search_input));
editText.setFocusable(true);
editText.requestFocus();
break;
case R.id.gesture_main_hide_btn:
closeHandwritePad();
break;
case R.id.gesture_main_space_btn:
String spaceText = gesture_main_space_btn.getText().toString();
if (spaceText.equals("space")) {
editText.append(" ");
} else {
editText.append(spaceText);
gesture_main_writepad_gs.clear(false);
gesture_main_space_btn.setText("space");
gesture_main_writepad_glly.setAdapter(new GalleryAdapter(new ArrayList<String>(), cont));
}
break;
case R.id.gesture_main_del_btn:
gesture_main_writepad_gs.clear(false);
gesture_main_writepad_glly.setAdapter(new GalleryAdapter(new ArrayList<String>(), cont));
gesture_main_space_btn.setText("space");
break;
default:
break;
}
}
};
private GestureOverlayView.OnGestureListener gestureListener = new GestureOverlayView.OnGestureListener() {
private ArrayList<ArrayList<int[]>> allLists = new ArrayList<ArrayList<int[]>>();
private ArrayList<int[]> offsetList;
@Override
public void onGestureStarted(GestureOverlayView overlay, MotionEvent event) {
handInputLay.setVisibility(GONE);
if(!CndicUtil.checkWifiConnected(cont)){
handInputLay.setVisibility(VISIBLE);
TextView tv=(TextView)handInputLay.getChildAt(0);
tv.setText(cont.getString(R.string.search_no_network));
}
offsetList = new ArrayList<int[]>();
if (overlay.getGesture().getStrokesCount() == 0) {
allLists.clear();
}
}
@Override
public void onGestureEnded(GestureOverlayView overlay, MotionEvent event) {
allLists.add(offsetList);
StringBuffer inputStr = new StringBuffer();
inputStr.append("=R " + allLists.size() + "\n");
for (ArrayList<int[]> list : allLists) {
inputStr.append("=S " + list.size() + "\n");
for (int[] offset : list) {
inputStr.append(offset[0] + " " + offset[1] + " ");
}
inputStr.append("\n");
}
final String resUrl = gestureRecServerURL + URLEncoder.encode(inputStr.toString());
new Thread() {
@Override
public void run() {
try {
HttpGet hg = new HttpGet(resUrl);
DefaultHttpClient dh = new DefaultHttpClient();
HttpResponse hr = null;
hr = dh.execute(hg);
InputStream obtainedStr = hr.getEntity().getContent();
BufferedReader br = new BufferedReader(new InputStreamReader(obtainedStr));
StringBuffer brsb = new StringBuffer();
String line = "";
while ((line = br.readLine()) != null) {
brsb.append(line);
}
JSONObject dataJson = new JSONObject(brsb.toString());
String[] dataWithPinyin = dataJson.getString("content").split(";");
StringBuffer tempStrBuf = new StringBuffer();
ArrayList<String> tempStrForInputArray = new ArrayList<String>();
for (int i = 0; i < dataWithPinyin.length; i++) {
String tempStr = dataWithPinyin[i];
tempStrBuf.append(tempStr.split(",")[0] + " ");
tempStrForInputArray.add(tempStr.split(",")[0]);
}
strForInputArray = tempStrForInputArray;
handler.sendEmptyMessage(10);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
@Override
public void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {
}
@Override
public void onGesture(GestureOverlayView overlay, MotionEvent event) {
int curX = (int)event.getX();
int curY = (int)event.getY();
int[] offset = {curX, curY};
offsetList.add(offset);
}
};
private OnItemClickListener galleryItemListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
editText.append(strForInputArray.get(position));
gesture_main_writepad_gs.clear(false);
gesture_main_writepad_glly.setAdapter(new GalleryAdapter(new ArrayList<String>(), cont));
gesture_main_space_btn.setText("space");
}
};
private void alignGalleryToLeft(Gallery gallery) {
int width = gesture_main_writepad_ll.getWidth();
MarginLayoutParams mlp = (MarginLayoutParams)gallery.getLayoutParams();
mlp.setMargins(-(int)(width / 1.1), mlp.topMargin, mlp.rightMargin, mlp.bottomMargin);
gallery.setLayoutParams(mlp);
}
private void closeHandwritePad() {
gesture_main_handwrite_btn.setVisibility(View.VISIBLE);
gesture_main_writepad_gs.clear(false);
gesture_main_writepad_ll.setVisibility(View.GONE);
gesture_main_writepad_glly.setAdapter(new GalleryAdapter(new ArrayList<String>(), cont));
gesture_main_space_btn.setText("space");
editText.clearFocus();
}
private void setHandwritelistener() {
gesture_main_handwrite_btn.setOnClickListener(handwritelistener);
gesture_main_hide_btn.setOnClickListener(handwritelistener);
gesture_main_space_btn.setOnClickListener(handwritelistener);
gesture_main_del_btn.setOnClickListener(handwritelistener);
gesture_main_writepad_gs.addOnGestureListener(gestureListener);
gesture_main_writepad_glly.setOnItemClickListener(galleryItemListener);
}有问题的童鞋,回复后解答
本文将介绍如何在Android平台上开发一款自定义的手写输入法,包括XML布局设计和Android代码实现的关键步骤,帮助开发者理解并创建自己的输入法组件。
1151

被折叠的 条评论
为什么被折叠?



