android如何做自己的手寫IME

來源:互聯網
上載者:User

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() {@Overridepublic 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;@Overridepublic 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();}}@Overridepublic 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() {@Overridepublic 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();}@Overridepublic void onGestureCancelled(GestureOverlayView overlay, MotionEvent event) {}@Overridepublic 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() {@Overridepublic 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);}

有問題的童鞋,回複後解答

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.