Android : How to add a reset button to clear numbers on Edittext field

on Saturday, August 9, 2014


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.)


Cheers! Thanks!


ActivityMain XML file!



<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);
}
}

0 comments:

Post a Comment