【问题标题】:Adding Matarial date picker value to textview将 Material datepicker 值添加到 textview
【发布时间】:2018-01-18 09:57:32
【问题描述】:

我为 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 中,而不是出现在 toast 中。如何通过修改片段代码来做到这一点?

【问题讨论】:

    标签: java android android-fragments datepicker


    【解决方案1】:

    为了简单写这段代码 onDate selected

    ((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);
                    } 
    

    【讨论】:

      猜你喜欢
      • 2020-08-24
      • 2019-03-05
      • 2013-09-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多