首页 > 技术文章 > Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input)

ArrozZhu 2018-01-31 00:24 原文

 一、应用名称

Android简易项目--傻瓜式阿拉伯语输入法(Dummy Arabic Input)

 

二、应用图标

 

 三、应用说明

 

现在通行的阿拉伯语键盘布局并无规律可循,阿拉伯语使用者需要花费较多时间才能掌握指法。这款傻瓜式阿拉伯语输入法依照阿语字母排序,可满足基本的阿语输入需求;使用者无需学习,可立即上手。

 

四、项目结构

 

五、主要代码

src/com.example.dummy_arabic_input/DummyArabicInputService.java

  1 package com.example.dummy_arabic_input;
  2 
  3 import android.inputmethodservice.InputMethodService;
  4 import android.util.Log;
  5 import android.view.View;
  6 import android.view.View.OnClickListener;
  7 import android.view.inputmethod.EditorInfo;
  8 import android.view.inputmethod.InputConnection;
  9 import android.widget.Button;
 10 
 11 public class DummyArabicInputService extends InputMethodService implements
 12         OnClickListener
 13 {
 14     
 15     @Override
 16     public void onCreate()//不用写(Bundle savedInstanceState)
 17     //因为这里没有Activity界面
 18     {
 19         super.onCreate();
 20         Log.d("dummy_arabic_input_onCreate", "invoked");
 21     }
 22 
 23     @Override
 24     public View onCreateInputView()
 25     {
 26         View view = getLayoutInflater().inflate(R.layout.arabic_keyboard, null);
 27         //LayoutInflater is a class used to instantiate layout XML 
 28         //file into its corresponding View objects.
 29         //inflate(int resource, ViewGroup root)
 30         view.findViewById(R.id.btn1).setOnClickListener(this);
 31         view.findViewById(R.id.btn2).setOnClickListener(this);
 32         view.findViewById(R.id.btn3).setOnClickListener(this);
 33         view.findViewById(R.id.btn4).setOnClickListener(this);
 34         view.findViewById(R.id.btn5).setOnClickListener(this);
 35         view.findViewById(R.id.btn6).setOnClickListener(this);
 36         view.findViewById(R.id.btn7).setOnClickListener(this);
 37         view.findViewById(R.id.btn8).setOnClickListener(this);
 38         view.findViewById(R.id.btn9).setOnClickListener(this);
 39         view.findViewById(R.id.btn10).setOnClickListener(this);
 40         view.findViewById(R.id.btn11).setOnClickListener(this);
 41         view.findViewById(R.id.btn12).setOnClickListener(this);
 42         view.findViewById(R.id.btn13).setOnClickListener(this);
 43         view.findViewById(R.id.btn14).setOnClickListener(this);
 44         view.findViewById(R.id.btn15).setOnClickListener(this);
 45         view.findViewById(R.id.btn16).setOnClickListener(this);
 46         view.findViewById(R.id.btn17).setOnClickListener(this);
 47         view.findViewById(R.id.btn18).setOnClickListener(this);
 48         view.findViewById(R.id.btn19).setOnClickListener(this);
 49         view.findViewById(R.id.btn20).setOnClickListener(this);
 50         view.findViewById(R.id.btn21).setOnClickListener(this);
 51         view.findViewById(R.id.btn22).setOnClickListener(this);
 52         view.findViewById(R.id.btn23).setOnClickListener(this);
 53         view.findViewById(R.id.btn24).setOnClickListener(this);
 54         view.findViewById(R.id.btn25).setOnClickListener(this);
 55         view.findViewById(R.id.btn26).setOnClickListener(this);
 56         view.findViewById(R.id.btn27).setOnClickListener(this);
 57         view.findViewById(R.id.btn28).setOnClickListener(this);
 58         view.findViewById(R.id.btn29).setOnClickListener(this);
 59         view.findViewById(R.id.btn30).setOnClickListener(this);
 60         view.findViewById(R.id.btn31).setOnClickListener(this);
 61         view.findViewById(R.id.btn32).setOnClickListener(this);
 62         view.findViewById(R.id.btn33).setOnClickListener(this);
 63         view.findViewById(R.id.btn34).setOnClickListener(this);
 64         view.findViewById(R.id.btn35).setOnClickListener(this);
 65         view.findViewById(R.id.btn36).setOnClickListener(this);
 66         view.findViewById(R.id.btn37).setOnClickListener(this);
 67         view.findViewById(R.id.btn38).setOnClickListener(this);
 68         view.findViewById(R.id.btn39).setOnClickListener(this);
 69         view.findViewById(R.id.btn40).setOnClickListener(this);
 70         view.findViewById(R.id.btn41).setOnClickListener(this);
 71         Log.d("dummy_arabic_input_onCreateInputView", "invoked");
 72         return view;
 73     }
 74 
 75     @Override
 76     public View onCreateCandidatesView() 
 77     //Create and return the view hierarchy used to show candidates.
 78     /* view hierarchy是用来说明在window中的view之间的关系的。 
 79     可以把view hierarchy认为是一棵翻转的tree structure,
 80     而window就是这棵树的最上面的节点(根节点)。
 81     树的下面就是父子view之间的关系。
 82     从视觉上来看,view hierarchy就是一个封闭的结构,
 83     就是一个view包含一个或多个view,而window包含所有的view。*/
 84     {
 85         //下面的View.Gone是View类的静态成员,
 86         //GONE: This view is invisible, 
 87         //and it doesn't take any space for layout purposes.
 88         //我们的智能输入法界面最上面一般会有一栏候选项(CandidatesView),
 89         //但我们这里创造的输入法不是智能输入法,不需要显示候选项,
 90         //所以这里将CandidatesView设为GONE,即不可见
 91         View view = getLayoutInflater().inflate(R.layout.arabic_keyboard, null);
 92         view.findViewById(R.id.btn1).setVisibility(View.GONE);
 93         view.findViewById(R.id.btn2).setVisibility(View.GONE);
 94         view.findViewById(R.id.btn3).setVisibility(View.GONE);
 95         view.findViewById(R.id.btn4).setVisibility(View.GONE);
 96         view.findViewById(R.id.btn5).setVisibility(View.GONE);
 97         view.findViewById(R.id.btn6).setVisibility(View.GONE);
 98         view.findViewById(R.id.btn7).setVisibility(View.GONE);
 99         view.findViewById(R.id.btn8).setVisibility(View.GONE);
100         view.findViewById(R.id.btn9).setVisibility(View.GONE);
101         view.findViewById(R.id.btn10).setVisibility(View.GONE);
102         view.findViewById(R.id.btn11).setVisibility(View.GONE);
103         view.findViewById(R.id.btn12).setVisibility(View.GONE);
104         view.findViewById(R.id.btn13).setVisibility(View.GONE);
105         view.findViewById(R.id.btn14).setVisibility(View.GONE);
106         view.findViewById(R.id.btn15).setVisibility(View.GONE);
107         view.findViewById(R.id.btn16).setVisibility(View.GONE);
108         view.findViewById(R.id.btn17).setVisibility(View.GONE);
109         view.findViewById(R.id.btn18).setVisibility(View.GONE);
110         view.findViewById(R.id.btn19).setVisibility(View.GONE);
111         view.findViewById(R.id.btn20).setVisibility(View.GONE);
112         view.findViewById(R.id.btn21).setVisibility(View.GONE);
113         view.findViewById(R.id.btn22).setVisibility(View.GONE);
114         view.findViewById(R.id.btn23).setVisibility(View.GONE);
115         view.findViewById(R.id.btn24).setVisibility(View.GONE);
116         view.findViewById(R.id.btn25).setVisibility(View.GONE);
117         view.findViewById(R.id.btn26).setVisibility(View.GONE);
118         view.findViewById(R.id.btn27).setVisibility(View.GONE);
119         view.findViewById(R.id.btn28).setVisibility(View.GONE);
120         view.findViewById(R.id.btn29).setVisibility(View.GONE);
121         view.findViewById(R.id.btn30).setVisibility(View.GONE);
122         view.findViewById(R.id.btn31).setVisibility(View.GONE);
123         view.findViewById(R.id.btn32).setVisibility(View.GONE);
124         view.findViewById(R.id.btn33).setVisibility(View.GONE);
125         view.findViewById(R.id.btn34).setVisibility(View.GONE);
126         view.findViewById(R.id.btn35).setVisibility(View.GONE);
127         view.findViewById(R.id.btn36).setVisibility(View.GONE);
128         view.findViewById(R.id.btn37).setVisibility(View.GONE);
129         view.findViewById(R.id.btn38).setVisibility(View.GONE);
130         view.findViewById(R.id.btn39).setVisibility(View.GONE);
131         view.findViewById(R.id.btn40).setVisibility(View.GONE);
132         view.findViewById(R.id.btn41).setVisibility(View.GONE);
133         
134         Log.d("dummy_arabic_input_onCreateCandidatesView", "invoked");
135         return view;
136     }
137 
138     @Override
139     public void onStartInputView(EditorInfo info, boolean restarting)
140     {
141         Log.d("dummy_arabic_input_onStartInputView", "invoked");
142         super.onStartInputView(info, restarting);
143     }
144 
145     @Override
146     public void onFinishInput()
147     {
148         Log.d("dummy_arabic_input_onFinishInput", "invoked");
149         super.onFinishInput();
150     }
151 
152     @Override
153     public void onDestroy()
154     {
155         Log.d("dummy_arabic_input_onDestroy", "invoked");
156         super.onDestroy();
157     }
158 
159     @Override
160     public void onClick(View view)
161     {
162         if (view.getId() == R.id.btn37)
163         {
164             getCurrentInputConnection().deleteSurroundingText(1, 0);
165             //InputConnection接口是用来给Activity传数据的渠道(channel)
166         }
167         else
168         {
169             Button button = (Button) view;
170             InputConnection inputConnection = getCurrentInputConnection();
171             
172             if (button.getId() != R.id.btn37)
173             {                        
174                 inputConnection.commitText(button.getText(), 1);
175             }
176         }
177     }
178 }
View Code

 

src/com.example.dummy_arabic_input/InputSetting.java

1 InputSetting.java
View Code

 

res/layout/input_setting.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 3     android:layout_width="fill_parent"
 4     android:layout_height="fill_parent"
 5     android:orientation="horizontal" >
 6 
 7     <TextView
 8         android:id="@+id/textview"
 9         android:layout_width="wrap_content"
10         android:layout_height="wrap_content"
11         android:text="输入法设置窗口" />
12 
13 </LinearLayout>
View Code

 

res/layout/arabic_keyboard.xml

  1 <?xml version="1.0" encoding="utf-8"?>
  2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3     android:layout_width="fill_parent"
  4     android:layout_height="wrap_content"
  5     android:background="#F2F2F2"
  6     android:gravity="bottom"
  7     android:orientation="vertical" >
  8 
  9     <LinearLayout
 10         android:layout_width="fill_parent"
 11         android:layout_height="40.0dip"
 12         android:orientation="horizontal" >
 13 
 14         <Button
 15             android:id="@+id/btn1"
 16             android:layout_width="fill_parent"
 17             android:layout_height="fill_parent"
 18             android:layout_weight="1.0"
 19             android:background="@drawable/blank"
 20             android:text="ب" />
 21 
 22         <Button
 23             android:id="@+id/btn2"
 24             android:layout_width="fill_parent"
 25             android:layout_height="fill_parent"
 26             android:layout_weight="1.0"
 27             android:background="@drawable/blank"
 28             android:text="إ" />
 29 
 30         <Button
 31             android:id="@+id/btn3"
 32             android:layout_width="fill_parent"
 33             android:layout_height="fill_parent"
 34             android:layout_weight="1.0"
 35             android:background="@drawable/blank"
 36             android:text="آ" />
 37 
 38         <Button
 39             android:id="@+id/btn4"
 40             android:layout_width="fill_parent"
 41             android:layout_height="fill_parent"
 42             android:layout_weight="1.0"
 43             android:background="@drawable/blank"
 44             android:text="أ" />
 45 
 46         <Button
 47             android:id="@+id/btn5"
 48             android:layout_width="fill_parent"
 49             android:layout_height="fill_parent"
 50             android:layout_weight="1.0"
 51             android:background="@drawable/blank"
 52             android:text="ا" />
 53 
 54         <Button
 55             android:id="@+id/btn6"
 56             android:layout_width="fill_parent"
 57             android:layout_height="fill_parent"
 58             android:layout_weight="1.0"
 59             android:background="@drawable/blank"
 60             android:text="ء" />
 61     </LinearLayout>
 62 
 63     <LinearLayout
 64         android:layout_width="fill_parent"
 65         android:layout_height="40.0dip"
 66         android:orientation="horizontal" >
 67 
 68         <Button
 69             android:id="@+id/btn7"
 70             android:layout_width="fill_parent"
 71             android:layout_height="fill_parent"
 72             android:layout_weight="1.0"
 73             android:background="@drawable/blank"
 74             android:text="خ" />
 75 
 76         <Button
 77             android:id="@+id/btn8"
 78             android:layout_width="fill_parent"
 79             android:layout_height="fill_parent"
 80             android:layout_weight="1.0"
 81             android:background="@drawable/blank"
 82             android:text="ح" />
 83 
 84         <Button
 85             android:id="@+id/btn9"
 86             android:layout_width="fill_parent"
 87             android:layout_height="fill_parent"
 88             android:layout_weight="1.0"
 89             android:background="@drawable/blank"
 90             android:text="ج" />
 91 
 92         <Button
 93             android:id="@+id/btn10"
 94             android:layout_width="fill_parent"
 95             android:layout_height="fill_parent"
 96             android:layout_weight="1.0"
 97             android:background="@drawable/blank"
 98             android:text="ث" />
 99 
100         <Button
101             android:id="@+id/btn11"
102             android:layout_width="fill_parent"
103             android:layout_height="fill_parent"
104             android:layout_weight="1.0"
105             android:background="@drawable/blank"
106             android:text="ة" />
107 
108         <Button
109             android:id="@+id/btn12"
110             android:layout_width="fill_parent"
111             android:layout_height="fill_parent"
112             android:layout_weight="1.0"
113             android:background="@drawable/blank"
114             android:text="ت" />
115     </LinearLayout>
116 
117     <LinearLayout
118         android:layout_width="fill_parent"
119         android:layout_height="40.0dip"
120         android:orientation="horizontal" >
121 
122         <Button
123             android:id="@+id/btn13"
124             android:layout_width="fill_parent"
125             android:layout_height="fill_parent"
126             android:layout_weight="1.0"
127             android:background="@drawable/blank"
128             android:text="ش" />
129 
130         <Button
131             android:id="@+id/btn14"
132             android:layout_width="fill_parent"
133             android:layout_height="fill_parent"
134             android:layout_weight="1.0"
135             android:background="@drawable/blank"
136             android:text="س" />
137 
138         <Button
139             android:id="@+id/btn15"
140             android:layout_width="fill_parent"
141             android:layout_height="fill_parent"
142             android:layout_weight="1.0"
143             android:background="@drawable/blank"
144             android:text="ز" />
145 
146         <Button
147             android:id="@+id/btn16"
148             android:layout_width="fill_parent"
149             android:layout_height="fill_parent"
150             android:layout_weight="1.0"
151             android:background="@drawable/blank"
152             android:text="ر" />
153 
154         <Button
155             android:id="@+id/btn17"
156             android:layout_width="fill_parent"
157             android:layout_height="fill_parent"
158             android:layout_weight="1.0"
159             android:background="@drawable/blank"
160             android:text="ذ" />
161 
162         <Button
163             android:id="@+id/btn18"
164             android:layout_width="fill_parent"
165             android:layout_height="fill_parent"
166             android:layout_weight="1.0"
167             android:background="@drawable/blank"
168             android:text="د" />
169     </LinearLayout>
170 
171     <LinearLayout
172         android:layout_width="fill_parent"
173         android:layout_height="40.0dip"
174         android:orientation="horizontal" >
175 
176         <Button
177             android:id="@+id/btn19"
178             android:layout_width="fill_parent"
179             android:layout_height="fill_parent"
180             android:layout_weight="1.0"
181             android:background="@drawable/blank"
182             android:text="غ" />
183 
184         <Button
185             android:id="@+id/btn20"
186             android:layout_width="fill_parent"
187             android:layout_height="fill_parent"
188             android:layout_weight="1.0"
189             android:background="@drawable/blank"
190             android:text="ع" />
191 
192         <Button
193             android:id="@+id/btn21"
194             android:layout_width="fill_parent"
195             android:layout_height="fill_parent"
196             android:layout_weight="1.0"
197             android:background="@drawable/blank"
198             android:text="ظ" />
199 
200         <Button
201             android:id="@+id/btn22"
202             android:layout_width="fill_parent"
203             android:layout_height="fill_parent"
204             android:layout_weight="1.0"
205             android:background="@drawable/blank"
206             android:text="ط" />
207 
208         <Button
209             android:id="@+id/btn23"
210             android:layout_width="fill_parent"
211             android:layout_height="fill_parent"
212             android:layout_weight="1.0"
213             android:background="@drawable/blank"
214             android:text="ض" />
215 
216         <Button
217             android:id="@+id/btn24"
218             android:layout_width="fill_parent"
219             android:layout_height="fill_parent"
220             android:layout_weight="1.0"
221             android:background="@drawable/blank"
222             android:text="ص" />
223     </LinearLayout>
224 
225     <LinearLayout
226         android:layout_width="fill_parent"
227         android:layout_height="40.0dip"
228         android:orientation="horizontal" >
229 
230         <Button
231             android:id="@+id/btn25"
232             android:layout_width="fill_parent"
233             android:layout_height="fill_parent"
234             android:layout_weight="1.0"
235             android:background="@drawable/blank"
236             android:text="ن" />
237 
238         <Button
239             android:id="@+id/btn26"
240             android:layout_width="fill_parent"
241             android:layout_height="fill_parent"
242             android:layout_weight="1.0"
243             android:background="@drawable/blank"
244             android:text="م" />
245 
246         <Button
247             android:id="@+id/btn27"
248             android:layout_width="fill_parent"
249             android:layout_height="fill_parent"
250             android:layout_weight="1.0"
251             android:background="@drawable/blank"
252             android:text="ل" />
253 
254         <Button
255             android:id="@+id/btn28"
256             android:layout_width="fill_parent"
257             android:layout_height="fill_parent"
258             android:layout_weight="1.0"
259             android:background="@drawable/blank"
260             android:text="ك" />
261 
262         <Button
263             android:id="@+id/btn29"
264             android:layout_width="fill_parent"
265             android:layout_height="fill_parent"
266             android:layout_weight="1.0"
267             android:background="@drawable/blank"
268             android:text="ق" />
269 
270         <Button
271             android:id="@+id/btn30"
272             android:layout_width="fill_parent"
273             android:layout_height="fill_parent"
274             android:layout_weight="1.0"
275             android:background="@drawable/blank"
276             android:text="ف" />
277     </LinearLayout>
278 
279     <LinearLayout
280         android:layout_width="fill_parent"
281         android:layout_height="40.0dip"
282         android:orientation="horizontal" >
283 
284         <Button
285             android:id="@+id/btn31"
286             android:layout_width="fill_parent"
287             android:layout_height="fill_parent"
288             android:layout_weight="1.0"
289             android:background="@drawable/blank"
290             android:text="ئ" />
291 
292         <Button
293             android:id="@+id/btn32"
294             android:layout_width="fill_parent"
295             android:layout_height="fill_parent"
296             android:layout_weight="1.0"
297             android:background="@drawable/blank"
298             android:text="ى" />
299 
300         <Button
301             android:id="@+id/btn33"
302             android:layout_width="fill_parent"
303             android:layout_height="fill_parent"
304             android:layout_weight="1.0"
305             android:background="@drawable/blank"
306             android:text="ي" />
307 
308         <Button
309             android:id="@+id/btn34"
310             android:layout_width="fill_parent"
311             android:layout_height="fill_parent"
312             android:layout_weight="1.0"
313             android:background="@drawable/blank"
314             android:text="ؤ" />
315 
316         <Button
317             android:id="@+id/btn35"
318             android:layout_width="fill_parent"
319             android:layout_height="fill_parent"
320             android:layout_weight="1.0"
321             android:background="@drawable/blank"
322             android:text="و" />
323 
324         <Button
325             android:id="@+id/btn36"
326             android:layout_width="fill_parent"
327             android:layout_height="fill_parent"
328             android:layout_weight="1.0"
329             android:background="@drawable/blank"
330             android:text="ه" />
331     </LinearLayout>
332 
333     <LinearLayout
334         android:layout_width="fill_parent"
335         android:layout_height="40.0dip"
336         android:orientation="horizontal" >
337 
338         <Button
339             android:id="@+id/btn37"
340             android:layout_width="0dp"
341             android:layout_height="fill_parent"
342             android:layout_weight="1.0"
343             android:background="@drawable/delete" />
344             
345         <Button
346             android:id="@+id/btn38"
347             android:layout_width="0dp"      
348             android:layout_height="fill_parent"
349             android:layout_weight="1.0"
350             android:background="@drawable/enter"
351             android:text="\n" />
352 
353         <Button
354             android:id="@+id/btn39"
355             android:layout_width="0dp"       
356             android:layout_height="fill_parent"
357             android:layout_weight="2.0"
358             android:background="@drawable/blank"
359             android:text=" " />
360 
361         <Button
362             android:id="@+id/btn40"
363             android:layout_width="0dp"            
364             android:layout_height="fill_parent"
365             android:layout_weight="1.0"
366             android:background="@drawable/blank"
367             android:text="." />
368 
369         <Button
370             android:id="@+id/btn41"
371             android:layout_width="0dp"            
372             android:layout_height="fill_parent"
373             android:layout_weight="1.0"
374             android:background="@drawable/blank"
375             android:text="،" />
376 
377     </LinearLayout>
378 
379 </LinearLayout>
View Code

 

六、效果显示

 

推荐阅读