我有一个表单,其中我有TextInputEditText,我希望它应该显示一个时间选择器上点击它,但用户应该不能直接输入任何文本到该框。我不会将其替换为TextView或按钮,因为表单中有类似于屏幕截图的框线。我希望保持表单中字段的一致性。

这是EditText的代码--
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edittext_add_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:fontFamily="sans-serif-light"
android:inputType="textAutoComplete"
android:minHeight="@dimen/min_height"
android:paddingLeft="6dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/button_import"
app:startIconDrawable="@drawable/ic_baseline_child_care_24">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edittext_add_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
android:inputType="textEmailAddress" />
</com.google.android.material.textfield.TextInputLayout>我已经尝试过enabled=false,但是这个字段也无法点击。有什么建议吗?我怎样才能达到预期的效果?
发布于 2021-11-02 15:33:56
我希望这段代码能帮助你得到我已经做过很多次的期望输出。
第一件事是对布局进行更改
<com.google.android.material.textfield.TextInputLayout
android:id="@+id/edittext_add_name"
style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginHorizontal="16dp"
android:layout_marginTop="32dp"
android:fontFamily="sans-serif-light"
android:inputType="textAutoComplete"
android:paddingStart="6dp"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlSymmetry">
<com.google.android.material.textfield.TextInputEditText
android:id="@+id/edt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/name"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
/>
</com.google.android.material.textfield.TextInputLayout>在Java端进行此更改以实现输出
public class TestActivity extends AppCompatActivity {
Calendar myCalendar;
TextInputEditText edt ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
edt = findViewById(R.id.edt);
myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener dateListener = (datePicker, year, monthOfYear, dayOfMonth) -> {
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateEdtTextview();
};
edt.setOnClickListener(view -> new DatePickerDialog(
TestActivity.this,
dateListener,
myCalendar.get(Calendar.YEAR),
myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show());
}
private void updateEdtTextview() {
String myFormat = "dd/MM/yyyy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
edt.setText(sdf.format(myCalendar.getTime()));
}}
https://stackoverflow.com/questions/69811972
复制相似问题