
2.切换到随机数字键盘
3.自定义确定和删除等键(向外抛出接口)
使用方法:
1.在项目build.gradle文件中添加jitpack,添加jitpcak就够了。allprojects{undefinedrepositories{undefinedjcenter()maven{url'https://jitpack.io'}}}2.在module的build.gradle文件添加依赖compile'com.github.Simon986793021:NumberKeyboard:v1.0'3.在布局文件中添加布局android:id="@+id/keyboard_view"xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="wrap_content"android:focusable="true"android:paddingTop="0dp"android:focusableInTouchMode="true"android:keyBackground="@drawable/bg_keyboardview"android:keyPreviewOffset="0dp"android:keyTextColor="#000"android:shadowColor="#fff"android:shadowRadius="0.0"android:layout_alignParentBottom="true"/>4.在MainActivity调用。editText=(EditText)findViewById(R.id.et_numberplate)changebutton=(Button)findViewById(R.id.bt_change_keyboard)finalOfoKeyboardkeyboard=newOfoKeyboard(MainActivity.this)//获取到keyboard对象changebutton.setOnClickListener(newView.OnClickListener(){undefined@OverridepublicvoidonClick(Viewv){undefinedkeyboard.attachTo(editText,true)//eiditext绑定keyboard,true表示随机数字}})editText.setOnClickListener(newView.OnClickListener(){undefined@OverridepublicvoidonClick(Viewv){undefinedkeyboard.attachTo(editText,false)//eiditext绑定keyboard,false表示普通数字键盘}})/*确定按钮*/keyboard.setOnOkClick(newOfoKeyboard.OnOkClick(){undefined@OverridepublicvoidonOkClick(){undefinedLog.i(">>>>>>","点击了确定")Toast.makeText(MainActivity.this,editText.getText().toString(),Toast.LENGTH_SHORT).show()}})//隐藏键盘按钮keyboard.setOnCancelClick(newOfoKeyboard.OnCancelClcik(){undefined@OverridepublicvoidonCancelClick(){undefinedToast.makeText(MainActivity.this,"隐藏键盘",Toast.LENGTH_SHORT).show()}})只需要这些简单的代码就能够实现一个自己定义的键盘了。实现过程1.新建一个keyboard布局在看这个代码之前需要了解keyboard的属性:不清楚属性,怎么画页面,不懂的请移步这篇博客在res新建一个xml文件,然后在xml新建一个keyboard.xml里面代码如下xmlns:android="http://schemas.android.com/apk/res/android"android:keyHeight="9%p"android:keyWidth="25%p"android:horizontalGap="0dp">android:codes="49"android:keyLabel="1"/>android:codes="50"android:keyLabel="2"/>android:codes="51"android:keyLabel="3"/>android:codes="-5"android:keyHeight="18%p"android:keyEdgeFlags="right"android:isRepeatable="true"android:keyIcon="@drawable/icon_delete_32dp"/>android:codes="52"android:keyLabel="4"/>android:codes="53"android:keyLabel="5"/>android:codes="54"android:keyLabel="6"/>android:codes="55"android:keyLabel="7"/>android:codes="56"android:keyLabel="8"/>android:codes="57"android:keyLabel="9"/>android:codes="-4"android:keyLabel="确定"android:keyEdgeFlags="right"android:keyHeight="18%p"/>android:codes="46"android:keyLabel="."/>android:codes="48"android:keyLabel="0"/>android:codes="-3"android:keyIcon="@drawable/icon_hide_keyboard"/>这个布局就是自己自定义键盘的布局实现,有了布局,显然是不够的。2.自定义KeyboardViewpackagecom.wind.keyboardimportandroid.content.Contextimportandroid.graphics.Canvasimportandroid.graphics.Colorimportandroid.graphics.Paintimportandroid.graphics.Rectimportandroid.graphics.Typefaceimportandroid.graphics.drawable.Drawableimportandroid.inputmethodservice.Keyboardimportandroid.inputmethodservice.KeyboardViewimportandroid.util.AttributeSetimportandroid.util.Logimportjava.lang.reflect.Fieldimportjava.util.List/***Createdbyzhangcongon2017/8/24.*/publicclassOfoKeyboardViewextendsKeyboardView{undefinedprivateContextcontextprivateKeyboardkeyboardpublicOfoKeyboardView(Contextcontext,AttributeSetattrs){undefinedsuper(context,attrs)this.context=contextLog.i(">>>>>","构造函数被调用了")}/***重新画一些按键*/@OverridepublicvoidonDraw(Canvascanvas){undefinedsuper.onDraw(canvas)keyboard=this.getKeyboard()Listkeys=nullif(keyboard!=null){undefinedkeys=keyboard.getKeys()}if(keys!=null){undefinedfor(Keyboard.Keykey:keys){undefined//数字键盘的处理if(key.codes[0]==-4){undefineddrawKeyBackground(R.drawable.bg_keyboardview_yes,canvas,key)drawText(canvas,key)}}}}privatevoiddrawKeyBackground(intdrawableId,Canvascanvas,Keyboard.Keykey){undefinedDrawablenpd=context.getResources().getDrawable(drawableId)int[]drawableState=key.getCurrentDrawableState()if(key.codes[0]!=0){undefinednpd.setState(drawableState)}npd.setBounds(key.x,key.y,key.x+key.width,key.y+key.height)npd.draw(canvas)}privatevoiddrawText(Canvascanvas,Keyboard.Keykey){undefinedRectbounds=newRect()Paintpaint=newPaint()paint.setTextAlign(Paint.Align.CENTER)paint.setAntiAlias(true)paint.setColor(Color.WHITE)if(key.label!=null){undefinedStringlabel=key.label.toString()Fieldfieldif(label.length()>1&&key.codes.length>>>>","attachTo")this.editText=editTexthideSystemSofeKeyboard(activity,editText)showSoftKeyboard()}privatevoidshowSoftKeyboard(){undefinedif(keyboard==null){undefinedkeyboard=newKeyboard(activity,R.xml.keyboard)}if(keyboardView==null){undefinedkeyboardView=(OfoKeyboardView)activity.findViewById(R.id.keyboard_view)}if(isRandom){undefinedrandomKeyboardNumber()}else{undefinedkeyboardView.setKeyboard(keyboard)}keyboardView.setEnabled(true)keyboardView.setPreviewEnabled(false)keyboardView.setVisibility(View.VISIBLE)keyboardView.setOnKeyboardActionListener(listener)}privateKeyboardView.OnKeyboardActionListenerlistener=newKeyboardView.OnKeyboardActionListener(){undefined@OverridepublicvoidonPress(intprimaryCode){undefined}@OverridepublicvoidonRelease(intprimaryCode){undefined}@OverridepublicvoidonKey(intprimaryCode,int[]keyCodes){undefinedEditableeditable=editText.getText()intstart=editText.getSelectionStart()if(primaryCode==Keyboard.KEYCODE_DELETE)//keycodes为-5{undefinedif(editable!=null&&editable.length()>0){undefinedif(start>0){undefinededitable.delete(start-1,start)}}}elseif(primaryCode==Keyboard.KEYCODE_CANCEL){undefinedhideKeyBoard()if(mCancelClick!=null){undefinedmCancelClick.onCancelClick()}}elseif(primaryCode==Keyboard.KEYCODE_DONE){undefinedhideKeyBoard()if(mOkClick!=null){undefinedmOkClick.onOkClick()}}else{undefinedLog.i(">>>>>>",primaryCode+"1")Log.i(">>>>>>",(char)primaryCode+"2")editable.insert(start,Character.toString((char)primaryCode))}}@OverridepublicvoidonText(CharSequencetext){undefined}@OverridepublicvoidswipeLeft(){undefined}@OverridepublicvoidswipeRight(){undefined}@OverridepublicvoidswipeDown(){undefined}@OverridepublicvoidswipeUp(){undefined}}publicinterfaceOnOkClick{undefinedvoidonOkClick()}publicinterfaceOnCancelClcik{undefinedvoidonCancelClick()}publicOnOkClickmOkClickpublicOnCancelClcikmCancelClickpublicvoidsetOnOkClick(OnOkClickonOkClick){undefinedthis.mOkClick=onOkClick}publicvoidsetOnCancelClick(OnCancelClcikonCancelClick){undefinedthis.mCancelClick=onCancelClick}privatevoidhideKeyBoard(){undefinedintvisibility=keyboardView.getVisibility()if(visibility==KeyboardView.VISIBLE){undefinedkeyboardView.setVisibility(KeyboardView.GONE)}}privatebooleanisNumber(Stringstr){undefinedStringwordstr="0123456789"returnwordstr.contains(str)}privatevoidrandomKeyboardNumber(){undefinedListkeyList=keyboard.getKeys()//查找出0-9的数字键ListnewkeyList=newArrayList()for(inti=0i 回答于 2022-04-01
android选项菜单怎么添加图片android选项菜单怎么添加图片,Android图片选择框架--PictureSelector
weixin_39756235
转载
关注
0点赞·464人阅读
图片.png
功能特点
支持通过拍照获取图片
支持通过相册获取图片
支持图片裁切
支持仿IOS底部d出选择菜单ActionSheet效果
支持6.0动态授予权限
解决图片有黑边问题
解决7.0调用相机crash问题
解决小米miui系统调用系统裁剪图片功能crash问题
使用
Step 1. 添加JitPack仓库
在项目的build.gradle添加JitPack仓库
allprojects {
repositories {
...
maven { url "https://jitpack.io" }
}
}
Step 2. 添加依赖
在需要使用的module中添加依赖
dependencies {
compile 'com.github.wildma:PictureSelector:1.0.0'
}
或者引用本地lib
compile project(':pictureselector')
Step 3. 配置清单文件所需activity
android:name="com.wildma.pictureselector.PictureSelectActivity"
android:theme="@android:style/Theme.Translucent.NoTitleBar"/>
Step 4. 拍照或者从相册选择图片
/**
* create方法参数一是上下文,在activity中传activity.this,在fragment中传fragment.this。参数二为请求码,用于结果回调onActivityResult中判断
* selectPicture方法参数分别为图片的裁剪宽、裁剪高、宽比例、高比例。默认不传则为宽200,高200,宽高比例为1:1。
*/
PictureSelector
.create(MainActivity.this, PictureSelector.SELECT_REQUEST_CODE)
.selectPicture(200, 200, 1, 1)
Step 5. 获取裁剪后的图片地址
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data)
/*结果回调*/
if (requestCode == PictureSelector.SELECT_REQUEST_CODE) {
if (data != null) {
String picturePath = data.getStringExtra(PictureSelector.PICTURE_PATH)
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)