使 TextInputEditText 只读但可点击

Make TextInputEditText readonly but clickable

我有一个包含 TextInputEditText 的表单,我希望它在单击时显示一个时间选择器,但用户不能直接在该框中输入任何文本。我没有用 TextView 或 Button 替换它,因为该表单具有类似于屏幕截图的轮廓框。我想保持表单中字段的一致性。

这是 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 但该字段也不可点击。任何建议我怎样才能达到预期的结果?

我希望这段代码能帮助你得到想要的输出我已经做过很多次同样的事情了。

第一件事就是改变布局

<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()));
}

}