how to define a reset button to clear all text field at one click? [closed]

时光怂恿深爱的人放手 提交于 2020-01-17 04:11:27

问题


<ScrollView
    android:id="@+id/ScrollView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true" >

     <LinearLayout
         android:id="@+id/MainParent"
         android:layout_width="wrap_content"
         android:layout_height="match_parent" >

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="bottom"
        android:layout_marginLeft="20sp"
        android:layout_marginRight="20sp"
        android:orientation="vertical" >

        <TextView
            android:id="@+id/textView1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80sp"
            android:text="Sim Card Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/simno"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:text="Sim No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/simName"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:selectAllOnFocus="true" >
            <requestFocus />
        </EditText>

        <TextView
            android:id="@+id/TextView01"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="Service Provider"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <Spinner
            android:id="@+id/spinner"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:entries="@array/service_arrays"
            android:prompt="@string/serviceprovid" />

        <TextView
            android:id="@+id/TextView02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="100sp"
            android:layout_marginTop="5dp"
            android:text="Unit Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/TextView03"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5sp"
            android:text="Unit No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/unitNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10"
            android:selectAllOnFocus="true" />

        <TextView
            android:id="@+id/TextView04"
            android:layout_width="145dp"
            android:layout_height="wrap_content"
            android:text="IMEI No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/imeiNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10" 
            android:selectAllOnFocus="true"/>

        <TextView
            android:id="@+id/vehicleNo"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="80sp"
            android:layout_marginTop="5dp"
            android:gravity="center"
            android:text="Vehicle Master"
            android:textAppearance="?android:attr/textAppearanceSmall"
            android:textColor="@color/pumpkinorange"
            android:textSize="17sp" />

        <TextView
            android:id="@+id/TextView06"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:text="Vehicle No"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@color/darkblue"
            android:textSize="14sp" />

        <EditText
            android:id="@+id/vehicleNo"
            android:layout_width="275dp"
            android:layout_height="wrap_content"
            android:ems="10" 
            android:selectAllOnFocus="true"/>

        <Button
            android:id="@+id/button1"
            android:layout_width="wrap_content"
            android:layout_height="34dp"
            android:layout_gravity="center"
            android:text="Save"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" />

        <Button
            android:id="@+id/button2"
            style="?android:attr/buttonStyleSmall"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_weight="1"
            android:text="Reset"
            android:textSize="15sp"
            android:textStyle="italic"
            android:typeface="normal" />

    </LinearLayout>

  </LinearLayout>
</ScrollView>

In this xml layout has four text fields and two buttons. while click on the reset button all the four textfields get cleared. How to define code for this in main activity.please Show explain me with code.


回答1:


In your MainActivity, In your onCreate do this

Button reset = (Button)findViewById(R.id.reset);

Similarly Find your all textview here. then,

reset.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        textview1.settext("");
        textview2.settext("");
        ......
        textview8.settext("");
    }
});



回答2:


This is easier. I have a reset button and I reuse this code across different pages all I do is just change the mActivityABC basically calling the same page again as if you had clicked a next button it reloads and all EditText are clear. No arrays no setting of boxes nada!!

reset.setOnClickListener(new View.OnClickListener() {

    @Override
    public void onClick(View v) {
        if (v==reset) {
            startActivity(new Intent(myActivityABC.this, myActivityABC.class));
        }
    }
});



回答3:


btn.setOnClickListener(new OnClickListener(){
    @public void onClick(View view)
    {
        txt1.setText("");
        txt2.setText("");
        ...
        txtN.setText("");
    }
});

Also you can use array for your views.




回答4:


// Onclicklistener on clear button.

btnclear.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    etjobname.setText("");
                    etcategor.setText("");
                    etname.setText("");
                    etaddress.setText("");

                }
            });



回答5:


reset.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        textview1.settext("");
        textview2.settext("");
        ......
        textview8.settext("");
    }
});

In this code please put caps T then it will work ...thanks i have used and tested.



来源:https://stackoverflow.com/questions/18331233/how-to-define-a-reset-button-to-clear-all-text-field-at-one-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!