我通过editText创建搜索功能,该功能可以从TextWatcher中侦听密钥。现在我要做的是,当用户键入键@时,它将显示一个对话框并运行recyclerview以显示对话框中的数据。问题是我无法继续从上一个活动输入,因为它已经在后面的堆栈中,前面的是我创建的DialogFragment。
即使对话框片段显示,我如何继续关注editText中的上一个活动?
来自触发对话的活动的示例代码
edit_text_chatbox.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
int lastString = s.length();
Character character = s.charAt(lastString - 1);
Log.i("after", "afterTextChanged: "+character);
if (character.toString().equals("@")){
AltTaggingDialogFragment dialog = AltTaggingDialogFragment.newInstance();
dialog.show(getFragmentManager(), "dialog");
}
}
我的DialogFragment(AltTaggingDialogFragment)
public class AltTaggingDialogFragment extends DialogFragment{
RecyclerView mRecyclerView;
RecyclerView.Adapter mAdapter_message;
RecyclerView.LayoutManager mLayoutManager;
List<UserFriendList> mUserFriendList;
View view;
EditText friend_tagging;
int friend_count = 7;
@Override
public void onStart() {
super.onStart();
Dialog dialog = getDialog();
Window window = dialog.getWindow();
int width = ViewGroup.LayoutParams.MATCH_PARENT;
int height = ViewGroup.LayoutParams.WRAP_CONTENT;
WindowManager.LayoutParams windowParams = window.getAttributes();
windowParams.dimAmount = 0.0f;
dialog.getWindow().setLayout(width,height);
windowParams.gravity = Gravity.BOTTOM;
//windowParams.x = width;
windowParams.y = 90;
if (friend_count >= 7)
windowParams.height = (210 * 4) + 150;
dialog.getWindow().setAttributes(windowParams);
dialog.getWindow().setSoftInputMode (WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
}
public static AltTaggingDialogFragment newInstance() {
AltTaggingDialogFragment f = new AltTaggingDialogFragment();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//inflate layout with recycler view
view = inflater.inflate(R.layout.fragment_alt_tagging_dialog, container, false);
friend_tagging = view.findViewById(R.id.friend_tagging);
mRecyclerView = view.findViewById(R.id.alt_tagging_recycler_view);
mRecyclerView.setHasFixedSize(true);
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
friend_tagging.requestFocus();
friend_tagging.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
Toast.makeText(getActivity(), "Toast!", Toast.LENGTH_SHORT).show();
}
});
mUserFriendList = new ArrayList<>();
UserFriendList userFriendList = new UserFriendList();
super.onViewCreated(view, savedInstanceState);
for (int i = 0; i < 8; i++) {
userFriendList.setFirst_name("Mar John");
userFriendList.setLast_name("Saycon");
userFriendList.setUsername("MJ");
mUserFriendList.add(userFriendList);
}
mAdapter_message = new AltTaggingAdapter(getActivity(), mUserFriendList);
mRecyclerView.setAdapter(mAdapter_message);
return view;
}
}
此图显示了我尝试做的事情。 Sample Image