过去我已经能够成功设置一个onKeyListener来检测用户是否选择了android键盘上的返回按钮,然后这会触发并动作将用户发送到另一个活动,但我现在需要构建将相同的功能集成到一个片段中,但是当我在片段中使用setOnKeyListener时由于某种原因它不起作用,下面我将发布代码
我的EditText
<EditText
android:id="@+id/searchetedittext"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:background="@drawable/vineline_edittext_inner_background"
android:gravity="top"
android:hint="Search #Hashtags"
android:imeOptions="actionSend"
android:inputType="textCapSentences|textMultiLine|textAutoCorrect|textAutoComplete"
android:lines="1"
android:padding="5dp"
android:textColor="@color/button_color_dark"
android:textSize="22dp" />
我的setOnKeyListener
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View v;
if (getArguments().getInt(EXTRA_SEARCH_TYPE) == 0) {
v = inflater.inflate(R.layout.content_of_tab1, container, false);
lv = (ListView) v.findViewById(R.id.searchtrendlist);
pb = (ProgressBar) v.findViewById(R.id.progressbartab1);
final LinearLayout searchButton = (LinearLayout) v
.findViewById(R.id.searchbutton);
final EditText searchEdittext = (EditText) v
.findViewById(R.id.searchetedittext);
searchEdittext.setTypeface(textStyleThin);
searchEdittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
searchButton.performClick();
return true;
default:
break;
}
}
return false;
}
});
searchButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
postText = searchEdittext.getText().toString();
Intent intent = new Intent(context,
HashtagScreenActivity.class);
intent.putExtra("screenname", postText);
startActivity(intent);
searchEdittext.setText("");
}
});
new setupTrends().execute();
} else {
v = inflater.inflate(R.layout.content_of_tab2, container, false);
lv = (ListView) v.findViewById(R.id.searchfriendlist);
pb = (ProgressBar) v.findViewById(R.id.progressbartab2);
final LinearLayout searchFriendButton = (LinearLayout) v
.findViewById(R.id.searchfriendbutton);
final EditText searchFriendEdittext = (EditText) v
.findViewById(R.id.searchfriendedittext);
searchFriendEdittext.setTypeface(textStyleThin);
searchFriendEdittext.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_DOWN) {
switch (keyCode) {
case KeyEvent.KEYCODE_ENTER:
searchFriendButton.performClick();
return true;
default:
break;
}
}
return false;
}
});
searchFriendEdittext.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable text) {
}
public void beforeTextChanged(CharSequence s, int start,
int count, int after) {
}
public void onTextChanged(CharSequence s, int start,
int before, int count) {
try {
fdb.open();
if (s.toString().matches("")) {
friendListItems = fdb.getFriends();
} else {
friendListItems = fdb.sortFriends(s.toString());
}
userListsAdapter = new SearchFriendsAdapter(
getActivity(), friendListItems);
lv.setAdapter(userListsAdapter);
fdb.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
searchFriendEdittext.requestFocus();
}
});
searchFriendButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
postFriendText = searchFriendEdittext.getText().toString();
if (postFriendText.length() > 0) {
Intent intent = new Intent(getActivity(),
FriendProfileActivity.class);
intent.putExtra("screenname", postFriendText);
startActivity(intent);
searchFriendEdittext.setText("");
} else {
Toast.makeText(getActivity(),
"Please enter a twitter username",
Toast.LENGTH_LONG).show();
}
}
});
fdb.open();
friendListItems = fdb.getFriends();
userListsAdapter = new SearchFriendsAdapter(getActivity(),
friendListItems);
lv.setAdapter(userListsAdapter);
fdb.close();
pb.setVisibility(View.GONE);
}
addHeader();
return v;
}
答案 0 :(得分:1)
这是一个古老的问题,但是当我设置edittext的输入类型android:inputType="textPersonName"