Pass value to a function in java

0

Hello! in Javascript to pass a value to the function I do:

<button onclick="myFunction('valor')">Click me</button>

and redemption like this:

function myFunction(a){alert(a);}

And in java on android, how do I do?

    
asked by anonymous 10.05.2014 / 01:50

3 answers

3

Represents a widget button. Buttons can be pressed or clicked by the user to perform an action. A typical use of a button in activity would be as follows

public class MyActivity extends Activity {
     protected void onCreate(Bundle icicle) {
         super.onCreate(icicle);

         setContentView(R.layout.content_layout_id);

         final Button button = (Button) findViewById(R.id.button_id);
         button.setOnClickListener(new View.OnClickListener() {
             public void onClick(View v) {
                 // Faça algo aqui quando clicar no botão.
             }
         });
     }
 }

However, instead of applying OnClickListener to the button in your activity, you can assign a method to the button in the XML layout, using the android:onClick attribute. For example:

 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

Now when a user clicks the button, the Android system calls the selfDestruct(View) activity method. For this to work, the method must be public and accept View as its only parameter. For example:

 public void selfDestruct(View view) {
     // Faça algo aqui quando clicar no botão.
 }

The View passed to the method is a reference to the widget that was clicked.

Reference here: Official Documentation

    
10.05.2014 / 03:30
0

You'd better put what you need to make it easier on the help ... more would be something + - so ...

public static int soma(int a, int b)  
{  
    int c = a + b;  
    return c;  
} 
    
10.05.2014 / 02:16
0
 <Button
     android:layout_height="wrap_content"
     android:layout_width="wrap_content"
     android:text="@string/self_destruct"
     android:onClick="selfDestruct" />

Taken from the above answer, it's quite simple.

    
13.05.2014 / 14:58