Android read from keyboard - Stack Overflow

文章推薦指數: 80 %
投票人數:10人

I can't have any button or textfields, so I have to read keyboard input without listening to a EditText field. So, an empty screen with an image(lock), that ... Home Public Questions Tags Users Collectives ExploreCollectives FindaJob Jobs Companies Teams StackOverflowforTeams –Collaborateandshareknowledgewithaprivategroup. CreateafreeTeam WhatisTeams? Teams CreatefreeTeam CollectivesonStackOverflow Findcentralized,trustedcontentandcollaboratearoundthetechnologiesyouusemost. Learnmore Teams Q&Aforwork Connectandshareknowledgewithinasinglelocationthatisstructuredandeasytosearch. Learnmore Androidreadfromkeyboard AskQuestion Asked 9years,3monthsago Active 9years,3monthsago Viewed 7ktimes 2 1 Ihavethislogonscreenwithnotextfieldsatall.Whentheuserscanshisidtokenwithrfidscanner,i'llgeta8characterlongstring.Itsthesameprincipasusingankeyboard(justfaster).Iwantmyloginactivitytoact,whentheuserhasscannedhistokenandnotbefore.Isthereasmartwayimplementingthis?Ican'thaveanybuttonortextfields,soIhavetoreadkeyboardinputwithoutlisteningtoaEditTextfield. So,anemptyscreenwithanimage(lock),thatlistenstoanrfidreader(orkeyboard)andactwhenthecharactercountis8. androidkeyboardlistenerrfid Share Improvethisquestion Follow askedAug20'12at13:18 user1581164user1581164 7122silverbadges55bronzebadges 2 implementonKeyDownlikeindeveloper.android.com/reference/android/app/…. – zapl Aug20'12at13:25 Thanks.Isitpossiblelisteningtothekeyboardwithouthavinganlisteneronatextfield.Causemuactualproblemis,thatineedtheusertotypeinan8charlongkey.Thereshouldnotbeanytextfieldsinthelayout,nothing...imagineablinduser.Isimplewanttolistenatanyinputfromtheuserandmanuallysavetheinputinmycode.Thelayoutwillonlycontainapictureofalock.I'vetriedEditTextwithyourexample,buticantmakethefieldunvisiblewithoutloosingitsfunction. – user1581164 Aug22'12at7:21 Addacomment  |  1Answer 1 Active Oldest Votes 8 IfyouimplementonKeyDown()inyourActivityandthereisnootherUIwidgetthathandleskeyeventsyoushouldgeteverykeypressfromyourkeyboard.BelowexampleshouldworkforA-Zatleastandisintendedtosimply"print"thekeypressestoaTextView. Youmightneedtoaddamoresophisticatedwaytomapkeycodetocharacterifthatisnotenough.(e.g.spacedoesnotwork,numbersprobablyneither) publicclassKeyActivityextendsActivity{ //shouldworkfora-z privatestaticfinalPatternKEYCODE_PATTERN=Pattern.compile("KEYCODE_(\\w)"); privateTextViewmTextView; @Override protectedvoidonCreate(BundlesavedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //thereisjustaTextView,nothingthathandlekeys mTextView=(TextView)findViewById(R.id.textView1); } @Override publicbooleanonKeyDown(intkeyCode,KeyEventevent){ Stringkey=KeyEvent.keyCodeToString(keyCode); //keywillbesomethinglike"KEYCODE_A"-extractthe"A" //usepatterntoconvertintkeycodetosomecharacter Matchermatcher=KEYCODE_PATTERN.matcher(key); if(matcher.matches()){ //appendcharactertotextview mTextView.append(matcher.group(1)); } //letthedefaultimplementationhandletheevent returnsuper.onKeyDown(keyCode,event); } } Share Improvethisanswer Follow answeredAug22'12at9:49 zaplzapl 61k1010goldbadges117117silverbadges144144bronzebadges Addacomment  |  YourAnswer ThanksforcontributingananswertoStackOverflow!Pleasebesuretoanswerthequestion.Providedetailsandshareyourresearch!Butavoid…Askingforhelp,clarification,orrespondingtootheranswers.Makingstatementsbasedonopinion;backthemupwithreferencesorpersonalexperience.Tolearnmore,seeourtipsonwritinggreatanswers. Draftsaved Draftdiscarded Signuporlogin SignupusingGoogle SignupusingFacebook SignupusingEmailandPassword Submit Postasaguest Name Email Required,butnevershown PostYourAnswer Discard Byclicking“PostYourAnswer”,youagreetoourtermsofservice,privacypolicyandcookiepolicy Nottheansweryou'relookingfor?Browseotherquestionstaggedandroidkeyboardlistenerrfidoraskyourownquestion. TheOverflowBlog Whoownsthisoutage?BuildingintelligentescalationchainsformodernSRE Podcast395:Whoisbuildingcloudsfortheindependentdeveloper? FeaturedonMeta Nowlive:Afullyresponsiveprofile Reducingtheweightofourfooter TwoBornottwoB-Farewell,BoltClockandBhargav! Linked 0 HowtodetectcontinuallykeyeventonAndroid? Related 2088 IsthereawaytorunPythononAndroid? 2031 HowtolazyloadimagesinListViewinAndroid 4135 Howdoyouclose/hidetheAndroidsoftkeyboardprogrammatically? 3058 HowtostopEditTextfromgainingfocusatActivitystartupinAndroid 1960 "Debugcertificateexpired"errorinEclipseAndroidplugins 2942 IsthereauniqueAndroiddeviceID? 2137 Whatis'Context'onAndroid? 3843 ProperusecasesforAndroidUserManager.isUserAGoat()? HotNetworkQuestions Dostateswithinfiniteaverageenergymakesense? HowtoannotatestaffwithbeatnumbersinLilypond? Whatisthemosthumanewaytokillcrayfishathome? Whatifyouhaveadouble-enginefailureafterV1butbeforeVR? Whatdoesitmeantograntachurchtoapriory? Whatdoes'dosteakandchips'mean? HowshouldIteachlogarithmstohighschoolstudents? Howcananoceanofseawaterbehavelikehoneywithoutchangingthechemistry? Sci-fistorywherepeoplearereincarnatedathubsandamanwantstofigureoutwhatishappening Datastructurethatsupportsinsertionandfastrandomelementlookup Are"shortbios"attheendofaletterofrecommendationthenewthing? Islookingforplaintextstringsonanencrypteddiskagoodtest? AnecdoteinWeinberger'sPsychologyofComputerProgramming:isitARPANET? DemerzelandtheLawsofRobotics Whatdothescammersdowhentheygetaccesstosomeone'sonlinebanking? Isitagoodideatomaketheactionsofmyantagonistreasonable? NewtoBikes:MychainfelloffandIputitbackon.HowcanIknowifit'sontherightcog? WhatCouldExplainASolitaryMountaininaGrassland? Does来mean"bring"? CrypticAcrostic3:BuiltSoFly ArethereanystudiedlandingsitesonMarsforacrewedlanding? Propertranslationfor"artissubtraction" Howcomemodelpredictionaccuracyhighbutmodeldoesnotgeneralisewell Isitoktofeedmycatonechickenliverdaily? morehotquestions Questionfeed SubscribetoRSS Questionfeed TosubscribetothisRSSfeed,copyandpastethisURLintoyourRSSreader. default Yourprivacy Byclicking“Acceptallcookies”,youagreeStackExchangecanstorecookiesonyourdeviceanddiscloseinformationinaccordancewithourCookiePolicy. Acceptallcookies Customizesettings  



請為這篇文章評分?