我有一個編輯文本和按鈕。 默認情況下,此按鈕不可見。單擊編輯文本時無法顯示按鈕
當我點擊編輯文本時,它顯示soft-keyborad,這個按鈕是可見的。 我抓住編輯文本的點擊事件,並設置此按鈕是可見的。然而,直到我點擊第二次進入編輯文本(這是在軟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);
}
}
嘗試setOnTouchListener,而不是... – dreamcoder 2012-04-18 09:56:16