gpt4 book ai didi

java - 将 Material 日期选择器值添加到 TextView

转载 作者:行者123 更新时间:2023-12-02 02:22:24 26 4
gpt4 key购买 nike

我为 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");
}

}

这是日期 fragment :

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 中。如何通过修改 fragment 代码来实现呢?

最佳答案

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

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

关于java - 将 Material 日期选择器值添加到 TextView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48318416/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com