Android EditText Settings Editable, Non-Editable and Click Events

Keywords: Android xml Java encoding

1. Set up non-editable and clickable events

public void setCanNotEditAndClick(View view) {
        etContent.setFocusable(false);
        etContent.setFocusableInTouchMode(false);
        etContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "do click", Toast.LENGTH_SHORT).show();
            }
        });
    }

2. Set non-editable and no click event

public void setCanNotEditNoClick(View view) {
        etContent.setFocusable(false);
        etContent.setFocusableInTouchMode(false);
        // If you have not set a click event before, you can omit it here
        etContent.setOnClickListener(null);
    }

3. Set Editable

public void setCanEdit(View view) {
        etContent.setFocusable(true);
        etContent.setFocusableInTouchMode(true);
    }

4. Source Code

4.1 XML Layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <EditText
        android:id="@+id/et_content"
        android:layout_width="match_parent"
        android:layout_height="48dp"
        android:background="@null"
        android:hint="Please enter content"
        android:textColor="#666666"
        android:textColorHint="#999999" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setCanNotEditAndClick"
        android:text="Set to non-editable with click events" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setCanNotEditNoClick"
        android:text="Set to non-editable and no click event" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="setCanEdit"
        android:text="Set Editable" />

</LinearLayout>

4.2 Java Code

package com.happy.sms;

import android.content.ContentResolver;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private EditText etContent;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        etContent = findViewById(R.id.et_content);
    }

    public void setCanNotEditAndClick(View view) {
        etContent.setFocusable(false);
        etContent.setFocusableInTouchMode(false);
        etContent.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(MainActivity.this, "do click", Toast.LENGTH_SHORT).show();
            }
        });
    }

    public void setCanNotEditNoClick(View view) {
        etContent.setFocusable(false);
        etContent.setFocusableInTouchMode(false);
        // If you have not set a click event before, you can omit it here
        etContent.setOnClickListener(null);
    }

    public void setCanEdit(View view) {
        etContent.setFocusable(true);
        etContent.setFocusableInTouchMode(true);
    }
}

V. Summary

Above, personal test, available!

If you have problems or have better solutions, please leave a message~

Posted by talper on Fri, 10 Jan 2020 11:18:04 -0800