Computer >> Máy Tính >  >> Lập trình >> Android

Làm thế nào để đặt ImageView trong edittext khi nhập văn bản?

Ví dụ này trình bày về Cách đặt ImageView trong edittext khi nhập văn bản.

Bước 1 - Tạo một dự án mới trong Android Studio, đi tới Tệp ⇒ Dự án Mới và điền tất cả các chi tiết cần thiết để tạo một dự án mới.

Bước 2 - Thêm mã sau vào res / layout / activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   xmlns:tools="https://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical"
   android:gravity="center"
   android:layout_marginTop="30dp"
   tools:context=".MainActivity">
   <EditText
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
      android:paddingStart="5dp"
      android:background="@drawable/rounded_edittext"
      android:drawableStart="@android:drawable/ic_menu_search"
      android:paddingLeft="5dp" />
</LinearLayout>

Trong đoạn mã trên, chúng tôi đã chỉnh sửa văn bản và thêm nền làm background.xml.

Bước 3 - Thêm mã sau vào drawable / background.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android" >
   <solid android:color="#FFFFFF" />
   <stroke
      android:width="1dp"
      android:color="#2f6699" />
   <corners
      android:radius="10dp" />
</shape>

Bước 4 - Thêm mã sau vào java / MainActivity.xml

package com.example.myapplication;

import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.MainThread;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {
   @TargetApi(Build.VERSION_CODES.LOLLIPOP)
   @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      final EditText editText=findViewById(R.id.edit_query);
      final Drawable image = MainActivity.this.getResources().getDrawable( R.drawable.sir );
      image.setBounds(0, 0, 40, 40);
      editText.addTextChangedListener(new TextWatcher() {
         @Override
         public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            editText.setCompoundDrawables(null,null,null,null);
         }

         @Override
         public void onTextChanged(CharSequence s, int start, int before, int count) {
            if(count>2 && count !=0)
               editText.setCompoundDrawables(image,null,null,null);
         }

         @Override
         public void afterTextChanged(Editable s) {
         }
      });
   }
}

Hãy thử chạy ứng dụng của bạn. Tôi giả sử bạn đã kết nối thiết bị Di động Android thực tế với máy tính của mình. Để chạy ứng dụng từ android studio, hãy mở một trong các tệp hoạt động của dự án của bạn và nhấp vào biểu tượng Chạy từ thanh công cụ. Chọn thiết bị di động của bạn làm tùy chọn, sau đó kiểm tra thiết bị di động sẽ hiển thị màn hình mặc định của bạn -

Làm thế nào để đặt ImageView trong edittext khi nhập văn bản?

Bây giờ nhập 3 hoặc nhiều hơn 3 chữ cái bất kỳ, nó sẽ hiện ra hình ảnh như hình dưới đây -

Làm thế nào để đặt ImageView trong edittext khi nhập văn bản?