how to add button click event in android studio

前端 未结 14 1260
面向向阳花
面向向阳花 2020-12-24 11:04

So I have done some research, and after defining you button as an object by the code

private Button buttonname;
buttonname = (Button) findViewById(R.id.butto         


        
14条回答
  •  醉梦人生
    2020-12-24 11:19

    package com.mani.helloworldapplication;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.support.design.widget.FloatingActionButton;
    import android.support.design.widget.Snackbar;
    import android.support.v7.app.AppCompatActivity;
    import android.support.v7.widget.Toolbar;
    import android.view.View;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.widget.Button;
    import android.widget.TextView;
    import android.widget.Toast;
    
    public class MainActivity extends Activity implements View.OnClickListener {
        //Declaration
        TextView tvName;
        Button btnShow;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
    
            //Empty Window
            super.onCreate(savedInstanceState);
            //Load XML File
            setContentView(R.layout.activity_main);
    
            //Intilization
            tvName = (TextView) findViewById(R.id.tvName);
            btnShow = (Button) findViewById(R.id.btnShow);
    
            btnShow.setOnClickListener(this);
    
        }
    
        @Override
        public void onClick(View v)
        {
            String name = tvName.getText().toString();
            Toast.makeText(MainActivity.this,name,Toast.LENGTH_SHORT).show();
        }
    }
    

提交回复
热议问题