我很新于Eclipse,我需要在软键盘上按下完成按钮后通过清除editText中的焦点来隐藏光标。
我发现这段代码对其他人有效:
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;
}
});
活动的代码
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;
}
}
}
但我不知道如何让它工作。我应该把那段代码放在哪里?并且第一行给我一个错误,指出"editText"无法识别,我试图将其设置为我的EditText的唯一ID,但没有起作用。