There is an input field, the amount is entered into it, I would like it so that they clicked on the field, such a cross appeared, as you know in the browser, if you click on the address line, to the right a cross appears that you can delete immediately.
1 answer
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <FrameLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_margin="10dp" android:background="#cccccc" > <EditText android:id="@+id/text" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:textColor="#ff0000" android:singleLine="true" /> <Button android:id="@+id/btn_clear" android:layout_width="25dp" android:layout_height="25dp" android:layout_gravity="end|center_vertical" android:layout_marginEnd="10dp" android:layout_marginRight="10dp" android:background="@drawable/delete" android:visibility="gone" /> </FrameLayout> </RelativeLayout> Class:
private EditText editText; private Button btnClear; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText)findViewById(R.id.text); btnClear = (Button)findViewById(R.id.btn_clear); editText.addTextChangedListener(textWatcher()); btnClear.setOnClickListener(onClickListener()); } private OnClickListener onClickListener() { return new OnClickListener() { @Override public void onClick(View v) { editText.setText(""); //очищаем edittext } }; } private TextWatcher textWatcher() { return new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if (!editText.getText().toString().equals("")) {//проверка на пустоту edittext и в зависимости от этого выводить ли кнопку (х) btnClear.setVisibility(View.VISIBLE); } else { btnClear.setVisibility(View.GONE); } } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { } @Override public void afterTextChanged(Editable s) { } }; } PS Left exactly what you need.
- I inserted your cheto button, it does not show it on my prntscr.com/c7g3od layer - then some yellow and red squares appear on the new one that I create - Alex Lizenberg
- @ AlexLizenberg show the code as pasted - iFr0z
|