简体   繁体   中英

My button is not working, how can i make it work on android studio?

I made a button on my android studio project and I'm pretty sure the code is just fine. the problem is that the button does not respond - I click it, but it doesn't give me the wanted result. If you could help, please do. thank you so much for your time!

this is the code:

XML file code:

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/Bdateofbirth"
android:text="Date of birth"
android:gravity="center"
android:layout_gravity="center"/>

onClick Code:

    @Override
public void onClick(View view) {
    if (view == Bdateofbirth) {
        dialogDOB();
    }

}

dialogDOB:

private void dialogDOB(){

    Calendar calendar = Calendar.getInstance();
    int d = calendar.get(Calendar.DAY_OF_MONTH);
    int m = calendar.get(Calendar.MONTH);
    int y = calendar.get(Calendar.YEAR);
    DatePickerDialog datePickerDialog = new DatePickerDialog(signup.this, new DatePickerDialog.OnDateSetListener() {
        @Override
        public void onDateSet(DatePicker datePicker, int i, int i1, int i2) {
            i1=i1+1;
            String date = i + "/" + i1 + "/" + i2;
            Toast.makeText(signup.this, date, Toast.LENGTH_SHORT).show();
            Bdateofbirth.setText(date);
        }
    }, d, m, y);
    datePickerDialog.show();
}

THANK YOU

In your Activity's XML file, add the attribute android:onclick="onClick" for your button to connect it to your onClick method. You don't need the @override .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM