I'm creating a search option in an android application.
After pressing "Search" on the screen-keyboard an event triggers which sends the query to the server.
What I want to do is disable the "Search" button on the screen-keyboard when no text has been typed in it's corresponding EditText
yet.
I added this to the EditText
:
android:imeOptions="actionSearch"
So that's why the keyboard has got a "Search" button in stead of the default "enter"/"done". (Just in case you where wondering).
We cannot disable the action button on the android keyboard. You will have to settle with checking for empty string in editor action listener.
setOnEditorActionListener(new TextView.OnEditorActionListener() {
if (txtView.getText().toString().trim().length() == 0) {
Toast.makeText(getApplicationContext(), "Please Enter some text to search",
Toast.Short).show();
return true; // returning true will keep the keyboard on
}
else search();
try this (may not be the exact result you wanted, but might be the closest you can get.
1)Remove the android:imeOptions="actionSearch" from the xml
2)Create a custom TextWatcher addTextChangedListener(TextWatcher watcher) that allows you to change the keyboard dynamically something like this
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
public void afterTextChanged(Editable s) {
if(firstTime){//a boolean you init to true
firstTime = false;
textMessage.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
}
public void beforeTextChanged(CharSequence s, int start, int count, int after){}
public void onTextChanged(CharSequence s, int start, int before, int count){}
});
you need to make Custom EditText ,Make Extends EditText and @Override Below Method in it.
Here is example code.
private ArrayList<Integer> keysToIgnore = new ArrayList<Integer>();
public EditTextCus(Context context) {
super(context);
}
public EditTextCus(Context context, AttributeSet attrs){
super(context, attrs);
}
public EditTextCus(Context context, AttributeSet attrs, int defStyle){
super (context, attrs, defStyle);
}
private Boolean keyInIgnoreList(int keyCode) {
for (Integer i:keysToIgnore){
int key = i.intValue();
if (key == keyCode)
return true;
}
return false;
}
public void addKeyToIgnoreList(int keyCode) {
if (!keyInIgnoreList(keyCode)){
keysToIgnore.add(keyCode);
}
}
public void removeKeyFromIgnoreList(int keyCode) {
if (keyInIgnoreList(keyCode)){
Integer key=new Integer(keyCode);
keysToIgnore.remove(key);
}
}
@Override
public boolean dispatchKeyEventPreIme(KeyEvent event) {
//Log.d("ws", "dispatchKeyEventPreIme(" + event + ")");
// swallow the any key in the ignore list
if (keyInIgnoreList(event.getKeyCode())) {
KeyEvent.DispatcherState state = getKeyDispatcherState();
if (state != null) {
if (event.getAction() == KeyEvent.ACTION_DOWN
/*&& event.getRepeatCount() == 0*/) {
state.startTracking(event, this);
return true;
} else if (event.getAction() == KeyEvent.ACTION_UP
&& !event.isCanceled() && state.isTracking(event)) {
return true;
}
}
}
}
return super.dispatchKeyEventPreIme(event);
}
you can also do this to Search Key :
mCustomEditText.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_SEARCH && event.getAction()==KeyEvent.KEYCODE_SEARCH){
System.out.println("Search pressed.........");
}
return false;
}
});
Implement OnKeyListener
in your Activity
.
add this keyListener to your EditText
.
myEditText.setOnKeyListener(this);
override onKey()
in your Activity
.
@Override
public boolean onKey(View v, int keyCode, KeyEvent event) {
if(v.getId() == R.id.myEditText)
{
if(KeyEvent.KEYCODE_ENTER == keyCode)
{
if(myEditText.getText().toString().equals("") || myEditText.getText().toString().equals(null))
{
Toast.makeText(KeyboardTestActivity.this, "Event Eaten", Toast.LENGTH_SHORT).show();
Log.e("onKey", "Event Eaten");
return true;
}
}
}
return false;
}
and this is Done it will Display a Toast whenever your EditText
is empty and you press search Key.
Note: onKey()
gets called twice once for keyDown and then for KeyUp. you will have to filter it using KeyEvent instance, received as a parameter in onKey()
method.
Attach OnEditorActionListener to your text field and return true from its onEditorAction method, when actionId is equal to IME_ACTION_DONE. This will prevent soft keyboard from hiding:
EditText txtEdit = (EditText) findViewById(R.id.txtEdit);
txtEdit.setOnEditorActionListener(new OnEditorActionListener() {
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if (actionId == EditorInfo.IME_ACTION_DONE) {
// your additional processing...
return true;
} else {
return false;
}
}
});
SearchInput.addTextChangedListener(new TextWatcher() {
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
if( SearchInput.getText().toString().length() == 0 )
{
Search.setClickable(false);
}
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
Search.setClickable(false);
}
@Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
// TODO Auto-generated method stub
Search.setClickable(true);
}
});
or Try this..! for fancy error dilog
Search.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if( searchinput.getText().toString().length() == 0 )
{
searchinput.setError( "Enter something Blah Blah..!" );
}
else
{
// Original Functions Goes Here
}
Use the following Code in Edit Text,
android:imeOptions="flagNoEnterAction"
I am not able to set flagNoEnterAction = "false"
But someone can help you
来源:https://stackoverflow.com/questions/10484310/disabling-the-keyboards-send-button-when-nothing-has-been-typed-yet-android