我是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,但它没有用。
答案 0 :(得分:4)
试试这个
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;
}
});
答案 1 :(得分:1)
您需要在onCreate()之内(或之后)通过id找到您的视图。
在EditText edittext = (EditText) findViewById(R.id.editText1);
onCreate()
内移动super.onCreate()