fragment主布局文件
1 27 8 14 15 100 10120 21 25 26 30 31 35 36 3741 42 46 47 51 52 56 57 5862 63 67 68 72 73 77 78 7983 84 88 89 93 94 98 99
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
18
使用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 }