Android Sdk DatePickerFragment 和 TextView [英] Android Sdk DatePickerFragment and TextView

查看:20
本文介绍了Android Sdk DatePickerFragment 和 TextView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了下一个问题.我想将使用我的 DatePickerFragment 输入的日期传递给 TextView,但我不知道我实际上如何做到这一点.

i got the next problem. I want to pass the date i've entered using my DatePickerFragment to TextView, but i don't know how i can actually do it.

当我在 DatePickerFragment 类中创建 TextView 对象,然后使用 onDateSet 方法在其中设置文本时 - 应用程序崩溃.也许这个问题太基础了,但我没有找到任何合适的解决方案.

When i create TextView object in DatePickerFragment class and then set the text in it using onDateSet method - the app crashes. Maybe this question too basic, but i didn' find any suitable solution.

public class DatePickerFragment extends DialogFragment 
            implements DatePickerDialog.OnDateSetListener{

TextView tvDate1 = (TextView) getView().findViewByiD(R.id.tvDate);


@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(),this,year,month,day);
}

public void onDateSet(DatePicker view,int year, int month, int day){
       String

    StringBuilder sb = new StringBuilder();
    sb.append(day);
    sb.append("/");
    sb.append(month);
    sb.append("/");
    sb.append(year);
    tvDate1.setText(sb);


}
}

推荐答案

您的应用可能因此崩溃

   TextView tvDate1 = (TextView) getView().findViewByiD(R.id.tvDate);

我猜您的选择器在不同的文件中并且您正在尝试初始化 textview.findViewById 是activity类的一个方法

I guess your picker is in a different file and you are trying to initialize textview. findViewById is a method of activity class

使用接口

http://developer.android.com/guide/components/fragments.html

查看与活动交流下的主题

Check the topic under communicating with activity

public class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {

    TheListener listener;

public interface TheListener{
    public void returnDate(String date);
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
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);
listener = (TheListener) getActivity(); 

// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}

@Override
public void onDateSet(DatePicker view, int year, int month, int day) {
Calendar c = Calendar.getInstance();
c.set(year, month, day);

SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) 
{
  listener.returnDate(formattedDate); 

}

}
}

然后在MainActivity中实现接口并将日期设置为textview

Then in MainActivity implement the interface and set the date to textview

public class MainActivity extends Activity implements DatePickerFragment.TheListener{

    Button b;
    TextView tv;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        b= (Button) findViewById(R.id.button1);
        tv= (TextView) findViewById(R.id.textView1);
        b.setOnClickListener(new OnClickListener()
        {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                DialogFragment picker = new DatePickerFragment();
                picker.show(getFragmentManager(), "datePicker");
            }

        });
    }

    @Override
    public void returnDate(String date) {
        // TODO Auto-generated method stub
        tv.setText(date);
    }

}

这篇关于Android Sdk DatePickerFragment 和 TextView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆