[英]Make TextInputEditText readonly but clickable
I have a form in which I have TextInputEditText, I want that it should show a timepicker on clicking it but user should not be able to enter any text directly into that box.我有一个包含 TextInputEditText 的表单,我希望它应该在单击它时显示一个时间选择器,但用户不应直接在该框中输入任何文本。 I am not replacing this with a TextView or a Button because the form has outlined boxes similar to the screenshot.我不会用 TextView 或 Button 替换它,因为表单具有类似于屏幕截图的轮廓框。 I want to maintain the uniformity of fields in the form.我想保持表单中字段的一致性。
This is the code for the EditText --这是 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>
I have tried enabled=false
but then the field is not clickable also.我试过enabled=false
但该字段也无法点击。 Any recommendation how can I achieve the desired result?任何建议我怎样才能达到预期的结果?
I hope this code will help you out in the desired output i have done same thing many times.我希望这段代码能帮助您获得所需的输出,我已经多次做了同样的事情。
1st thing do the changes in layout第一件事做布局的变化
<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>
In Java side do this changes to achieve the output在 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()));
}
} }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.