我有一个带有一堆EditText行的UI,供用户输入数据,c / f / gListenerEditTexts。当用户在fListenerEditText行上输入数据并按Back键时,DiscardFragment对话框将正确打开。如果用户单击对话框上的“确定”按钮,对话框将关闭,数据将被丢弃,用户将返回上一个活动。我的问题是当用户按下Back键时,gListenerEditText行无法按预期工作。在这种情况下,DiscardFragment对话框将打开1或2秒,然后自动关闭,UI将返回上一个Activity。没有用户点击“确定”按钮就会发生这种情况。我在这里缺少什么?
EditActivity.java
public class EditActivity extends AppCompatActivity
private ListenerEditText cListenerEditText,fListenerEditText,GListenerEditText;
@Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(layout.activity_edit);
cListenerEditText = (ListenerEditText) findViewById(id.CEditText);
fListenerEditText = (ListenerEditText) findViewById(id.FEditText);
gListenerEditText = (ListenerEditText) findViewById(id.GEditText);
...
cListenerEditText.setKeyImeChangeListener(new ListenerEditText.KeyImeChange() {
@Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
int stringToDo = cListenerEditText.getText().toString().replace(" ","").length();
int stringDueDate = fListenerEditText.getText().toString().replace(" ","").length();
int stringDueTime = gListenerEditText.getText().toString().replace(" ","").length();
// If the EditText input line has data ("stringToDo") and the user presses the Back button,
// then launch the DialogFragment to see if they really want to delete the data and
// go back to the previous activity.
if (stringToDo > 0) {
FragmentManager fm3 = getSupportFragmentManager();
DiscardFragment editNameDialog3 = new DiscardFragment();
editNameDialog3.show(fm3,"s_dialog");
}
// when a date is entered by the user, this test works as expected
else if (stringToDo == 0 && stringDueDate > 0) {
FragmentManager fm4 = getSupportFragmentManager();
DiscardFragment editNameDialog4 = new DiscardFragment();
editNameDialog4.show(fm4,"s_dialog");
}
// when a time is entered by the user, this test is not working as expected
else if (stringToDo == 0 && stringDueTime > 0) {
FragmentManager fm5 = getSupportFragmentManager();
DiscardFragment editNameDialog5 = new DiscardFragment();
editNameDialog5.show(fm5,"s_dialog");
}
else if (stringToDo == 0){
// close the softkeyboard
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(dListenerEditText.getWindowToken(),0);
}
return true;
}
});
DiscardFragment.java
public class DiscardFragment extends DialogFragment{
public DiscardSkycardFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.discardfrag_layout,container,false);
getDialog().setTitle("Discard");
// When the user clicks "Cancel" in the "Delete" dialog, return to the CardViewActivity.
Button btnCancel = (Button) rootView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
InputMethodManager imm = (InputMethodManager) getActivity().getSystemService(EditActivity.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
getDialog().cancel();
}
});
// When the user clicks "OK" to delete for the "Delete" dialog.
Button btnOK = (Button) rootView.findViewById(R.id.btnOK);
btnOK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dismiss();
// Next line returns to the previous activity (MainActivity) by closing
// the current activity (CardViewActivity).
getActivity().finish();
}
});
// handles when the user presses the Back Button when the "Delete" dialog is open.
getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// This filters the event as the back button press
if (event.getAction()!= KeyEvent.ACTION_DOWN)
// Next line dismisses the dialogfragment
dismiss();
// Next line returns to the previous activity (MainActivity) by closing the fragment and
// the current activity (CardViewActivity).
getActivity().finish();
return true;
}
return false;
}
});
return rootView;
}
}
答案 0 :(得分:0)
试试这个。
cListenerEditText.setKeyImeChangeListener(new ListenerEditText.KeyImeChange() {
@Override
public boolean onKeyIme(int keyCode, KeyEvent event) {
if(event.getAction()==KeyEvent.ACTION_UP) {
int stringToDo = cListenerEditText.getText().toString().replace(" ","").length();
int stringDueDate = fListenerEditText.getText().toString().replace(" ","").length();
int stringDueTime = gListenerEditText.getText().toString().replace(" ","").length();
// If the EditText input line has data ("stringToDo") and the user presses the Back button,
// then launch the DialogFragment to see if they really want to delete the data and
// go back to the previous activity.
if (stringToDo > 0) {
FragmentManager fm3 = getSupportFragmentManager();
DiscardFragment editNameDialog3 = new DiscardFragment();
editNameDialog3.show(fm3,"s_dialog");
}
// when a date is entered by the user, this test works as expected
else if (stringToDo == 0 && stringDueDate > 0) {
FragmentManager fm4 = getSupportFragmentManager();
DiscardFragment editNameDialog4 = new DiscardFragment();
editNameDialog4.show(fm4,"s_dialog");
}
// when a time is entered by the user, this test is not working as expected
else if (stringToDo == 0 && stringDueTime > 0) {
FragmentManager fm5 = getSupportFragmentManager();
DiscardFragment editNameDialog5 = new DiscardFragment();
editNameDialog5.show(fm5,"s_dialog");
}
else if (stringToDo == 0){
// close the softkeyboard
InputMethodManager imm = (InputMethodManager)
getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(dListenerEditText.getWindowToken(),0);
}
}
return true;
}
});