输入时清除 EditText 中的文本
我正在尝试设置 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(18)
您也可以使用下面的代码
Also you can use code below
首先,您需要调用
setContentView(R.layout.main)
然后进行所有其他初始化。请尝试下面的代码。
First you need to call
setContentView(R.layout.main)
then all other initialization.Please try below Code.
只需在 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.我们可以通过两种方式清除 EditText 数据
第一个设置 EditText 为空,如下所示
第二个清除 EditText 数据,如下所示
我建议第二种方式
We can clear EditText data in two ways
First One setting EditText is empty like below line
Second one clearing EditText data like this
I suggest second way
你的代码应该是:
Your code should be:
对于 Kotlin:
创建两个扩展,一个用于 EditText,一个用于 TextView
EditText:
TextView:
并像这样使用它
For Kotlin:
Create two extensions, one for EditText and one for TextView
EditText:
TextView:
and use it like
清除 editText 值非常简单。当您单击按钮时,只需遵循 1 行代码。
Very Simple to clear editText values.when u click button then only follow 1 line code.
我不知道在实施上述解决方案时犯了什么错误,但它们对我来说并不成功
这对我有用,
i don't know what mistakes i did while implementing the above solutions, bt they were unsuccessful for me
This works for me,
//点击清除按钮时清除
这将有助于清除您输入的错误关键字,因此您不必一次又一次地按退格键,只需单击按钮即可清除所有内容。这对我有用。希望有帮助
//To clear When Clear Button is Clicked
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
通过设置空字符串,您可以清除编辑文本
by setting Empty string you can clear your edittext
如果不强制使用 EditText,您可以使用新的材质组件轻松实现此行为:
您只需为将清除文本的按钮指定所需的可绘制对象以及它将执行的操作。要清除文本,您可以使用 iconMode="clear_text",但也可以使用“password_toggle”。
If the use of EditText is not mandatory, you can implement this behavior easily with the new material components:
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.
在 XML 中,您可以这样写:
在 java 类中,您可能有以下一种:
OnCreate() 您可以这样写:
它对我来说效果很好。
In XML you can write like:
and in java class you may have below one :
OnCreate() you write:
It works fine for me.
您也可以在代码中使用 EditText 中的“android:hint”属性:
那么您不需要任何 onClickListener 或类似的。但请考虑提示值不会被传递。 editText 将保持为空。在这种情况下,您可以使用默认值设置 editText:
You can use the 'android:hint' attribute in your EditText also from code:
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:
很简单:在类中声明小部件变量(
editText
、textView
、button
等),但在onCreate
中对其进行初始化> 在setContentView
之后。问题是,当您尝试首先访问布局的小部件时,您必须声明布局。声明布局是
setContentView
。当您通过
findViewById
初始化小部件变量时,您将在setContentView
的主布局中访问小部件的id
。我希望你能明白!
It's simple: declare the widget variables (
editText
,textView
,button
etc.) in class but initialize it inonCreate
aftersetContentView
.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 theid
of the widget in the main layout in thesetContentView
.I hope you get it!
我不确定您是否正在寻找这个
I am not sure if your searching for this one