我有一个编辑文本和按钮。 默认情况下,此按钮不可见。
当我点击编辑文本时,它会显示soft-keyborad,此按钮可见。 我抓住了编辑文本的点击事件,并设置此按钮是可见的。但是,直到我点击第二个编辑文本(显示在soft-keyborad中)才能看到它
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:orientation="vertical" >
<EditText
android:id="@+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:ems="10" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/editText1"
android:text="Button" />
</RelativeLayout>
代码
public class anr extends Activity {
InputMethodManager imm;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
imm = (InputMethodManager)this.getSystemService(Context.INPUT_METHOD_SERVICE);
super.onCreate(savedInstanceState);
setContentView(R.layout.testaaa);
final Button btn = (Button)this.findViewById(R.id.button1);
btn.setVisibility(View.INVISIBLE);
final EditText txtSearch = (EditText)this.findViewById(R.id.editText1);
txtSearch.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
txtSearch.setFocusable(true);//(false);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
btn.setVisibility(View.VISIBLE);
}
});
txtSearch.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
btn.setVisibility(View.INVISIBLE);
hideSoftKeyboard(v);
txtSearch.clearFocus();
}
return false;
}});
}
public void hideSoftKeyboard (View view) {
imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
}
答案 0 :(得分:1)
请为您的编辑文字设置触控监听器。
并放置你的onclick方法,即
txtSearch.setFocusable(true);
txtSearch.setFocusableInTouchMode(true);
txtSearch.requestFocus();
btn.setVisibility(View.VISIBLE);
那里。 :)
答案 1 :(得分:0)
试试这段代码......
txtSearch.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(View.INVISIBLE);
hideSoftKeyboard(v);
txtSearch.clearFocus();
}