输入时清除 EditText 中的文本

发布于 2024-10-22 03:32:14 字数 707 浏览 2 评论 0原文

我正在尝试设置 onclicklistener ,以便当我在 edittext 元素中单击时,它将清除其当前内容。这里有什么问题吗?当我编译此代码时,我收到强制退出和 ActivityManager: Can't split DDM chunk 4d505251: no handler Defined 错误。

public class Project extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    EditText editText = (EditText)findViewById(R.id.editText1);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        editText.setOnClickListener(this);

        setContentView(R.layout.main);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        editText.setText("");
    }
}

I'm trying to set and onclicklistener so that when I click within the edittext element it will clear its current contents. Is there something wrong here? When I compile this code I get a force quit and ActivityManager: Can't dispatch DDM chunk 4d505251: no handler defined error.

public class Project extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    EditText editText = (EditText)findViewById(R.id.editText1);
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


        editText.setOnClickListener(this);

        setContentView(R.layout.main);

    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        editText.setText("");
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

我为君王 2024-10-29 03:32:14

您也可以使用下面的代码

editText.getText().clear();

Also you can use code below

editText.getText().clear();
你对谁都笑 2024-10-29 03:32:14

首先,您需要调用 setContentView(R.layout.main) 然后进行所有其他初始化。

请尝试下面的代码。

public class Trackfolio extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.editText1);
        editText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        editText.getText().clear(); //or you can use editText.setText("");
    }
}

First you need to call setContentView(R.layout.main) then all other initialization.

Please try below Code.

public class Trackfolio extends Activity implements OnClickListener {
    /** Called when the activity is first created. */
    public EditText editText;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.main);

        editText = (EditText) findViewById(R.id.editText1);
        editText.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        editText.getText().clear(); //or you can use editText.setText("");
    }
}
南烟 2024-10-29 03:32:14

只需在 EditText 中使用 android:hint 属性即可。当该框为空且未获得焦点时,此文本会显示,但在选择 EditText 框后会消失。

just use the android:hint attribute in your EditText. This text shows up when the box is empty and not focused, but disappears upon selecting the EditText box.

清晨说晚安 2024-10-29 03:32:14

我们可以通过两种方式清除 EditText 数据

第一个设置 EditText 为空,如下所示

editext.setText("");

第二个清除 EditText 数据,如下所示

editText.getText().clear();

我建议第二种方式

We can clear EditText data in two ways

First One setting EditText is empty like below line

editext.setText("");

Second one clearing EditText data like this

editText.getText().clear();

I suggest second way

草莓味的萝莉 2024-10-29 03:32:14

你的代码应该是:

    public class Project extends Activity implements OnClickListener {
            /** Called when the activity is first created. */
            EditText editText;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);            

                setContentView(R.layout.main);
                editText = (EditText)findViewById(R.id.editText1);
                editText.setOnClickListener(this);            
            }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(v == editText) {
                    editText.setText("");
                }
            }
        }

Your code should be:

    public class Project extends Activity implements OnClickListener {
            /** Called when the activity is first created. */
            EditText editText;
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);            

                setContentView(R.layout.main);
                editText = (EditText)findViewById(R.id.editText1);
                editText.setOnClickListener(this);            
            }

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub

                if(v == editText) {
                    editText.setText("");
                }
            }
        }
抚你发端 2024-10-29 03:32:14

对于 Kotlin:

创建两个扩展,一个用于 EditText,一个用于 TextView

EditText:

fun EditText.clear() { text.clear() }

TextView:

fun TextView.clear() { text = "" }

并像这样使用它

myEditText.clear()

myTextView.clear()

For Kotlin:

Create two extensions, one for EditText and one for TextView

EditText:

fun EditText.clear() { text.clear() }

TextView:

fun TextView.clear() { text = "" }

and use it like

myEditText.clear()

myTextView.clear()
挽清梦 2024-10-29 03:32:14
public EditText editField;
public Button clear = null;
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.text_layout);
   this. editField = (EditText)findViewById(R.id.userName);
this.clear = (Button) findViewById(R.id.clear_button);  
this.editField.setOnClickListener(this);
this.clear.setOnClickListener(this);
@Override
public void onClick(View v) {

    // TODO Auto-generated method stub
if(v.getId()==R.id.clear_button){
//setText will remove all text that is written by someone
    editField.setText("");
    }
}
public EditText editField;
public Button clear = null;
@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
    setContentView(R.layout.text_layout);
   this. editField = (EditText)findViewById(R.id.userName);
this.clear = (Button) findViewById(R.id.clear_button);  
this.editField.setOnClickListener(this);
this.clear.setOnClickListener(this);
@Override
public void onClick(View v) {

    // TODO Auto-generated method stub
if(v.getId()==R.id.clear_button){
//setText will remove all text that is written by someone
    editField.setText("");
    }
}
尾戒 2024-10-29 03:32:14

清除 editText 值非常简单。当您单击按钮时,只需遵循 1 行代码。

内部按钮或任何你想要的地方。仅使用这个

editText.setText("");   

Very Simple to clear editText values.when u click button then only follow 1 line code.

Inside button or anywhere u want.Only use this

editText.setText("");   
温柔少女心 2024-10-29 03:32:14
package com.example.sampleproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class SampleProject extends Activity {
    EditText mSearchpeople;
    Button mCancel , msearchclose;
    ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        mSearchpeople = (EditText)findViewById(R.id.editText1);
        mCancel = (Button)findViewById(R.id.button2);
        msearchclose = (Button)findViewById(R.id.button1);
        mprofile = (ImageView)findViewById(R.id.imageView1);
        mContact = (ImageView)findViewById(R.id.imageView2);
        mcalender = (ImageView)findViewById(R.id.imageView3);
        mConnection = (ImageView)findViewById(R.id.imageView4);
        mGroup = (ImageView)findViewById(R.id.imageView5);
        mFollowup = (ImageView)findViewById(R.id.imageView6);
        msetting = (ImageView)findViewById(R.id.imageView7);
        mAddacard = (ImageView)findViewById(R.id.imageView8);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mSearchpeople.clearFocus();

            }
        });
    }


}
package com.example.sampleproject;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class SampleProject extends Activity {
    EditText mSearchpeople;
    Button mCancel , msearchclose;
    ImageView mprofile, mContact, mcalender, mConnection, mGroup , mFollowup , msetting , mAddacard;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dashboard);
        mSearchpeople = (EditText)findViewById(R.id.editText1);
        mCancel = (Button)findViewById(R.id.button2);
        msearchclose = (Button)findViewById(R.id.button1);
        mprofile = (ImageView)findViewById(R.id.imageView1);
        mContact = (ImageView)findViewById(R.id.imageView2);
        mcalender = (ImageView)findViewById(R.id.imageView3);
        mConnection = (ImageView)findViewById(R.id.imageView4);
        mGroup = (ImageView)findViewById(R.id.imageView5);
        mFollowup = (ImageView)findViewById(R.id.imageView6);
        msetting = (ImageView)findViewById(R.id.imageView7);
        mAddacard = (ImageView)findViewById(R.id.imageView8);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        mCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mSearchpeople.clearFocus();

            }
        });
    }


}
情丝乱 2024-10-29 03:32:14

我不知道在实施上述解决方案时犯了什么错误,但它们对我来说并不成功

txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override       
    public void onFocusChange(View v, boolean hasFocus) {
        txtDeck.setText("");
    }
});

这对我有用,

i don't know what mistakes i did while implementing the above solutions, bt they were unsuccessful for me

txtDeck.setOnFocusChangeListener(new OnFocusChangeListener() {
    @Override       
    public void onFocusChange(View v, boolean hasFocus) {
        txtDeck.setText("");
    }
});

This works for me,

羁绊已千年 2024-10-29 03:32:14

//点击清除按钮时清除

firstName = (EditText) findViewById(R.id.firstName);

    clear = (Button) findViewById(R.id.clearsearchSubmit);

    clear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() == R.id.clearsearchSubmit);
            firstName.setText("");
        }
    });

这将有助于清除您输入的错误关键字,因此您不必一次又一次地按退格键,只需单击按钮即可清除所有内容。这对我有用。希望有帮助

//To clear When Clear Button is Clicked

firstName = (EditText) findViewById(R.id.firstName);

    clear = (Button) findViewById(R.id.clearsearchSubmit);

    clear.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            if (v.getId() == R.id.clearsearchSubmit);
            firstName.setText("");
        }
    });

This will help to clear the wrong keywords that you have typed in so instead of pressing backspace again and again you can simply click the button to clear everything.It Worked For me. Hope It Helps

凉世弥音 2024-10-29 03:32:14
final EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
childItem.setHint(cellData);

childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //Log.d("NNN", "Has focus " + hasFocus);
        if (hasFocus) {
            Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(ctx.getApplicationContext(),
            "loss the focus", Toast.LENGTH_SHORT).show();
        }
        return;
    });
final EditText childItem = (EditText) convertView.findViewById(R.id.child_item);
childItem.setHint(cellData);

childItem.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        //Log.d("NNN", "Has focus " + hasFocus);
        if (hasFocus) {
            Toast.makeText(ctx.getApplicationContext(), "got the focus", Toast.LENGTH_LONG).show();
        } else {
            Toast.makeText(ctx.getApplicationContext(),
            "loss the focus", Toast.LENGTH_SHORT).show();
        }
        return;
    });
堇年纸鸢 2024-10-29 03:32:14

通过设置空字符串,您可以清除编辑文本

    editext.setText("");

by setting Empty string you can clear your edittext

    editext.setText("");
黎歌 2024-10-29 03:32:14

如果不强制使用 EditText,您可以使用新的材质组件轻松实现此行为:

<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_field"
            app:endIconDrawable="@drawable/ic_close_black_24dp"
            app:endIconMode="clear_text"
            app:endIconTint="@color/black">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_value"
                android:maxLines="1"
                android:text="@{itemModel.value}" />

        </com.google.android.material.textfield.TextInputLayout>

您只需为将清除文本的按钮指定所需的可绘制对象以及它将执行的操作。要清除文本,您可以使用 iconMode="clear_text",但也可以使用“password_toggle”。

If the use of EditText is not mandatory, you can implement this behavior easily with the new material components:

<com.google.android.material.textfield.TextInputLayout
            style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.Dense"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="@string/hint_field"
            app:endIconDrawable="@drawable/ic_close_black_24dp"
            app:endIconMode="clear_text"
            app:endIconTint="@color/black">

            <com.google.android.material.textfield.TextInputEditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@string/hint_value"
                android:maxLines="1"
                android:text="@{itemModel.value}" />

        </com.google.android.material.textfield.TextInputLayout>

You only have to specify the drawable you want for the button that will clear the text and the action that it will execute. To clear the text, you can use iconMode="clear_text", but also "password_toggle" is available.

泡沫很甜 2024-10-29 03:32:14

在 XML 中,您可以这样写:

    <EditText
        android:id="@+id/txtsearch"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:background="@drawable/roundlayoutbutton1"
        android:ems="10"
        android:gravity="center"
        android:inputType="text"
        android:textAllCaps="true"
        android:text="search_xxxx"

        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible" />

在 java 类中,您可能有以下一种:

    EditText searchHost;

OnCreate() 您可以这样写:

    searchHost=findViewById(R.id.txtsearch);

    searchHost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(searchHost.getText().toString().equalsIgnoreCase("search_xxxx")){
                searchHost.setText("");
                Toast.makeText(getApplicationContext(),"Enter you text xxx...", Toast.LENGTH_LONG).show();
            }
        }
    });

它对我来说效果很好。

In XML you can write like:

    <EditText
        android:id="@+id/txtsearch"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:layout_weight="1"
        android:background="@drawable/roundlayoutbutton1"
        android:ems="10"
        android:gravity="center"
        android:inputType="text"
        android:textAllCaps="true"
        android:text="search_xxxx"

        android:textColor="@color/colorPrimaryDark"
        android:visibility="visible" />

and in java class you may have below one :

    EditText searchHost;

OnCreate() you write:

    searchHost=findViewById(R.id.txtsearch);

    searchHost.setOnFocusChangeListener(new View.OnFocusChangeListener() {
        @Override
        public void onFocusChange(View v, boolean hasFocus) {
            if(searchHost.getText().toString().equalsIgnoreCase("search_xxxx")){
                searchHost.setText("");
                Toast.makeText(getApplicationContext(),"Enter you text xxx...", Toast.LENGTH_LONG).show();
            }
        }
    });

It works fine for me.

绿萝 2024-10-29 03:32:14

您也可以在代码中使用 EditText 中的“android:hint”属性:

editText.setHint(CharSequence hint / int resid);

那么您不需要任何 onClickListener 或类似的。但请考虑提示值不会被传递。 editText 将保持为空。在这种情况下,您可以使用默认值设置 editText:

if(editText.getText().toString().equals("")) { 
...use your default value instead of the editText... }

You can use the 'android:hint' attribute in your EditText also from code:

editText.setHint(CharSequence hint / int resid);

Then you don't need any onClickListener or similar. But consider that the hint value won't be passed. The editText will be stayed empty. In this case you can set your editText with your deflault value:

if(editText.getText().toString().equals("")) { 
...use your default value instead of the editText... }
优雅的叶子 2024-10-29 03:32:14

很简单:在类中声明小部件变量(editTexttextViewbutton 等),但在 onCreate 中对其进行初始化> 在 setContentView 之后。

问题是,当您尝试首先访问布局的小部件时,您必须声明布局。声明布局是setContentView
当您通过 findViewById 初始化小部件变量时,您将在 setContentView 的主布局中访问小部件的 id

我希望你能明白!

It's simple: declare the widget variables (editText, textView, button etc.) in class but initialize it in onCreate after setContentView.

The problem is when you try to access a widget of a layout first you have to declare the layout. Declaring the layout is setContentView.
And when you initialize the widget variable via findViewById you are accessing the id of the widget in the main layout in the setContentView.

I hope you get it!

晒暮凉 2024-10-29 03:32:14

我不确定您是否正在寻找这个

    {
    <EditText
     .
     . 
     android:hint="Please enter your name here">
    }

I am not sure if your searching for this one

    {
    <EditText
     .
     . 
     android:hint="Please enter your name here">
    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文