Android: Scroll to field with error when error occurs

This commit is contained in:
JosJuice 2021-08-10 15:12:05 +02:00
parent e299be1d85
commit 109aef4b81
1 changed files with 9 additions and 0 deletions

View File

@ -8,6 +8,7 @@ import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText; import android.widget.EditText;
import android.widget.ScrollView;
import android.widget.TextView; import android.widget.TextView;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -23,6 +24,8 @@ import org.dolphinemu.dolphinemu.features.cheats.model.CheatsViewModel;
public class CheatDetailsFragment extends Fragment public class CheatDetailsFragment extends Fragment
{ {
private View mRoot; private View mRoot;
private ScrollView mScrollView;
private TextView mLabelName;
private EditText mEditName; private EditText mEditName;
private TextView mLabelCreator; private TextView mLabelCreator;
private EditText mEditCreator; private EditText mEditCreator;
@ -48,6 +51,8 @@ public class CheatDetailsFragment extends Fragment
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState)
{ {
mRoot = view.findViewById(R.id.root); mRoot = view.findViewById(R.id.root);
mScrollView = view.findViewById(R.id.scroll_view);
mLabelName = view.findViewById(R.id.label_name);
mEditName = view.findViewById(R.id.edit_name); mEditName = view.findViewById(R.id.edit_name);
mLabelCreator = view.findViewById(R.id.label_creator); mLabelCreator = view.findViewById(R.id.label_creator);
mEditCreator = view.findViewById(R.id.edit_creator); mEditCreator = view.findViewById(R.id.edit_creator);
@ -94,15 +99,19 @@ public class CheatDetailsFragment extends Fragment
break; break;
case Cheat.TRY_SET_FAIL_NO_NAME: case Cheat.TRY_SET_FAIL_NO_NAME:
mEditName.setError(getString(R.string.cheats_error_no_name)); mEditName.setError(getString(R.string.cheats_error_no_name));
mScrollView.smoothScrollTo(0, mLabelName.getTop());
break; break;
case Cheat.TRY_SET_FAIL_NO_CODE_LINES: case Cheat.TRY_SET_FAIL_NO_CODE_LINES:
mEditCode.setError(getString(R.string.cheats_error_no_code_lines)); mEditCode.setError(getString(R.string.cheats_error_no_code_lines));
mScrollView.smoothScrollTo(0, mEditCode.getBottom());
break; break;
case Cheat.TRY_SET_FAIL_CODE_MIXED_ENCRYPTION: case Cheat.TRY_SET_FAIL_CODE_MIXED_ENCRYPTION:
mEditCode.setError(getString(R.string.cheats_error_mixed_encryption)); mEditCode.setError(getString(R.string.cheats_error_mixed_encryption));
mScrollView.smoothScrollTo(0, mEditCode.getBottom());
break; break;
default: default:
mEditCode.setError(getString(R.string.cheats_error_on_line, result)); mEditCode.setError(getString(R.string.cheats_error_on_line, result));
mScrollView.smoothScrollTo(0, mEditCode.getBottom());
break; break;
} }
} }