mirror of
https://github.com/material-components/material-components-android.git
synced 2026-01-17 10:21:51 +08:00
This should be considered a pre-alpha of timepicker. Try the component today, on the latest snapshot: https://github.com/material-components/material-components-android/blob/master/docs/using-snapshot-version.md The initial API offers feature parity with android.app.TimePickerDialog Resolves https://github.com/material-components/material-components-android/issues/690 PiperOrigin-RevId: 318879238
150 lines
4.4 KiB
Java
150 lines
4.4 KiB
Java
/*
|
|
* Copyright (C) 2020 The Android Open Source Project
|
|
*
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
* you may not use this file except in compliance with the License.
|
|
* You may obtain a copy of the License at
|
|
*
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
*
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
* See the License for the specific language governing permissions and
|
|
* limitations under the License.
|
|
*/
|
|
|
|
package com.google.android.material.timepicker;
|
|
|
|
import com.google.android.material.R;
|
|
|
|
import android.content.Context;
|
|
import android.text.Editable;
|
|
import android.text.InputFilter;
|
|
import android.text.TextUtils;
|
|
import android.text.TextWatcher;
|
|
import android.util.AttributeSet;
|
|
import android.view.LayoutInflater;
|
|
import android.widget.Checkable;
|
|
import android.widget.EditText;
|
|
import android.widget.FrameLayout;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import com.google.android.material.chip.Chip;
|
|
import com.google.android.material.internal.TextWatcherAdapter;
|
|
import com.google.android.material.textfield.TextInputLayout;
|
|
import java.util.Arrays;
|
|
|
|
/**
|
|
* A {@link Chip} that can switch to a {@link TextInputLayout} when checked to modify it's content.
|
|
* It keeps the helper text from the TextInput always visible.
|
|
*/
|
|
class ChipTextInputComboView extends FrameLayout implements Checkable {
|
|
|
|
private final Chip chip;
|
|
private final TextInputLayout textInputLayout;
|
|
private final EditText editText;
|
|
private TextWatcher watcher;
|
|
|
|
public ChipTextInputComboView(@NonNull Context context) {
|
|
this(context, null);
|
|
}
|
|
|
|
public ChipTextInputComboView(@NonNull Context context, @Nullable AttributeSet attrs) {
|
|
this(context, attrs, 0);
|
|
}
|
|
|
|
public ChipTextInputComboView(
|
|
@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
|
|
super(context, attrs, defStyleAttr);
|
|
LayoutInflater inflater = LayoutInflater.from(context);
|
|
chip = (Chip) inflater.inflate(R.layout.material_time_chip, this, false);
|
|
textInputLayout = (TextInputLayout) inflater.inflate(R.layout.material_time_input, this, false);
|
|
editText = textInputLayout.getEditText();
|
|
editText.setVisibility(INVISIBLE);
|
|
watcher = new HintSetterTextWatcher();
|
|
editText.addTextChangedListener(watcher);
|
|
addView(chip);
|
|
addView(textInputLayout);
|
|
}
|
|
|
|
@Override
|
|
public boolean isChecked() {
|
|
return chip.isChecked();
|
|
}
|
|
|
|
@Override
|
|
public void setChecked(boolean checked) {
|
|
chip.setChecked(checked);
|
|
editText.setVisibility(checked ? VISIBLE : INVISIBLE);
|
|
chip.setVisibility(checked ? GONE : VISIBLE);
|
|
if (isChecked()) {
|
|
editText.requestFocus();
|
|
}
|
|
}
|
|
|
|
@Override
|
|
public void toggle() {
|
|
chip.toggle();
|
|
}
|
|
|
|
public void setText(CharSequence text) {
|
|
chip.setText(text);
|
|
EditText editText = textInputLayout.getEditText();
|
|
if (!TextUtils.isEmpty(editText.getText())) {
|
|
editText.removeTextChangedListener(watcher);
|
|
editText.setText(null);
|
|
editText.setHint(HintSetterTextWatcher.DEFAULT_HINT);
|
|
editText.addTextChangedListener(watcher);
|
|
}
|
|
editText.setHint(text);
|
|
}
|
|
|
|
@Override
|
|
public void setOnClickListener(@Nullable OnClickListener l) {
|
|
chip.setOnClickListener(l);
|
|
}
|
|
|
|
@Override
|
|
public void setTag(int key, Object tag) {
|
|
chip.setTag(key, tag);
|
|
}
|
|
|
|
public void setHelperText(CharSequence helperText) {
|
|
textInputLayout.setHelperText(helperText);
|
|
}
|
|
|
|
public void setError(String error) {
|
|
textInputLayout.setError(error);
|
|
}
|
|
|
|
public void setCursorVisible(boolean visible) {
|
|
editText.setCursorVisible(visible);
|
|
}
|
|
|
|
public void addInputFilter(InputFilter filter) {
|
|
InputFilter[] current = editText.getFilters();
|
|
InputFilter[] arr = Arrays.copyOf(current, current.length + 1);
|
|
arr[current.length] = filter;
|
|
editText.setFilters(arr);
|
|
}
|
|
|
|
public TextInputLayout getTextInput() {
|
|
return textInputLayout;
|
|
}
|
|
|
|
private class HintSetterTextWatcher extends TextWatcherAdapter {
|
|
|
|
private static final String DEFAULT_HINT = "00";
|
|
@Override
|
|
public void afterTextChanged(Editable editable) {
|
|
if (TextUtils.isEmpty(editable)) {
|
|
setText(DEFAULT_HINT);
|
|
return;
|
|
}
|
|
|
|
chip.setText(editable.toString());
|
|
}
|
|
}
|
|
}
|