I am trying to run a programme in Android to clear all text in the EditText fields when the user presses the clear all button. Below is the code I have so far and at the moment it is not working as planned. I am new to java and adroid app development. Kindly let me know how I can resolve this problem, with examples,if possible. Many thanks in advance.
CEMMainActivity.java
public class CEMMainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) { // runs every time the app runs
super.onCreate(savedInstanceState); // loads saved instant state
setContentView(R.layout.activity_cemmain);//sets what the app will look like based on the graphical design created
if (savedInstanceState == null) {// action if app does not start up correctly
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
// Clears all text in EditText fields
Button clearalltext = (Button) findViewById(R.id.cleartext);
clearalltext.setOnClickListener(new OnClickListener() {
public void onClick(View c) {
ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);
clearText(group);
});
}
private void clearText((ViewGroup)group); {
// TODO Auto-generated method stub
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
ViewGroup view;
if(view instanceof ViewGroup && (((ViewGroup)view).getChildCount() > 0))
clearText((ViewGroup)group);
}
}
}
fragment_cemmain.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="clinical.equipment.monitor.CEMMainActivity$PlaceholderFragment" >
.....
<Button
android:id="@+id/cleartext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/findequip"
android:layout_marginTop="56dp"
android:onClick="cleartext"
android:text="@string/cleartext" />
....
</RelativeLayout>
(ViewGroup) findViewById(R.id.cleartext);
i think cleartext is button not a viewgroup you can use getParent() method of onclick argument parameter c like c.getParent() which return ViewGroup Relative Layout then you can fetch all children which wil define by you in clearText Method ...
or you can give id to relativelayout also
// Clears all text in EditText fields
Button clearalltext = (Button) findViewById(R.id.cleartext);
clearalltext.setOnClickListener(new OnClickListener() {
public void onClick(View c) {
ViewGroup group = (ViewGroup) c.getParent();
clearText(group);
});
}
I think problem with you are passing button id to view group so its child count will be zero.please pass you parent layout id to view group . make changes to below id
ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);
replace cleartext with your parent layout id. ie RelativeLayout id
ViewGroup group = (ViewGroup) findViewById(R.id.cleartext);
It will access button and will only clear text of the button. Assign an id to RelativeLayout and use it to clear text of all the childs.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.