我为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
而不是出现在吐司中。我怎么能通过修改片段代码来做到这一点?
答案 0 :(得分:2)
为简单编写此代码,请选择日期
((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);
}