How to use OnClick in android programming [closed]

2019-09-30 17:39发布

问题:

I am completely new to programming and I am trying to create an android app. At the moment I am trying to understand how to use OnClick() to make it so my button on MainActivity links to another activity.

回答1:

For doing some Action on Button Click following these Steps :

STEP 1:

Add a button in your Activity layout as:

<Button
    android:id="@+id/button_id_here"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>


STEP 2:

Add your NextActivity in AndroidManifest.xml as:

 <!-- your other xml  -->
 <application
  <!-- your other xml  -->
        <activity
            android:name=".NextActivity" />

 </application>


STEP 3:

In MainActivity code add a Button click Listener to button_id_here as:

public class MainActivity extends Activity {

    Button button_test;  //<< Create Button instance here
    Intent intent;       //<< For starting new Activity
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);

            // Add layout to Activity here
        setContentView(R.layout.your_Activity_layout);

         // Initilie button here
         button_test= (Button) findViewById(R.id.button_id_here);

               // add a onclick listner to button here
         button_test.setOnClickListener(new View.OnClickListener() {
         public void onClick(View v) {
              intent = new Intent(MainActivity.this,NextActivity.class);

              startActivity(intent); //<<< start Activity here
            }
        });
     }

}

if still facing to do some activity on Button click then learn here:

https://developer.android.com/training/index.html



回答2:

You can do this:

@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login_page);
        String info = "Extra info";
        btn= findViewById(R.id.btn);


        btn.setOnClickListener (new onClickListener(){
             Intent i = new Intent(this, NextActivity.class);
             //If you need to pass any information
             i.putExtra("Info", info);
             startActivity(i);

    });
}


回答3:

If you want to link to another actvity, on click of a button, then you first need to set a listener that listens for the button's click. then you need to override the onClick() function to implement the functionality that you need on that particular button's click.

Here is a snippet of code, that could let you know how it works.

Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        Toast.makeText(getApplicationContext(), "Send Button Clicked", 5000).show();
        Intent i = new Intent(CurrentActivity.this, NextActivity.class);
        i.putExtra("Data", data); //It is for any data you want to send to next activity
        startActivity(i);
            }
        });