gpt4 book ai didi

java - 按下完成后,Eclipse editText 移除焦点(光标)

转载 作者:太空宇宙 更新时间:2023-11-03 12:53:23 24 4
gpt4 key购买 nike

我是 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;
}
});

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;
}
}
}

但我不知道如何完成这项工作。我应该在哪里放置该代码?并且还在第一行“editText”处给我一个错误,因为没有重新识别,我试着给它我的 edittext 的唯一 ID,但它没有用。

最佳答案

试试这个

    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;
}
});

关于java - 按下完成后,Eclipse editText 移除焦点(光标),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25763788/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com