Android使用Fragment定义弹出数字键盘
fragment主布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F0F1EE"
android:orientation="horizontal" > <LinearLayout
android:id="@+id/input_buttons"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="10dp"
android:orientation="vertical" > <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal" > <Button
android:id="@+id/button_1"
style="@style/input_button_style"
android:text="1" /> <Button
android:id="@+id/button_2"
style="@style/input_button_style"
android:text="2" /> <Button
android:id="@+id/button_3"
style="@style/input_button_style"
android:text="3" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" > <Button
android:id="@+id/button_4"
style="@style/input_button_style"
android:text="4" /> <Button
android:id="@+id/button_5"
style="@style/input_button_style"
android:text="5" /> <Button
android:id="@+id/button_6"
style="@style/input_button_style"
android:text="6" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" > <Button
android:id="@+id/button_7"
style="@style/input_button_style"
android:text="7" /> <Button
android:id="@+id/button_8"
style="@style/input_button_style"
android:text="8" /> <Button
android:id="@+id/button_9"
style="@style/input_button_style"
android:text="9" />
</LinearLayout> <LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" > <Button
android:id="@+id/button_point"
style="@style/input_button_style"
android:text="拨打" /> <Button
android:id="@+id/button_0"
style="@style/input_button_style"
android:text="0" /> <Button
android:id="@+id/button_del"
style="@style/input_button_style"
android:text="删除" />
</LinearLayout>
</LinearLayout> </LinearLayout>
java文件
package com.example.administrator.yunphone.UI; import android.app.Activity;
import android.app.Fragment;
import android.app.Instrumentation;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast; import com.example.administrator.yunphone.R; public class MyKeyBoard extends Fragment implements OnClickListener{
Activity mActivity;
View rootView; 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;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
mActivity=getActivity();
rootView=inflater.inflate(R.layout.keyboard_layout, container, false);
initView();
return rootView;
} private void initView() {
button_0=(Button)rootView.findViewById(R.id.button_0);
button_1=(Button)rootView.findViewById(R.id.button_1);
button_2=(Button)rootView.findViewById(R.id.button_2);
button_3=(Button)rootView.findViewById(R.id.button_3);
button_4=(Button)rootView.findViewById(R.id.button_4);
button_5=(Button)rootView.findViewById(R.id.button_5);
button_6=(Button)rootView.findViewById(R.id.button_6);
button_7=(Button)rootView.findViewById(R.id.button_7);
button_8=(Button)rootView.findViewById(R.id.button_8);
button_9=(Button)rootView.findViewById(R.id.button_9);
button_point=(Button)rootView.findViewById(R.id.button_point);
button_del=(Button)rootView.findViewById(R.id.button_del);
button_0.setOnClickListener(this);
button_1.setOnClickListener(this);
button_2.setOnClickListener(this);
button_3.setOnClickListener(this);
button_4.setOnClickListener(this);
button_5.setOnClickListener(this);
button_6.setOnClickListener(this);
button_7.setOnClickListener(this);
button_8.setOnClickListener(this);
button_9.setOnClickListener(this);
button_point.setOnClickListener(this);
button_del.setOnClickListener(this);
button_del.setOnLongClickListener(new OnLongClickListener() {
@Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
performKeyDown(KeyEvent.KEYCODE_CLEAR);
return false;
}
});
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch (arg0.getId()) {
case R.id.button_0:
performKeyDown(KeyEvent.KEYCODE_0);
Toast.makeText(getActivity(),"这是0",Toast.LENGTH_SHORT).show();
break;
case R.id.button_1:
performKeyDown(KeyEvent.KEYCODE_1);
break;
case R.id.button_2:
performKeyDown(KeyEvent.KEYCODE_2);
break;
case R.id.button_3:
performKeyDown(KeyEvent.KEYCODE_3);
break;
case R.id.button_4:
performKeyDown(KeyEvent.KEYCODE_4);
break;
case R.id.button_5:
performKeyDown(KeyEvent.KEYCODE_5);
break;
case R.id.button_6:
performKeyDown(KeyEvent.KEYCODE_6);
break;
case R.id.button_7:
performKeyDown(KeyEvent.KEYCODE_7);
break;
case R.id.button_8:
performKeyDown(KeyEvent.KEYCODE_8);
break;
case R.id.button_9:
performKeyDown(KeyEvent.KEYCODE_9);
break;
case R.id.button_point:
performKeyDown(KeyEvent.KEYCODE_NUMPAD_DOT);
break;
case R.id.button_del:
performKeyDown(KeyEvent.KEYCODE_DEL);
break;
default:
break;
} }
//模拟键盘输入
public void performKeyDown(final int keyCode) {
new Thread() {
public void run() {
try {
Instrumentation inst = new Instrumentation();
inst.sendKeyDownUpSync(keyCode);
} catch (Exception e) {
e.printStackTrace();
}
}
}.start();
}
}
使用XML
<FrameLayout
android:id="@+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingLeft="10dp"
>
</FrameLayout>
使用JAVA
private void setKeyBoardFragment(){
FragmentManager fragmentManager=getFragmentManager();
FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
MyKeyBoard myKeyBoard=new MyKeyBoard();
fragmentTransaction.replace(R.id.keyboard,myKeyBoard);
fragmentTransaction.commit();
}
Android使用Fragment定义弹出数字键盘的更多相关文章
- js输入密文弹出数字键盘
我们经常被产品要求,在移动端的web页面上的输入框输入密码时要弹出数字键盘,而不是全键盘,这个该怎么实现呢? 1.首先要弹出数字键盘,我们只能把input框的type从password改为tel 2. ...
- [HTML5]移动开发不同手机弹出数字键盘问题
这里还是先那么先交代一下遇到的问题.其实无论是tel还是number都不是完美的:type="tel"优点是iOS和Android的键盘表现都差不多缺点是那些字母好多余,虽然我没有 ...
- 【android】禁止Edittext弹出软键盘而且使光标正常显示
/** * 禁止Edittext弹出软件盘,光标依旧正常显示. */ public void disableShowSoftInput() { if (android.os.Build.VERSION ...
- Android EditView 获取焦点 不弹出软键盘
很简单的做法: 找到AndroidManifest.xml文件 然后在对应的activity中增加android:windowSoftInputMode="adjustPan" & ...
- android 点击edittext弹出软键盘,否则不弹
只需要加android:windowSoftInputMode="stateHidden|stateAlwaysHidden"就可以 如:<activity android: ...
- android edittext 获取焦点并弹出软键盘
editText.setFocusable(true); editText.setFocusableInTouchMode(true); editText.requestFocus(); activi ...
- Android实现dialog时候弹出软键盘dialog移位问题
Window win = getWindow(); WindowManager.LayoutParams params = win.getAttributes(); win.setSoftInputM ...
- (转载) EditText初始不弹出软键盘,只有光标显示,点击再弹出
EditText初始不弹出软键盘,只有光标显示,点击再弹出 2013-06-08 10:13 21305人阅读 评论(5) 收藏 举报 分类: android基础(91) 版权声明:本文为博主原创 ...
- android EditText设置弹出数字输入法键盘
<EditText android:id="@+id/edit_digit_input" android:layout_width="wrap_ ...
随机推荐
- 数字信号处理实验(一)——DTFT
1.MATLAB自编绘图函数 function [] = signal_write(X,w,flag) % X:数据 % w:频率向量 magX=abs(X);angX=angle(X); realX ...
- AJAX案例二:简单表单验证
案例:如果用户名输入为张三,那么在失去焦点时后面会显示该用户名已被注册,否则显示可以注册! <%@ page language="java" import="jav ...
- AngularJS学习之HTML DOM
1.AngularJS为HTML DOM元素的属性提供了绑定应用数据的指令: 2.ng-disabled指令:直接绑定应用程序数据到HTML的disable属性: <div ng-app=&qu ...
- 【虚拟机】苹果虚拟机mac10.11.6+Xcode8.1
[虚拟机]苹果虚拟机mac10.11.6+Xcode8.1本虚拟机加装Xcode8.1,方便大家更好学习Swift3.0语言以及iOS开发.安装注意事项:第一步:确认硬件:1.确认主板以及cpu支持虚 ...
- 理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序
理解是最好的记忆方法 之 CSS中a链接的④个伪类为何有顺序 在CSS中,a标签有4种伪类,分别为: a:link, a:visited, a:hover, a:active 对其稍有了解的前端er都 ...
- [BZOJ2599][Race][IOI2011]点分治
这是为了真正去学一下点分治..然后看了迪克李的ppt 又是一道写(改)了很久的题..终于ac了 1354799 orzliyicheng 2599 Accepted 31936 kb 23584 ms ...
- 使用Spring发送邮件
http://www.oschina.net/code/snippet_253813_36503
- Codeforces 653D Delivery Bears(最大流)
题目大概说有一张n个点m条边的简单有向图,每条边只能允许一定总量的货物通过.要让x只熊从1点到n点运送货物,每只熊都要运送且运送的货物重量都一样,求该重量的最大值. 二分重量判断是否成立. 如果已知重 ...
- HIT2739 The Chinese Postman Problem(最小费用最大流)
题目大概说给一张有向图,要从0点出发返回0点且每条边至少都要走过一次,求走的最短路程. 经典的CPP问题,解法就是加边构造出欧拉回路,一个有向图存在欧拉回路的充分必要条件是基图连通且所有点入度等于出度 ...
- .net平台下C#socket通信(上)
在开始介绍socket前先补充补充基础知识,在此基础上理解网络通信才会顺理成章,当然有基础的可以跳过去了.都是废话,进入正题. TCP/IP:Transmission Control Protocol ...