onClick Sign Up method includes onClick Datepicker, how to make it work?

huangapple go评论74阅读模式
英文:

onClick Sign Up method includes onClick Datepicker, how to make it work?

问题

package com.example.sportsbuddyz;

import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;

import java.util.Calendar;
import java.util.HashMap;

public class registerPage extends AppCompatActivity implements View.OnClickListener {

    EditText jEmailEt, jPasswordEt;
    Button jSignUpBtn;
    ProgressDialog progressDialog;
    private FirebaseAuth mAuth;
    TextView jAccountExist;

    EditText jName, jStudentID, jPhoneNumber, jAddress;
    TextView jDOB, jAutoAge;

    DatePickerDialog.OnDateSetListener mDateSetListener;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.registerpage);

        jEmailEt = findViewById(R.id.emailEt);
        jPasswordEt = findViewById(R.id.passwordEt);
        mAuth = FirebaseAuth.getInstance();

        jName = findViewById(R.id.nameEt);
        jStudentID = findViewById(R.id.studentIDEt);
        jPhoneNumber = findViewById(R.id.phoneEt);
        jAddress = findViewById(R.id.addressEt);

        jDOB = findViewById(R.id.DOB);
        jAutoAge = findViewById(R.id.AgeAuto);

        ActionBar actionBar = getSupportActionBar();
        actionBar.setTitle("Sign Up Account");
        actionBar.setDisplayHomeAsUpEnabled(true);
        actionBar.setDisplayShowHomeEnabled(true);

        progressDialog = new ProgressDialog(this);
        progressDialog.setMessage("Progressing User...");

        findViewById(R.id.signupBtn).setOnClickListener(this);
        findViewById(R.id.accountExist).setOnClickListener(this);
        findViewById(R.id.DOB).setOnClickListener(this);
    }

    @Override
    public void onClick(View view){
        switch (view.getId()){
            case R.id.signupBtn:{
                    registerUser();
            }
            break;
            case R.id.accountExist: {
                startActivity(new Intent(registerPage.this, loginPage.class));
                finish();
            }
            break;
        }
    }

    private void registerUser(){
        progressDialog.show();

        String email = jEmailEt.getText().toString().trim();
        String password = jPasswordEt.getText().toString().trim();
        String name = jName.getText().toString().trim();
        String studentID = jStudentID.getText().toString().trim();
        String phoneNo = jPhoneNumber.getText().toString().trim();
        String address = jAddress.getText().toString().trim();

        if (email.isEmpty()) {
            jEmailEt.setError("Email must not be empty");
            jEmailEt.setFocusable(true);
            return;
        }
        if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
            jEmailEt.setError("Invalid Email");
            jEmailEt.setFocusable(true);
            return;
        }
        // ... (similar validation checks for other fields)

        jDOB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Calendar calendar = Calendar.getInstance();
                int year = calendar.get(Calendar.YEAR);
                int month = calendar.get(Calendar.MONTH);
                int day = calendar.get(Calendar.DAY_OF_MONTH);

                DatePickerDialog datePickerDialog = new DatePickerDialog(registerPage.this, android.R.style.Theme_Holo_Dialog_MinWidth, mDateSetListener, year, month, day);
                datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
                datePickerDialog.show();
            }
        });

        mDateSetListener = new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int month, int day) {
                month = month + 1;
                String dobDate = day + "/" + month + "/" + year;
                jDOB.setText(dobDate);

                jAutoAge.setText(getAge(year, month, day));
            }
        };

        mAuth.createUserWithEmailAndPassword(email, password)
                .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                    @Override
                    public void onComplete(@NonNull Task<AuthResult> task) {
                        if (task.isSuccessful()) {
                            progressDialog.dismiss();
                            FirebaseUser user = mAuth.getCurrentUser();
                            // ... (storing user information in Firebase database)
                            Toast.makeText(registerPage.this, "Registered..\n" + user.getEmail(), Toast.LENGTH_SHORT).show();
                            startActivity(new Intent(registerPage.this, dashBoard.class));
                            finish();
                        } else {
                            progressDialog.dismiss();
                            Toast.makeText(registerPage.this, "Authentication failed.",
                                    Toast.LENGTH_SHORT).show();
                        }
                    }
                }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {
                progressDialog.dismiss();
                Toast.makeText(registerPage.this, "" + e.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }

    // ... (getAge function and other methods)
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".registerPage">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">
            
            <!-- View components similar to the original XML layout -->

        </RelativeLayout>

    </ScrollView>

</RelativeLayout>
英文:

Using Firebase to save User's information by Registering/SignUp. Then I met an obstacle....

I'm using switch case for onClick, which one is for registerpage() method which in form of Button & another for going to next activity via textview. But at the registerpage() method, there is a onClick DatePicker (for selecting dateOfBirth) . But the onClick for DatePicker doesn't work. Here's my code. Thank you.

registerPage.java

package com.example.sportsbuddyz;
import android.app.DatePickerDialog;
import android.app.ProgressDialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.media.tv.TvContract;
import android.nfc.Tag;
import android.support.annotation.NonNull;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.util.Patterns;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
public class registerPage extends AppCompatActivity implements View.OnClickListener {
EditText jEmailEt, jPasswordEt;
Button jSignUpBtn;
ProgressDialog progressDialog;
private FirebaseAuth mAuth;
TextView jAccountExist;
EditText jName, jStudentID, jPhoneNumber, jAddress;
TextView jDOB, jAutoAge;
DatePickerDialog.OnDateSetListener mDateSetListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.registerpage);
jEmailEt = findViewById(R.id.emailEt);
jPasswordEt = findViewById(R.id.passwordEt);
//jSignUpBtn = findViewById(R.id.signupBtn);
mAuth = FirebaseAuth.getInstance();
//jAccountExist = findViewById(R.id.accountExist);
jName = findViewById(R.id.nameEt);
jStudentID = findViewById(R.id.studentIDEt);
jPhoneNumber = findViewById(R.id.phoneEt);
jAddress = findViewById(R.id.addressEt);
jDOB = findViewById(R.id.DOB);
jAutoAge = findViewById(R.id.AgeAuto);
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(&quot;Sign Up Account&quot;);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setDisplayShowHomeEnabled(true);
progressDialog = new ProgressDialog(this);
progressDialog.setMessage(&quot;Progressing User...&quot;);
findViewById(R.id.signupBtn).setOnClickListener(this);
findViewById(R.id.accountExist).setOnClickListener(this);
findViewById(R.id.DOB).setOnClickListener(this);
}
@Override
public void onClick(View view){
switch (view.getId()){
//CASE 1 -- SignUp Button
case R.id.signupBtn:{
registerUser();
}
break;
//CASE 2 -- AccountExist(Text)
case R.id.accountExist: {
startActivity(new Intent(registerPage.this, loginPage.class));
finish();
}
break;
}
}
private void registerUser(){
progressDialog.show();
String email = jEmailEt.getText().toString().trim();
String password = jPasswordEt.getText().toString().trim();
String name = jName.getText().toString().trim();
String studentID = jStudentID.getText().toString().trim();
String phoneNo = jPhoneNumber.getText().toString().trim();
String address = jAddress.getText().toString().trim();
if (email.isEmpty()) {
jEmailEt.setError(&quot;Email must not be empty&quot;);
jEmailEt.setFocusable(true);
return;
}
if (!Patterns.EMAIL_ADDRESS.matcher(email).matches()) {
jEmailEt.setError(&quot;Invalid Email&quot;);
jEmailEt.setFocusable(true);
return;
}
if (password.isEmpty()) {
jPasswordEt.setError(&quot;Password must not be empty&quot;);
jPasswordEt.setFocusable(true);
return;
}
if (password.length() &lt; 6) {
jPasswordEt.setError(&quot;Password must be at least 6 digit&quot;);
jPasswordEt.setFocusable(true);
return;
}
if (name.isEmpty()) {
jName.setError(&quot;Name must not be empty&quot;);
jName.setFocusable(true);
return;
}
if (studentID.isEmpty()) {
jStudentID.setError(&quot;Student ID must not be empty&quot;);
jStudentID.setFocusable(true);
return;
}
if (phoneNo.isEmpty()) {
jPhoneNumber.setError(&quot;Phone Number must not be empty&quot;);
jPhoneNumber.setFocusable(true);
return;
}
if (address.isEmpty()) {
jAddress.setError(&quot;Address must not be empty&quot;);
jAddress.setFocusable(true);
return;
}
jDOB.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(registerPage.this, android.R.style.Theme_Holo_Dialog_MinWidth, mDateSetListener, year, month, day);
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
}
});
mDateSetListener = new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
month = month + 1;
String dobDate = day + &quot;/&quot; + month + &quot;/&quot; + year;
jDOB.setText(dobDate);
jAutoAge.setText(getAge(year, month, day));
}
};
mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener&lt;AuthResult&gt;() {
@Override
public void onComplete(@NonNull Task&lt;AuthResult&gt; task) {
if (task.isSuccessful()) {
// Sign in success, dismiss dialog &amp; start register activity
progressDialog.dismiss();
//User userDetail = new User(email, name, studentID, phoneNo, address);
FirebaseUser user = mAuth.getCurrentUser();
//Get User email and UID from auth
String email = user.getEmail();
String uid = user.getUid();
String name = jName.getText().toString().trim();
String studentID = jStudentID.getText().toString().trim();
String phoneNo = jPhoneNumber.getText().toString().trim();
String address = jAddress.getText().toString().trim();
String DOB = jDOB.getText().toString().trim();
String Age = jAutoAge.getText().toString().trim();
//User is registered store user info in Firebase realtime database also
HashMap&lt;Object,String&gt; hashMap = new HashMap&lt;&gt;();
//Put Info in HashMap
hashMap.put(&quot;email&quot;,email);
hashMap.put(&quot;uid&quot;,uid);
hashMap.put(&quot;name&quot;, name);
hashMap.put(&quot;studentID&quot;, studentID);
hashMap.put(&quot;phone&quot;,phoneNo);
hashMap.put(&quot;address&quot;,address);
hashMap.put(&quot;Date Of Birth&quot;, DOB);
hashMap.put(&quot;Age&quot;,Age);
hashMap.put(&quot;image&quot;,&quot;&quot;);
hashMap.put(&quot;cover&quot;,&quot;&quot;);
//Firebase database instance
FirebaseDatabase database = FirebaseDatabase.getInstance();
//path to store user data named &quot;Users&quot;
DatabaseReference reference = database.getReference(&quot;Users&quot;);
//Put data within HashMap in database
reference.child(uid).setValue(hashMap);
Toast.makeText(registerPage.this, &quot;Registered..\n&quot;+user.getEmail(), Toast.LENGTH_SHORT).show();
startActivity(new Intent(registerPage.this, dashBoard.class));
finish();
} else {
// If sign in fails, display a message to the user.
progressDialog.dismiss();
Toast.makeText(registerPage.this, &quot;Authentication failed.&quot;,
Toast.LENGTH_SHORT).show();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
progressDialog.dismiss();
Toast.makeText(registerPage.this, &quot;&quot;+e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
private String getAge(int year, int month, int day){
Calendar dob = Calendar.getInstance();
Calendar today = Calendar.getInstance();
dob.set(year, month, day);
int age = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR);
if (today.get(Calendar.DAY_OF_YEAR) &lt; dob.get(Calendar.DAY_OF_YEAR)){
age--;
}
Integer ageInt = new Integer(age);
String ageS = ageInt.toString();
return ageS;
}
@Override
public boolean onSupportNavigateUp(){
onBackPressed();
return super.onSupportNavigateUp();
}
@Override
public void onPointerCaptureChanged(boolean hasCapture) {
}
}

registerpage.xml

&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;
&lt;RelativeLayout
xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:app=&quot;http://schemas.android.com/apk/res-auto&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
tools:context=&quot;.registerPage&quot;&gt;
&lt;ScrollView
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;RelativeLayout
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;&gt;
&lt;!--Confirm Sign Up Button--&gt;
&lt;Button
android:id=&quot;@+id/signupBtn&quot;
style=&quot;@style/Widget.AppCompat.Button.Small&quot;
android:layout_width=&quot;163dp&quot;
android:layout_height=&quot;47dp&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:background=&quot;@drawable/loginbutton&quot;
android:text=&quot;Sign Up&quot;
android:layout_below=&quot;@id/accountExist&quot;
android:layout_margin=&quot;10dp&quot;
android:textAllCaps=&quot;false&quot;
android:textStyle=&quot;bold&quot; /&gt;
&lt;!--EmailAddress--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/emailTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_marginTop=&quot;120dp&quot;&gt;
&lt;EditText
android:id=&quot;@+id/emailEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Email Address&quot;
android:inputType=&quot;textEmailAddress&quot; /&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--Password--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/passwordTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_below=&quot;@+id/emailTIL&quot;
app:passwordToggleEnabled=&quot;true&quot;&gt;
&lt;EditText
android:id=&quot;@+id/passwordEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Password&quot;
android:inputType=&quot;textPassword&quot; /&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--Name--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/nameTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_below=&quot;@+id/passwordTIL&quot;
app:passwordToggleEnabled=&quot;true&quot;&gt;
&lt;EditText
android:id=&quot;@+id/nameEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Full Name&quot;
android:inputType=&quot;textPersonName&quot; /&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--Student ID--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/studentIDTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_below=&quot;@+id/nameTIL&quot;
app:passwordToggleEnabled=&quot;true&quot;&gt;
&lt;EditText
android:id=&quot;@+id/studentIDEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Student ID&quot; /&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--Phone Number--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/phoneTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_below=&quot;@+id/studentIDTIL&quot;
app:passwordToggleEnabled=&quot;true&quot;&gt;
&lt;EditText
android:id=&quot;@+id/phoneEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Phone No.&quot;
android:inputType=&quot;phone&quot; /&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--DOB--&gt;
&lt;TextView
android:id=&quot;@+id/DOB&quot;
android:layout_width=&quot;150dp&quot;
android:layout_height=&quot;50dp&quot;
android:layout_marginBottom=&quot;140dp&quot;
android:textAlignment=&quot;center&quot;
android:hint=&quot;Date of Birth&quot;
android:layout_below=&quot;@+id/addressTIL&quot;
android:layout_marginTop=&quot;10dp&quot;
android:layout_marginLeft=&quot;30dp&quot;
android:textSize=&quot;20dp&quot;/&gt;
&lt;!--Age--&gt;
&lt;TextView
android:id=&quot;@+id/AgeAuto&quot;
android:layout_width=&quot;150dp&quot;
android:layout_height=&quot;50dp&quot;
android:layout_marginBottom=&quot;140dp&quot;
android:textAlignment=&quot;center&quot;
android:hint=&quot;Age&quot;
android:layout_toRightOf=&quot;@+id/DOB&quot;
android:layout_below=&quot;@id/addressTIL&quot;
android:layout_margin=&quot;10dp&quot;
android:layout_marginLeft=&quot;10dp&quot;
android:textSize=&quot;20dp&quot;/&gt;
&lt;!--Address--&gt;
&lt;android.support.design.widget.TextInputLayout
android:id=&quot;@+id/addressTIL&quot;
android:layout_width=&quot;325dp&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_centerVertical=&quot;true&quot;
android:layout_centerHorizontal=&quot;true&quot;
android:layout_below=&quot;@+id/phoneTIL&quot;
app:passwordToggleEnabled=&quot;true&quot;&gt;
&lt;EditText
android:id=&quot;@+id/addressEt&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:hint=&quot;Address&quot;
android:inputType=&quot;textShortMessage&quot;/&gt;
&lt;/android.support.design.widget.TextInputLayout&gt;
&lt;!--Already Register? Login now--&gt;
&lt;TextView
android:id=&quot;@+id/accountExist&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:layout_marginBottom=&quot;140dp&quot;
android:textAlignment=&quot;center&quot;
android:text=&quot;Already have account? Login now&quot;
android:layout_below=&quot;@+id/AgeAuto&quot;
android:layout_margin=&quot;10dp&quot;/&gt;
&lt;/RelativeLayout&gt;
&lt;/ScrollView&gt;
&lt;/RelativeLayout&gt;

答案1

得分: 0

请注意,以下是您要翻译的内容:

请尝试以下添加DatePicker的方法:

首先,在XML中以这种方式添加DatePicker:

<DatePicker
    android:id="@+id/datePicker"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:datePickerMode="spinner"/>

现在进入您的Activity.java文件:
//您可以使用可见性函数来显示/隐藏日期选择器。
//或者尝试创建一个包含DatePicker的弹出窗口。这样做会更容易,而且看起来更清晰。

DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
datePicker.setSpinnersShown(false);

在点击jDOB之后:

//jDOB.setOnClickListener:
String day = "Day = " + datePicker.getDayOfMonth();
String month = "Month = " + datePicker.getMonth();
String year = "Year = " + datePicker.getYear();

String dobDate = day + "/" + month + "/" + year;
jDOB.setText(dobDate);


<details>
<summary>英文:</summary>
Can you try this way of adding DatePicker?
First add the DatePicker in XML this way:
&lt;DatePicker
android:id=&quot;@+id/datePicker&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;
android:datePickerMode=&quot;spinner&quot;/&gt;
Now go to your Activity.java:
//You can use visibility function to show/hide the datapicker.
// Or try creating a pop-up containing the DatePicker. It will be easy and look cleaner.
DatePicker datePicker = (DatePicker)findViewById(R.id.datePicker);
datePicker.setSpinnersShown(false);
Now after clicking the jDOB :
//jDOB.setOnClickListener:
String day = &quot;Day = &quot; + datePicker.getDayOfMonth();
String month = &quot;Month = &quot; + datePicker.getMonth();
String year = &quot;Year = &quot; + datePicker.getYear();
String dobDate = day + &quot;/&quot; + month + &quot;/&quot; + year;
jDOB.setText(dobDate);
</details>
# 答案2
**得分**: 0
```java
你注册了两次点击监听器
在xml中使用了onClick,在Java代码中使用了setOnClickListener
所以你需要移除其中一个... 在你的情况下移除onClickListener,然后将你的代码放在switch case中
@Override
public void onClick(View view){
switch (view.getId()){
case R.id.DOB:
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(registerPage.this, android.R.style.Theme_Holo_Dialog_MinWidth, mDateSetListener, year, month, day);
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
break;
}
}
英文:

You registered the click listener twice

onClick in xml and setOnClickListener in java code

So you have to remove one of them .. in your case remove onClickListener and put your code in switch case

@Override
public void onClick(View view){
switch (view.getId()){
case R.id.DOB:
Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(registerPage.this, android.R.style.Theme_Holo_Dialog_MinWidth, mDateSetListener, year, month, day);
datePickerDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
datePickerDialog.show();
break;
}
}

huangapple
  • 本文由 发表于 2020年4月5日 14:21:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/61038781.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定