每日总结 2.27
今天上了王老师的软件工程,老师为我们讲述了程序和软件的组成。
课上编写了单词接龙链的代码书写,思考如何进行代码编写。
今天学习了软键盘的编写。
<?xml version="1.0" encoding="utf-8"?> <Keyboard xmlns:android="http://schemas.android.com/apk/res/android" android:keyHeight="50dp" android:keyWidth="25%p" android:horizontalGap="1px" android:verticalGap="1px" > <Row> <Key android:codes="49" android:keyLabel="1"></Key> <Key android:codes="50" android:keyLabel="2"></Key> <Key android:codes="51" android:keyLabel="3"></Key> <Key android:codes="-5" android:keyLabel="删除"></Key> </Row> <Row> <Key android:codes="52" android:keyLabel="4"></Key> <Key android:codes="53" android:keyLabel="5"></Key> <Key android:codes="54" android:keyLabel="6"></Key> <Key android:codes="-4" android:keyHeight="150dp" android:keyLabel="确定"></Key> </Row> <Row> <Key android:codes="55" android:keyLabel="7"></Key> <Key android:codes="56" android:keyLabel="8"></Key> <Key android:codes="57" android:keyLabel="9"></Key></Row> <Row> <Key android:codes="-3" android:keyLabel="清零"></Key> <Key android:codes="48" android:keyLabel="0"></Key> <Key android:codes="46" android:keyLabel="."></Key></Row> </Keyboard>
下面是java文件;
package com.example.xx.util; import android.inputmethodservice.Keyboard; import android.inputmethodservice.KeyboardView; import android.text.Editable; import android.text.InputType; import android.view.View; import android.widget.EditText; import com.example.xx.R; public class KeyBordUtil { private final Keyboard keyboard; private KeyboardView keyboardView; private EditText editText; private int visibility; private int visibility1; public interface onEnsureListener{ public void onEnsure(); } onEnsureListener onEnsureListener; public void setOnEnsureListener(onEnsureListener onEnsureListener){ this.onEnsureListener=onEnsureListener; } public KeyBordUtil(KeyboardView keyboardView, EditText editText) { this.keyboardView = keyboardView; this.editText = editText; this.editText.setInputType(InputType.TYPE_NULL);//取消系统键盘 keyboard = new Keyboard(this.editText.getContext(), R.xml.key); this.keyboardView.setKeyboard(keyboard);//键盘样式 this.keyboardView.setEnabled(true); this.keyboardView.setPreviewEnabled(false); this.keyboardView.setOnKeyboardActionListener(listener);//键盘监听 } KeyboardView.OnKeyboardActionListener listener=new KeyboardView.OnKeyboardActionListener(){ public void onPress(int primaryCode){ } @Override public void onRelease(int i) { } @Override public void onKey(int i, int[] ints) { Editable editable=editText.getText(); int start = editText.getSelectionStart(); switch (i){ case Keyboard.KEYCODE_DELETE://删除 if(editable!=null&&editable.length()>0){ if(start> 0){ editable.delete(start-1,start); } } break; case Keyboard.KEYCODE_CANCEL://清零 editable.clear(); break; case Keyboard.KEYCODE_DONE://完场 onEnsureListener.onEnsure();//接口回调的方法,当 break; default://其他数字 editable.insert(start,Character.toString((char)i)); break; } } @Override public void onText(CharSequence charSequence) { } @Override public void swipeLeft() { } @Override public void swipeRight() { } @Override public void swipeDown() { } @Override public void swipeUp() { }
}; //显示键盘灯的方法 public void showKeyboard(){ visibility = keyboardView.getVisibility(); if (visibility == View.INVISIBLE||visibility==View.GONE) { keyboardView.setVisibility(View.VISIBLE); } } //隐藏键盘 public void hideKeyboard(){ visibility1 = keyboardView.getVisibility(); if (visibility1== View.VISIBLE||visibility1==View.INVISIBLE) { keyboardView.setVisibility(View.GONE); } } }
import java.io.BufferedWriter; import java.io.IOException; import java.util.ArrayList; public class FineWord { public ArrayList<String> words = new ArrayList<>(); public ArrayList<String> wordsList = new ArrayList<>(); public boolean compare(String a, String b) { a = a.toLowerCase(); b = b.toLowerCase(); return (a.substring(a.length() - 1).equals(b.substring(0, 1))); } public void fileSplit(String path) throws Exception { MyFile file = new MyFile(); String theFileString = file.get(path); if (theFileString == null) { return; } if (theFileString.equals("")) { throw new Exception("空文件"); } for (String word : theFileString.split("\\,|\\.| |\\(|\\)|\\;|\\:|\"|\\?|\\!|\\'| |\\、|\\”|\\“")) { if (!word.equals("")) { words.add(word); } } if (words.size() <= 1) { throw new Exception("文件内单词过少(只有" + words.size() + "个词)"); } } public void wordWrite(int index, String path) throws Exception { MyFile file = new MyFile(); BufferedWriter bf = file.put(path); wordsList.add(words.get(index)); try { for (String string : words) { if (compare(wordsList.get(wordsList.size() - 1), string)) { wordsList.add(string); bf.append(string); bf.newLine(); } } bf.close(); } catch (IOException e) { // TODO 自动生成的 catch 块 e.printStackTrace(); } if (wordsList.size() <= 1) { throw new Exception("文件内无单词链"); } } public static void main(String[] args) { FineWord aFineWord = new FineWord(); try { aFineWord.fileSplit("d://input1.txt"); aFineWord.wordWrite(0, "d://output1.txt"); System.out.println(aFineWord.wordsList); } catch (IOException e) { System.out.println("无此文件"); } catch (Exception e) { System.out.println(e.getMessage()); } } }