i'm pretty new with Eclipse and i have to hide the cursor by clearing the focus in the editText after the done button is pressed in the soft keyboard.
I found that this code work for others:
editText.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
editText.clearFocus();
}
return false;
}
});
the code of the activity
package com.iprogettisti.cppp;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
public class VisualizzaPagina extends ActionBarActivity {
EditText edittext = (EditText) findViewById(R.id.editText1);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (MainActivity.TipoPagina == 1) {
setContentView(R.layout.fragment_visualizza_pagina);
}
else if (MainActivity.TipoPagina == 2) {
setContentView(R.layout.fragment_visualizza_pagina2);
}
else if (MainActivity.TipoPagina == 3) {
setContentView(R.layout.fragment_visualizza_pagina3);
}
else if (MainActivity.TipoPagina == 4) {
setContentView(R.layout.fragment_visualizza_pagina4);
}
else if (MainActivity.TipoPagina == 5) {
setContentView(R.layout.fragment_visualizza_pagina5);
}
edittext.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
edittext.setCursorVisible(false);
}
return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.visualizza_pagina, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_visualizza_pagina, container, false);
return rootView;
}
}
}
but i cant figure it out how to get that work. Where should i place that code? And also give me an errore at the first line "editText" as not reconized, i tryied to give that the unique ID of my edittext but it didnt worked.
Try this
EditText edittext = (EditText) findViewById(R.id.edittext1<your unique ID>);
edittext.setOnEditorActionListener(new OnEditorActionListener() {
@Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(actionId==EditorInfo.IME_ACTION_DONE){
//Clear focus here from edittext
edittext.setCursorVisible(false);
}
return false;
}
});
edittext.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
edittext.setCursorVisible(true);
return false;
}
});
You need to find your view by id within (or after) the onCreate().
Move EditText edittext = (EditText) findViewById(R.id.editText1);
inside your onCreate()
after the super.onCreate()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.