how to add button click event in android studio

前端 未结 14 1263
面向向阳花
面向向阳花 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:23

    Start your OnClickListener, but when you get to the first set up parenthesis, type new, then View, and press enter. Should look like this when you're done:

    Button btn1 = (Button)findViewById(R.id.button1);
    
    btn1.setOnClickListener(new View.OnClickListener() {            
        @Override
        public void onClick(View v) {
    //your stuff here.
        }
    });
    
    0 讨论(0)
  • 2020-12-24 11:24

    //as I understand it, the "this" denotes the current view(focus) in the android program

    No, "this" will only work if your MainActivity referenced by this implements the View.OnClickListener, which is the parameter type for the setOnClickListener() method. It means that you should implement View.OnClickListener in MainActivity.

    0 讨论(0)
  • 2020-12-24 11:24
    public class EditProfile extends AppCompatActivity {
    
        Button searchBtn;
        EditText userName_editText;
        EditText password_editText;
        EditText dob_editText;
        RadioGroup genderRadioGroup;
        RadioButton genderRadioBtn;
        Button editBtn;
        Button deleteBtn;
        Intent intent;
        DBHandler dbHandler;
    
        public static final String USERID_EDITPROFILE = "userID";
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_edit_profile);
    
            searchBtn = (Button)findViewById(R.id.editprof_searchbtn);
            userName_editText = (EditText)findViewById(R.id.editprof_userName);
            password_editText = (EditText)findViewById(R.id.editprof_password);
            dob_editText = (EditText)findViewById(R.id.editprof_dob);
            genderRadioGroup = (RadioGroup)findViewById(R.id.editprof_radiogroup);
            editBtn = (Button)findViewById(R.id.editprof_editbtn);
            deleteBtn = (Button)findViewById(R.id.editprof_deletebtn);
            intent = getIntent();
    
    
            dbHandler = new DBHandler(EditProfile.this);
    
            setUserDetails();
    
            deleteBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    String username = userName_editText.getText().toString();
    
                    if(username == null){
                        Toast.makeText(EditProfile.this,"Please enter username to delete your profile",Toast.LENGTH_SHORT).show();
                    }
                    else{
                        UserProfile.Users users = dbHandler.readAllInfor(username);
    
                        if(users == null){
                            Toast.makeText(EditProfile.this,"No profile found from this username, please enter valid username",Toast.LENGTH_SHORT).show();
                        }
                        else{
                            dbHandler.deleteInfo(username);
                            Intent redirectintent_home = new Intent("com.modelpaper.mad.it17121002.Home");
                            startActivity(redirectintent_home);
                        }
                    }
                }
            });
    
            editBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
    
                    String userID_String = intent.getStringExtra(Home.USERID);
                    if(userID_String == null){
                        Toast.makeText(EditProfile.this,"Error!!",Toast.LENGTH_SHORT).show();
                        Intent redirectintent_home =  new Intent(getApplicationContext(),Home.class);
                        startActivity(redirectintent_home);
                    }
                    int userID = Integer.parseInt(userID_String);
    
                    String username = userName_editText.getText().toString();
                    String password = password_editText.getText().toString();
                    String dob = dob_editText.getText().toString();
                    int selectedGender = genderRadioGroup.getCheckedRadioButtonId();
                    genderRadioBtn = (RadioButton)findViewById(selectedGender);
                    String gender = genderRadioBtn.getText().toString();
    
                    UserProfile.Users users = UserProfile.getProfile().getUser();
                    users.setUsername(username);
                    users.setPassword(password);
                    users.setDob(dob);
                    users.setGender(gender);
                    users.setId(userID);
    
                    dbHandler.updateInfor(users);
                    Toast.makeText(EditProfile.this,"Updated Successfully",Toast.LENGTH_SHORT).show();
                    Intent redirectintent_home = new Intent(getApplicationContext(),Home.class);
                    startActivity(redirectintent_home);
                }
            });
    
            searchBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   String username = userName_editText.getText().toString();
                   if (username == null){
                       Toast.makeText(EditProfile.this,"Please enter a username",Toast.LENGTH_SHORT).show();
                   }
                   else{
                       UserProfile.Users users_search = dbHandler.readAllInfor(username);
    
    
                       if(users_search == null){
                           Toast.makeText(EditProfile.this,"Please enter a valid username",Toast.LENGTH_SHORT).show();
                       }
                       else{
                           userName_editText.setText(users_search.getUsername());
                           password_editText.setText(users_search.getPassword());
                           dob_editText.setText(users_search.getDob());
                           int id = users_search.getId();
                           Intent redirectintent = new Intent("com.modelpaper.mad.it17121002.EditProfile");
                           redirectintent.putExtra(USERID_EDITPROFILE,Integer.toString(id));
                           startActivity(redirectintent);
                       }
                   }
                }
            });
    
    
        }
    
        public void setUserDetails(){
    
            String userID_String = intent.getStringExtra(Home.USERID);
            if(userID_String == null){
                Toast.makeText(EditProfile.this,"Error!!",Toast.LENGTH_SHORT).show();
                Intent redirectintent_home = new Intent("com.modelpaper.mad.it17121002.Home");
                startActivity(redirectintent_home);
            }
            int userID = Integer.parseInt(userID_String);
            UserProfile.Users users = dbHandler.readAllInfor(userID);
            userName_editText.setText(users.getUsername());
            password_editText.setText(users.getPassword());
            dob_editText.setText(users.getDob());
        }
    }
    
    0 讨论(0)
  • 2020-12-24 11:26

    Your Activity must implement View.OnClickListener, like this:

    public class MainActivity extends 
    Activity implements View.OnClickListener{
    // YOUR CODE
    }
    

    And inside MainActivity override the method onClick(), like this:

    @override
    public void onClick (View view){
    //here YOUR Action response to Click Button 
    }
    
    0 讨论(0)
  • 2020-12-24 11:26

    Different ways to handle button event

    Button btn1 = (Button)findViewById(R.id.button1);
    btn1.setOnClickListener(new View.OnClickListener() {            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Toast.makeText(context, "Button 1", 
         Toast.LENGTH_LONG).show();
            }
        });
    

    [Check this article for more details about button event handlers]

    0 讨论(0)
  • 2020-12-24 11:30

    When you define an OnClickListener (or any listener) this way

    btnClick.setOnClickListener(this);
    

    you need to implement the OnClickListener in your Activity.

    public class MainActivity extends ActionBarActivity implements OnClickListener{
    
    0 讨论(0)
提交回复
热议问题