代码之家  ›  专栏  ›  技术社区  ›  Ajay Kulkarni

将Matarial日期选择器值添加到textview

  •  0
  • Ajay Kulkarni  · 技术社区  · 7 年前

    我为android编写了一个自定义日期选择器,用于添加出生日期。代码如下:
    layout.xml :

    <!--Date of Birth Label -->
            <android.support.design.widget.TextInputLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="8dp"
                android:layout_marginBottom="8dp">
                <EditText android:id="@+id/dob"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:hint="Date of Birth"
                    android:onClick="showDatePicker"/>
            </android.support.design.widget.TextInputLayout>    
    

    MainActivity.java:

    package com.emc.kulkaa.dellcsrmate;
    
    import android.app.DialogFragment;
    import android.content.Intent;
    import android.support.v7.app.AppCompatActivity;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.TextView;
    
    import org.w3c.dom.Text;
    
    public class RegistrationActivity extends AppCompatActivity {
    
        private TextView textView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.layout);
    
            textView = (TextView) findViewById(R.id.link_login);
            textView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Intent intent = new Intent(RegistrationActivity.this, LoginActivity.class);
                    startActivity(intent);
                }
            });
    
        }
    
        public void showDatePicker(View v) {
            DialogFragment newFragment = new MyDatePickerFragment();
            newFragment.show(getFragmentManager(), "Date Picker");
        }
    
    }  
    

    以下是日期片段:

    package com.emc.kulkaa.dellcsrmate;
    
    import android.app.DatePickerDialog;
    import android.app.Dialog;
    import android.app.DialogFragment;
    import android.os.Bundle;
    import android.widget.DatePicker;
    import android.widget.Toast;
    
    import java.util.Calendar;
    
    /**
     * Created by kulkaa on 1/18/2018.
     */
    
    public class MyDatePickerFragment extends DialogFragment {
        @Override
        public Dialog onCreateDialog(Bundle savedInstanceState) {
    
            final Calendar c = Calendar.getInstance();
            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);
    
            return new DatePickerDialog(getActivity(), dateSetListener, year, month, day);
        }
    
        private DatePickerDialog.OnDateSetListener dateSetListener =
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Toast.makeText(getActivity(), "selected date is " + view.getYear() +
                                " / " + (view.getMonth() + 1) +
                                " / " + view.getDayOfMonth(), Toast.LENGTH_SHORT).show();
                    }
                };
    }  
    

    以上代码成功运行。在自定义日期选择器中选择的值显示在 Toast 成功地

    现在,我希望所选值显示在 EditText 而不是出现在吐司中。如何通过修改片段代码来实现这一点?

    1 回复  |  直到 7 年前
        1
  •  2
  •   Munir    7 年前

    为了简单起见,请在选定的日期编写此代码

    ((TextView) getActivity().findViewById(R.id.link_login)).setText("date");
    

    因此,您的代码看起来

    public void onDateSet(DatePicker view, int year, int month, int day) {
                        Toast.makeText(getActivity(), "selected date is " + view.getYear() +
                                " / " + (view.getMonth() + 1) +
                                " / " + view.getDayOfMonth(), Toast.LENGTH_SHORT).show();
                        String date ="selected date is " + view.getYear() +
                                " / " + (view.getMonth() + 1) +
                                " / " + view.getDayOfMonth();
    
                        ((TextView) getActivity().findViewById(R.id.link_login)).setText(date);
                    }