Android使用Fragment定义弹出数字键盘

fragment主布局文件

  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:background="#F0F1EE"
  6     android:orientation="horizontal" >
  7 
  8     <LinearLayout
  9         android:id="@+id/input_buttons"
 10         android:layout_width="fill_parent"
 11         android:layout_height="fill_parent"
 12         android:layout_marginTop="10dp"
 13         android:orientation="vertical" >
 14 
 15         <LinearLayout
 16             android:layout_width="fill_parent"
 17             android:layout_height="0dp"
 18             android:layout_weight="1"
 19             android:orientation="horizontal" >
 20 
 21             <Button
 22                 android:id="@+id/button_1"
 23                 style="@style/input_button_style"
 24                 android:text="1" />
 25 
 26             <Button
 27                 android:id="@+id/button_2"
 28                 style="@style/input_button_style"
 29                 android:text="2" />
 30 
 31             <Button
 32                 android:id="@+id/button_3"
 33                 style="@style/input_button_style"
 34                 android:text="3" />
 35         </LinearLayout>
 36 
 37         <LinearLayout
 38             android:layout_width="fill_parent"
 39             android:layout_height="0dp"
 40             android:layout_weight="1" >
 41 
 42             <Button
 43                 android:id="@+id/button_4"
 44                 style="@style/input_button_style"
 45                 android:text="4" />
 46 
 47             <Button
 48                 android:id="@+id/button_5"
 49                 style="@style/input_button_style"
 50                 android:text="5" />
 51 
 52             <Button
 53                 android:id="@+id/button_6"
 54                 style="@style/input_button_style"
 55                 android:text="6" />
 56         </LinearLayout>
 57 
 58         <LinearLayout
 59             android:layout_width="fill_parent"
 60             android:layout_height="0dp"
 61             android:layout_weight="1" >
 62 
 63             <Button
 64                 android:id="@+id/button_7"
 65                 style="@style/input_button_style"
 66                 android:text="7" />
 67 
 68             <Button
 69                 android:id="@+id/button_8"
 70                 style="@style/input_button_style"
 71                 android:text="8" />
 72 
 73             <Button
 74                 android:id="@+id/button_9"
 75                 style="@style/input_button_style"
 76                 android:text="9" />
 77         </LinearLayout>
 78 
 79         <LinearLayout
 80             android:layout_width="fill_parent"
 81             android:layout_height="0dp"
 82             android:layout_weight="1" >
 83 
 84             <Button
 85                 android:id="@+id/button_point"
 86                 style="@style/input_button_style"
 87                 android:text="拨打" />
 88 
 89             <Button
 90                 android:id="@+id/button_0"
 91                 style="@style/input_button_style"
 92                 android:text="0" />
 93 
 94             <Button
 95                 android:id="@+id/button_del"
 96                 style="@style/input_button_style"
 97                 android:text="删除" />
 98         </LinearLayout>
 99     </LinearLayout>
100 
101 </LinearLayout>

java文件

  1 package com.example.administrator.yunphone.UI;
  2 
  3 import android.app.Activity;
  4 import android.app.Fragment;
  5 import android.app.Instrumentation;
  6 import android.os.Bundle;
  7 import android.view.KeyEvent;
  8 import android.view.LayoutInflater;
  9 import android.view.View;
 10 import android.view.View.OnClickListener;
 11 import android.view.View.OnLongClickListener;
 12 import android.view.ViewGroup;
 13 import android.widget.Button;
 14 import android.widget.Toast;
 15 
 16 import com.example.administrator.yunphone.R;
 17 
 18 
 19 public class MyKeyBoard extends Fragment implements OnClickListener{
 20     Activity mActivity;
 21     View rootView;
 22 
 23     private Button button_1,button_2,button_3,button_4,button_5,button_6,button_7,button_8,button_9,button_0,button_del,button_point;
 24     @Override
 25     public View onCreateView(LayoutInflater inflater, ViewGroup container,
 26                              Bundle savedInstanceState) {
 27         // TODO Auto-generated method stub
 28         mActivity=getActivity();
 29         rootView=inflater.inflate(R.layout.keyboard_layout, container, false);
 30         initView();
 31         return rootView;
 32     }
 33 
 34     private void initView() {
 35         button_0=(Button)rootView.findViewById(R.id.button_0);
 36         button_1=(Button)rootView.findViewById(R.id.button_1);
 37         button_2=(Button)rootView.findViewById(R.id.button_2);
 38         button_3=(Button)rootView.findViewById(R.id.button_3);
 39         button_4=(Button)rootView.findViewById(R.id.button_4);
 40         button_5=(Button)rootView.findViewById(R.id.button_5);
 41         button_6=(Button)rootView.findViewById(R.id.button_6);
 42         button_7=(Button)rootView.findViewById(R.id.button_7);
 43         button_8=(Button)rootView.findViewById(R.id.button_8);
 44         button_9=(Button)rootView.findViewById(R.id.button_9);
 45         button_point=(Button)rootView.findViewById(R.id.button_point);
 46         button_del=(Button)rootView.findViewById(R.id.button_del);
 47         button_0.setOnClickListener(this);
 48         button_1.setOnClickListener(this);
 49         button_2.setOnClickListener(this);
 50         button_3.setOnClickListener(this);
 51         button_4.setOnClickListener(this);
 52         button_5.setOnClickListener(this);
 53         button_6.setOnClickListener(this);
 54         button_7.setOnClickListener(this);
 55         button_8.setOnClickListener(this);
 56         button_9.setOnClickListener(this);
 57         button_point.setOnClickListener(this);
 58         button_del.setOnClickListener(this);
 59         button_del.setOnLongClickListener(new OnLongClickListener() {
 60             @Override
 61             public boolean onLongClick(View v) {
 62                 // TODO Auto-generated method stub
 63                 performKeyDown(KeyEvent.KEYCODE_CLEAR);
 64                 return false;
 65             }
 66         });
 67     }
 68     @Override
 69     public void onClick(View arg0) {
 70         // TODO Auto-generated method stub
 71         switch (arg0.getId()) {
 72             case R.id.button_0:
 73                 performKeyDown(KeyEvent.KEYCODE_0);
 74                 Toast.makeText(getActivity(),"这是0",Toast.LENGTH_SHORT).show();
 75                 break;
 76             case R.id.button_1:
 77                 performKeyDown(KeyEvent.KEYCODE_1);
 78                 break;
 79             case R.id.button_2:
 80                 performKeyDown(KeyEvent.KEYCODE_2);
 81                 break;
 82             case R.id.button_3:
 83                 performKeyDown(KeyEvent.KEYCODE_3);
 84                 break;
 85             case R.id.button_4:
 86                 performKeyDown(KeyEvent.KEYCODE_4);
 87                 break;
 88             case R.id.button_5:
 89                 performKeyDown(KeyEvent.KEYCODE_5);
 90                 break;
 91             case R.id.button_6:
 92                 performKeyDown(KeyEvent.KEYCODE_6);
 93                 break;
 94             case R.id.button_7:
 95                 performKeyDown(KeyEvent.KEYCODE_7);
 96                 break;
 97             case R.id.button_8:
 98                 performKeyDown(KeyEvent.KEYCODE_8);
 99                 break;
100             case R.id.button_9:
101                 performKeyDown(KeyEvent.KEYCODE_9);
102                 break;
103             case R.id.button_point:
104                 performKeyDown(KeyEvent.KEYCODE_NUMPAD_DOT);
105                 break;
106             case R.id.button_del:
107                 performKeyDown(KeyEvent.KEYCODE_DEL);
108                 break;
109             default:
110                 break;
111         }
112 
113     }
114     //模拟键盘输入
115     public void performKeyDown(final int keyCode) {
116         new Thread() {
117             public void run() {
118                 try {
119                     Instrumentation inst = new Instrumentation();
120                     inst.sendKeyDownUpSync(keyCode);
121                 } catch (Exception e) {
122                     e.printStackTrace();
123                 }
124             }
125         }.start();
126     }
127 }

使用XML

1 <FrameLayout
2         android:id="@+id/keyboard"
3         android:layout_width="match_parent"
4         android:layout_height="wrap_content"
5         android:layout_gravity="bottom"
6         android:paddingLeft="10dp"
7         >
8         </FrameLayout>

使用JAVA

1  private void setKeyBoardFragment(){
2         FragmentManager fragmentManager=getFragmentManager();
3         FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
4         MyKeyBoard myKeyBoard=new MyKeyBoard();
5         fragmentTransaction.replace(R.id.keyboard,myKeyBoard);
6         fragmentTransaction.commit();
7     }

 

posted @ 2016-07-20 10:24  点滴之水  阅读(1347)  评论(0编辑  收藏  举报