android


how Edittext onclick listener works?


I want to make an activity Which will check the phone number validity.here I have a textview which shows a string "+91" and an edittext where we can input a phone number.
I have made this:--
public class MainActivity extends Activity {
EditText ed;
TextView textv;
Button btn;
String number = "+91";
Context context;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
ed = (EditText) findViewById(R.id.ed1);
btn = (Button) findViewById(R.id.btn1);
textv = (TextView) findViewById(R.id.txt1);
textv.setText(number);
addlistener();
}
public void addlistener(){
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
try {
String ap;
String edstr = ed.getText().toString();
String txstr= textv.getText().toString();
ap=txstr+edstr;
PhoneNumberUtil phoneUtil = PhoneNumberUtil.getInstance();
PhoneNumber numberProto = phoneUtil.parse(ap, "");
String internationalFormat = phoneUtil.format(numberProto,PhoneNumberFormat.INTERNATIONAL);
Toast.makeText(getApplicationContext(),(phoneUtil.isValidNumber(numberProto) == true ? "Yes" : "No")
+ internationalFormat,
Toast.LENGTH_SHORT).show();
} catch (NumberParseException e) {
Toast.makeText(context,"error",Toast.LENGTH_SHORT).show();
}
}
});
}
}
This code works fine..and showing the result..My requirement is when I enter the phone number in edittext it will automatically show the validation.means in my code I use a button to check the validation.I want this validation as edittext.What I have to do now????
You can use onKeyListener for that edittext, check if pressed key is Enter, validate number.
The code snippet:
ed.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(keyCode == KeyEvent.KEYCODE_ENTER) {
validateNumber(ed.getText().toString())
}
return false;
}
});
You can also use addTextChangedListener.
Hope this helps.
youredittext.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
//put your validation code here
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});

Related Links

LinearLayout not aligning properly
ArrayAdapter getView method trigger only first element
Having a instance of SQLiteOpenHelper in Singleton Class
ClassNotFoundException in custom flavor using kotlin
How do I download XML from Internet Simple Framework?
Supporting all video formats in android
jquery drag drop hangs or gets stuck for android browser
2 ViewPager not scrolling in Android 2.3
“Rename Application Package” stops android cordova app from accessing Internet
how to use interface for communicatiion among classes and activities
Android Emulator Disconnected
Delete row in listview using sql database in android
Dialog with which contain fragment
how to use maps inside the fragment
Adding rows in onCreate() function
new Date() returns local time instead of UTC

Categories

HOME
google-app-engine
facebook-oauth
fftw
tivoli
mips
code-coverage
azure-container-service
flume
barcode
extjs4.2
p2p
endeca
cmdb
speech-synthesis
plink
mongodb-csharp
symbol
dreamweaver
google-picker
asp.net-core-webapi
visual-glitch
rubymotion
pimcore
scriptella
google-api-client
expressionengine3
jquery-multiselect
has-and-belongs-to-many
wcag
errbot
user-defined-functions
autodesk-designautomation
utorrent
carriage-return
numpy-broadcasting
nohup
sim-card
bc
azure-arm
magento-1.9.3
watchface
datacachefactory
branching-and-merging
bindingnavigator
wc
ovf
cnc
manova
google-cse
reindex
pair-programming
amq
stdmap
subnet
check-mk
type-theory
rackspace-cloud
vibrate
shinobi
custom-lists
builtins
nikeplus-api
jboss-tools
firebase-security
observablecollection
nss
yajsw
beanstalk
kendo-upload
user-management
custom-errors
avisynth
wiredep
roadkill-wiki
flock
zen-cart
openbabel
grouping-sets
mruby
sql-scripts
cocos3d
lambda-architecture
radiant
maven-scm
getopt
solace-mq
kendo-dataviz
rolify
octal
actiondispatch
cascadingdropdown
twisted.web
device-width
pdfviewer
panda3d
web-farm
node-redis
filemerge
timthumb
application-verifier
scrollbars
movieplayer
leader

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App