how to add button click event in android studio

前端 未结 14 1358
面向向阳花
面向向阳花 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: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());
        }
    }
    

提交回复
热议问题