How to put wipe button on android

Asked

Viewed 3,096 times

3

I would like to put a clean button on my calculator app, I have put the Button in xml with id Clear and would like to know how I put in MainActivity.java, follows the code:

public  class MainActivity extends AppCompatActivity implements View.OnClickListener{


    private EditText A1,B1,C1,K1,A2,B2,C2,K2,A3,B3,C3,K3;
    private Button button,Limpar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        A1 = (EditText) findViewById(R.id.A1);
        B1 = (EditText) findViewById(R.id.B1);
        C1 = (EditText) findViewById(R.id.C1);
        K1 = (EditText) findViewById(R.id.K1);
        A2 = (EditText) findViewById(R.id.A2);
        B2 = (EditText) findViewById(R.id.B2);
        C2 = (EditText) findViewById(R.id.C2);
        K2 = (EditText) findViewById(R.id.K2);
        A3 = (EditText) findViewById(R.id.A3);
        B3 = (EditText) findViewById(R.id.B3);
        C3 = (EditText) findViewById(R.id.C3);
        K3 = (EditText) findViewById(R.id.K3);
        Button button = (Button) findViewById(R.id.button);
        Button limpar = (Button) findViewById(R.id.Limpar);


        button.setOnClickListener(this);

    }

    public void onClick (View v){

        String VA1 = A1.getText().toString();
        String VB1 = B1.getText().toString();
        String VC1 = C1.getText().toString();
        String VK1 = K1.getText().toString();
        String VA2 = A2.getText().toString();
        String VB2 = B2.getText().toString();
        String VC2 = C2.getText().toString();
        String VK2 = K2.getText().toString();
        String VA3 = A3.getText().toString();
        String VB3 = B3.getText().toString();
        String VC3 = C3.getText().toString();
        String VK3 = K3.getText().toString();


        if(VA1.trim().isEmpty() || VA2.trim().isEmpty()  || VA3.trim().isEmpty() ||
           VB1.trim().isEmpty() || VB2.trim().isEmpty() || VB3.trim().isEmpty() ||
           VC1.trim().isEmpty() || VC2.trim().isEmpty() || VC3.trim().isEmpty() ||
           VK1.trim().isEmpty() || VK2.trim().isEmpty() || VK3.trim().isEmpty() ){

            AlertDialog.Builder dlg = new AlertDialog.Builder(this);



            dlg.setMessage(" Erro: \n\n Por favor Digite todos os numeros.");
            dlg.setNeutralButton("ok", null);
            dlg.show();







        }else{




        double valorA1= Double.parseDouble( A1.getText().toString());
        double valorB1= Double.parseDouble( B1.getText().toString());
        double valorC1= Double.parseDouble( C1.getText().toString());
        double valorK1= Double.parseDouble( K1.getText().toString());
        double valorA2= Double.parseDouble( A2.getText().toString());
        double valorB2= Double.parseDouble( B2.getText().toString());
        double valorC2= Double.parseDouble( C2.getText().toString());
        double valorK2= Double.parseDouble( K2.getText().toString());
        double valorA3= Double.parseDouble( A3.getText().toString());
        double valorB3= Double.parseDouble( B3.getText().toString());
        double valorC3= Double.parseDouble( C3.getText().toString());
        double valorK3= Double.parseDouble( K3.getText().toString());
        double D1 = valorA2/valorA1;
        double D2 = valorA3/valorA1;
        double L2A = valorA1 * D1 - valorA2;
        double L2B = valorB1 * D1 - valorB2;
        double L2C = valorC1 * D1 - valorC2;
        double L2K = valorK1 * D1 - valorK2;
        double D3A = valorA1 * D2 - valorA3;
        double D3B = valorB1 * D2 - valorB3;
        double D3C = valorC1 * D2 - valorC3;
        double D3K = valorK1 * D2 - valorK3;
        double D3 = D3B / L2B ;
        double L3A = L2A * D3 - D3A;
        double L3B = L2B * D3 - D3B;
        double L3C = L2C * D3 - D3C;
        double L3K = L2K * D3 - D3K;
        double RZ = L3K / L3C ;
        double RY = (L2K - L2C * RZ)/L2B;
        double RX = (valorK1 - RY * valorB1 - RZ * valorC1)/valorA1;









        AlertDialog.Builder dlg = new AlertDialog.Builder(this);



        dlg.setMessage("Calculos:\n\n" +
                "L1 vezes "+D1+" menos a L2.\n " +
                "L1 vezes "+D2+" menos a L3\n\n"+
                "Temos:\n\n"+
                valorA1+"X+"+valorB1+"Y+"+valorC1+"Z="+valorK1+"\n"+
                L2A+"X+"+L2B+"Y+"+L2C+"Z="+L2K+"\n"+
                D3A+"X+"+D3B+"Z+"+D3C+"Z="+D3K+"\n\n"+
                "Por fim L2 vezes "+D3+" menos a L3\n\n"+
                "Equações Escalonadas:\n\n"+
                valorA1+"X+"+valorB1+"Y+"+valorC1+"Z="+valorK1+"\n"+
                L2A+"X+"+L2B+"Y+"+L2C+"Z="+L2K+"\n"+
                L3A+"X+"+L3B+"Z+"+L3C+"Z="+L3K+"\n\n"+
                "Valores de X,Y,Z:\n\n"+
                "Valor de X = "+RX+"\n"+
                "valor de Y = "+RY+"\n"+
                "Valor de Z = "+RZ+"\n");
        dlg.setNeutralButton("ok",null);
        dlg.show();}






    }
}

Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:weightSum="1"
    android:orientation="horizontal">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/Linha1"
        android:id="@+id/textView"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="0dp"
        android:layout_alignParentTop="true"
        android:layout_marginTop="20dp" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/A1"
        android:layout_marginLeft="35dp"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/textView"
        android:layout_toEndOf="@+id/textView"
        android:layout_marginStart="35dp" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/B1"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/A1"
        android:layout_toEndOf="@+id/A1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/C1"
        android:layout_alignParentTop="true"
        android:layout_toRightOf="@+id/B1"
        android:layout_toEndOf="@+id/B1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/K1"
        android:accessibilityLiveRegion="polite"
        android:layout_alignBottom="@+id/textView"
        android:layout_toRightOf="@+id/C1"
        android:layout_toEndOf="@+id/C1"
        android:layout_marginLeft="40dp"
        android:layout_marginStart="40dp" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/Linha2"
        android:id="@+id/textView2"
        android:layout_marginTop="41dp"
        android:layout_below="@+id/A1"
        android:layout_toLeftOf="@+id/A1"
        android:layout_toStartOf="@+id/A1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/A2"
        android:layout_alignBottom="@+id/textView2"
        android:layout_toLeftOf="@+id/B2"
        android:layout_toStartOf="@+id/B2" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/B2"
        android:accessibilityLiveRegion="polite"
        android:layout_alignTop="@+id/C2"
        android:layout_toLeftOf="@+id/C2"
        android:layout_toStartOf="@+id/C2" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/C2"
        android:layout_alignTop="@+id/K2"
        android:layout_toLeftOf="@+id/K1"
        android:layout_toStartOf="@+id/K1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/K2"
        android:layout_alignTop="@+id/A2"
        android:layout_alignLeft="@+id/K1"
        android:layout_alignStart="@+id/K1" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="@string/Linha3"
        android:id="@+id/textView3"
        android:layout_marginTop="37dp"
        android:layout_below="@+id/A2"
        android:layout_toLeftOf="@+id/A1"
        android:layout_toStartOf="@+id/A1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/A3"
        android:layout_alignBottom="@+id/textView3"
        android:layout_toLeftOf="@+id/B3"
        android:layout_toStartOf="@+id/B3" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/B3"
        android:layout_alignTop="@+id/C3"
        android:layout_toLeftOf="@+id/C3"
        android:layout_toStartOf="@+id/C3" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/C3"
        android:layout_alignBottom="@+id/A3"
        android:layout_toLeftOf="@+id/K1"
        android:layout_toStartOf="@+id/K1" />

    <EditText
        android:layout_width="46dp"
        android:layout_height="wrap_content"
        android:inputType="number|numberSigned|numberDecimal"
        android:ems="10"
        android:id="@+id/K3"
        android:layout_alignBottom="@+id/C3"
        android:layout_alignLeft="@+id/K2"
        android:layout_alignStart="@+id/K2" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button"
        android:id="@+id/button"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Limpar"
        android:id="@+id/Limpar"
        android:layout_below="@+id/button"
        android:layout_centerHorizontal="true" />

</RelativeLayout>
  • onclick is linked to the button?

  • this is my first app on android and n I’m sure but I think yes pq when I click the button it scale and shows the Alert

  • You want to clear all the fields editText correct?

  • yes when I click on Clear

2 answers

4


First in xml add event to button

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Limpar"
    android:id="@+id/Limpar"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:onClick="LimparCampos" <!-- troque pelo nome do evento-->
/>

In your Mainactivity you generate a Method with the same name placed in xml containing a View parameter.

public void LimparCampos(View view)
{
    //seu codigo para limpar os editText aqui
}
  • Will conflict with the onclick no? His class is implementing the onclicklisterner interface.

  • I didn’t see anything in your code that might create a conflict. You can add the onClickListener event directly into Mainactivity with Java or directly into the xml. Both ways work.

0

You can do it programmatically like this:

Button limpar = (Button) findViewById(R.id.Limpar);

limpar.setOnClickListener(new View.OnClickListener() 
{
    @Override
    public void onClick(View v)
    {
        limpar(v);
    }
});

public void limpar(View view)
{
    //código para limpar aqui
}

Of course you can also do as Christian said, through XML:

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Limpar"
    android:id="@+id/Limpar"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:onClick="limpar"  <!-- nome da função no Java -->
/>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.