[英]How to add a reset button to clear numbers on Edittext field
I want to add a button to clear the numbers on textfield by tapping on the button.我想通过点击按钮添加一个按钮来清除文本字段上的数字。
(Reset my EditText back to an empty "space" after a button has pressed that would have completed an activity with input from the EditText field.) (在按下按钮后,将我的 EditText 重置为空白的“空格”,该按钮将通过 EditText 字段的输入完成活动。)
Cheers!干杯! Thanks!
谢谢!
ActivityMain XML file! ActivityMain XML 文件!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/back"
android:orientation="vertical"
tools:context="com.miuapps.unitconverter.Centimetres_Metres" >
<TextView
android:id="@+id/textView1" //first textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter a Value(cm) :"
android:layout_gravity="center"
android:gravity="center"
android:textSize="30dp"/>
<EditText
android:id="@+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Convert" />
<TextView
android:id="@+id/tvDisplay1" //output textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Metres: 0"
android:textSize="30dp" />
</LinearLayout>
double counter, counter2 = 100;
double x;
Button conv;
TextView dis1, dis2;
EditText ent1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
conv = (Button)findViewById(R.id.button1);
dis1 = (TextView)findViewById(R.id.tvDisplay1);
//dis2 = (TextView)findViewById(R.id.tvDisplay2);
ent1 = (EditText)findViewById(R.id.editText1);
conv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
x = new Integer(ent1.getText().toString());
counter = x/100;
//counter2++;
dis1.setText("In Metres: " +counter);
//dis2.setText("In Centimetres: " +counter2);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How about this:这个怎么样:
ent1.setText("");
just put it at the end of your onClick Method只需将其放在 onClick 方法的末尾
Edit:编辑:
conv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// here you get the text from your editText
x = new Integer(ent1.getText().toString());
// do something with the text
counter = x/100;
// set result in another TextView
dis1.setText("In Metres: " +counter);
// work is done, so the editText can be cleared
ent1.setText("");
}
});
Edit 2 (reset button):编辑 2(重置按钮):
resetButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// reset only by another button
ent1.setText("");
}
});
Simply add a button in your layout.只需在布局中添加一个按钮。
<Button
android:id="@+id/buttonReset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Reset" />
Find that button in your activity:在您的活动中找到该按钮:
Button mButtonReset = (Button) findViewById(R.id.buttonReset);
mButtonReset.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View view){
ent1.setText("");
}
});
Activity_main.xml like below, Activity_main.xml 如下所示,
<EditText
android:id="@+id/txt_1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:minHeight="48dp" />
<Button
android:id="@+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Button" />
<Button
android:id="@+id/btn_cln"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Clear" />
** **
public class MainActivity extends AppCompatActivity {
Button button,button2;
EditText editText;
String name;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn_ok);
editText = findViewById(R.id.txt_1);
button2 = findViewById(R.id.btn_cln);
//create text add button
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
name = editText.getText().toString();
}
});
//clean,reset text button
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setText("");
}
});
}
}
** **
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.