我正在尝试创建一个 Todo 应用程序,在该应用程序中我使用的是 bottomsheetfragment,但是当我显示它时。它隐藏在键盘下方。
我已经尝试了 Stack Overflow 上的所有解决方案,所以在答案中,我们从 bottomsheetfragmet 的 oncreate 方法中设置了片段的样式。这对我有用。
但是,每当我第一次运行应用程序或打开应用程序时,键盘会重叠或隐藏片段,但是当我第二次打开时它工作正常并且片段没有隐藏在键盘下,所以任何人都可以说出为什么会这样只有第一次。
样式.xml
<resources>
<style name="DialogStyle" parent="Theme.MaterialComponents.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">@android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize|stateVisible</item>
</style>
</resources>
bottom_sheet.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottomSheet"
style="@style/Animation.Design.BottomSheetDialog"
android:background="#fff"
app:behavior_hideable="false"
app:behavior_peekHeight="@dimen/dimen_32"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<EditText
android:id="@+id/enter_todo_et"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/dimen_16"
android:hint="@string/enter_todo_hint"
android:inputType="textPersonName"
android:padding="@dimen/dimen_16"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="@+id/priority_todo_button"
android:layout_width="77dp"
android:layout_height="46dp"
android:layout_marginStart="24dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
app:layout_constraintStart_toEndOf="@+id/today_calendar_button"
app:layout_constraintTop_toBottomOf="@+id/enter_todo_et"
app:srcCompat="@drawable/ic_baseline_outlined_flag_24"
android:contentDescription="@string/image_description" />
<ImageButton
android:id="@+id/save_todo_button"
android:layout_width="70dp"
android:layout_height="72dp"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
android:scaleX="0.4"
android:scaleY="0.4"
android:contentDescription="TODO"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.843"
app:layout_constraintStart_toEndOf="@+id/priority_todo_button"
app:layout_constraintTop_toBottomOf="@+id/enter_todo_et"
app:srcCompat="@drawable/ic_baseline_arrow_circle_up_24" />
<ImageButton
android:id="@+id/today_calendar_button"
android:layout_width="74dp"
android:layout_height="48dp"
android:layout_marginStart="16dp"
android:contentDescription="@string/image_description"
android:layout_marginTop="16dp"
android:background="@android:color/transparent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/enter_todo_et"
app:srcCompat="@drawable/ic_baseline_calendar_today_24" />
<com.google.android.material.chip.Chip
android:id="@+id/tomorrow_chip"
android:layout_width="269dp"
android:layout_height="41dp"
android:text="@string/tomorrow"
app:chipIcon="@drawable/ic_baseline_wb_sunny_24"
app:layout_constraintEnd_toEndOf="@+id/next_week_chip"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="@+id/next_week_chip"
app:layout_constraintTop_toBottomOf="@+id/today_chip" />
<com.google.android.material.chip.Chip
android:id="@+id/next_week_chip"
android:layout_width="272dp"
android:layout_height="41dp"
android:background="@android:color/transparent"
android:text="@string/next_week"
app:chipIcon="@drawable/ic_baseline_next_week_24"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tomorrow_chip" />
<com.google.android.material.chip.Chip
android:id="@+id/today_chip"
android:layout_width="267dp"
android:layout_height="41dp"
android:layout_marginTop="2dp"
android:text="@string/today"
app:chipIcon="@drawable/ic_baseline_today_24"
app:layout_constraintEnd_toEndOf="@+id/tomorrow_chip"
app:layout_constraintStart_toStartOf="@+id/tomorrow_chip"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<TextView
android:id="@+id/textView"
android:layout_width="244dp"
android:layout_height="33dp"
android:layout_marginTop="48dp"
android:gravity="center_horizontal"
android:text="@string/due_date_text"
android:textSize="18sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/priority_todo_button" />
<CalendarView
android:id="@+id/calendar_view"
android:layout_width="318dp"
android:layout_height="296dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.494"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/next_week_chip"
app:layout_constraintVertical_bias="0.16000003" />
<RadioGroup
android:id="@+id/radioGroup_priority"
android:layout_width="0dp"
android:layout_height="51dp"
android:layout_marginTop="8dp"
android:orientation="horizontal"
android:padding="@dimen/dimen_10"
android:visibility="gone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/priority_todo_button">
<RadioButton
android:id="@+id/radioButton_high"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#C91517"
android:text="@string/radio_high" />
<RadioButton
android:id="@+id/radioButton_med"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFB300"
android:text="@string/radio_med" />
<RadioButton
android:id="@+id/radioButton_low"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="@android:color/holo_blue_light"
android:text="@string/radio_low" />
</RadioGroup>
<androidx.constraintlayout.widget.Group
android:id="@+id/calendar_group"
android:layout_width="280dp"
android:layout_height="0dp"
android:visibility="gone"
app:constraint_referenced_ids="calendar_view,next_week_chip,tomorrow_chip,today_chip,textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
BottomSheetFragment.java
public class BottomSheetFragment extends BottomSheetDialogFragment implements View.OnClickListener {
private EditText enterTodo;
private ImageButton calendarButton;
private ImageButton priorityButton;
private ImageButton saveButton;
private CalendarView calendarView;
private RadioGroup radioGroup;
private RadioButton selectedPriorityButton;
private int selectedButtonId;
private Date dueDate;
private Group calendarGroup;
private SharedViewModel sharedViewModel;
Calendar calendar = Calendar.getInstance();
public BottomSheetFragment(){
}
@Override
public View onCreateView(
LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState
) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.bottom_sheet, container, false);
calendarButton = view.findViewById(R.id.today_calendar_button);
priorityButton = view.findViewById(R.id.priority_todo_button);
saveButton = view.findViewById(R.id.save_todo_button);
enterTodo = view.findViewById(R.id.enter_todo_et);
calendarView = view.findViewById(R.id.calendar_view);
calendarGroup = view.findViewById(R.id.calendar_group);
radioGroup = view.findViewById(R.id.radioGroup_priority);
setStyle(BottomSheetFragment.STYLE_NORMAL,R.style.DialogStyle);
Chip todayChip = view.findViewById(R.id.today_chip);
todayChip.setOnClickListener(this);
Chip tomorrowChip = view.findViewById(R.id.tomorrow_chip);
tomorrowChip.setOnClickListener(this);
Chip nextWeekChip = view.findViewById(R.id.next_week_chip);
nextWeekChip.setOnClickListener(this);
return view;
}
@Override
public void onResume() {
super.onResume();
if(sharedViewModel.getSelectedTask().getValue()!=null){
Task task = sharedViewModel.getSelectedTask().getValue();
enterTodo.setText(task.getTaskName());
}
}
public void onViewCreated(@NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
enterTodo.requestFocus();
InputMethodManager imm = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
sharedViewModel = new ViewModelProvider(requireActivity()).get(SharedViewModel.class);
calendarButton.setOnClickListener(view12 -> {
calendarGroup.setVisibility(
calendarGroup.getVisibility() ==View.VISIBLE ? View.GONE:View.VISIBLE
);
});
calendarView.setOnDateChangeListener((calendarView, year, month, dayOfMonth) -> {
calendar.clear();
calendar.set(year,month,dayOfMonth);
dueDate = calendar.getTime();
});
saveButton.setOnClickListener(view1 -> {
String task = enterTodo.getText().toString().trim();
if (!TextUtils.isEmpty(task) && dueDate !=null){
Task todoTask = new Task(task, Priority.HIGH,dueDate,Calendar.getInstance().getTime(),false);
if (sharedViewModel.getEdit()){
Task updateTask = sharedViewModel.getSelectedTask().getValue();
updateTask.setTaskName(task);
updateTask.setDueDate(dueDate);
updateTask.setTodayDate(Calendar.getInstance().getTime());
updateTask.setPriority(Priority.HIGH);
TaskViewModel.update(updateTask);
sharedViewModel.isEditable(false);
}else {
TaskViewModel.insert(todoTask);
}
}
});
}
@Override
public void onClick(View view) {
int id = view.getId();
calendar = Calendar.getInstance();
if (id==R.id.today_chip){
calendar.add(Calendar.DAY_OF_YEAR,0);
dueDate=calendar.getTime();
Log.d("TAG", "onClick: " +dueDate);
}else if (id==R.id.tomorrow_chip){
calendar.add(Calendar.DAY_OF_YEAR,1);
dueDate=calendar.getTime();
Log.d("TAG", "onClick: " +dueDate);
}else if (id==R.id.next_week_chip){
calendar.add(Calendar.DAY_OF_YEAR,7);
dueDate=calendar.getTime();
Log.d("TAG", "onClick: " +dueDate);
}
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity implements OnTodoClickListener {
private TaskViewModel taskViewModel;
private RecyclerView recyclerView;
private RecyclerViewAdapter recyclerViewAdapter;
private BottomSheetFragment bottomSheetFragment;
private SharedViewModel sharedViewModel;
private BottomSheetBehavior<NestedScrollView> bottomSheetBehavior;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
bottomSheetFragment = new BottomSheetFragment();
NestedScrollView constraintLayout = findViewById(R.id.bottomSheet);
bottomSheetBehavior = BottomSheetBehavior.from(constraintLayout);
bottomSheetBehavior.setPeekHeight(BottomSheetBehavior.STATE_HIDDEN);
recyclerView = findViewById(R.id.recycler_view);
sharedViewModel = new ViewModelProvider(this).get(SharedViewModel.class);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
taskViewModel = new ViewModelProvider.AndroidViewModelFactory(MainActivity.this.getApplication())
.create(TaskViewModel.class);
taskViewModel.getAllTasks().observe(this,tasks->{
recyclerViewAdapter = new RecyclerViewAdapter(tasks, this);
recyclerView.setAdapter(recyclerViewAdapter);
});
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
bottomSheetFragment.show(getSupportFragmentManager(),bottomSheetFragment.getTag());
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void onClick(Task task) {
sharedViewModel.setSelectedTask(task);
sharedViewModel.isEditable(true);
bottomSheetFragment.show(getSupportFragmentManager(),bottomSheetFragment.getTag());
bottomSheetBehavior.setPeekHeight(BottomSheetBehavior.STATE_EXPANDED);
}
@Override
public void onRadioButtonClick(Task task) {
TaskViewModel.delete(task);
recyclerViewAdapter.notifyDataSetChanged();
}
}