【问题标题】:Passing values around传递值
【发布时间】:2019-10-09 14:59:31
【问题描述】:

所以我有一个应用程序,我需要为过滤器选择一个日期并且选择工作正常,但我似乎无法更改选择屏幕上的值。这就是我称之为SelectDateFragment 的地方。还有更多代码,但这并不重要。

public class filter extends BottomSheetDialogFragment {

public static filter getInstance() {
    return new filter();
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    final View view = inflater.inflate(R.layout.filter_popup, container, false);
    final TextView dateTextView = (TextView) view.findViewById(R.id.KuupaevValikView);
    final View button1 = view.findViewById(R.id.Nooleke1);
    final View button2 = view.findViewById(R.id.KuupaevValikView);
    final View button3 = view.findViewById(R.id.Nooleke3);
    final View button4 = view.findViewById(R.id.Nooleke4);
    button1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating the instance of PopupMenu
            PopupMenu popup1 = new PopupMenu(getActivity(), button1);
            //Inflating the Popup using xml file
            popup1.getMenuInflater()
                    .inflate(R.menu.filtermenuprice, popup1.getMenu());

            //registering popup1 with OnMenuItemClickListener
            popup1.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
                public boolean onMenuItemClick(MenuItem item) {
                    ((TextView)view.findViewById(R.id.HinnavahemikValikView)).setText (item.getTitle());
                    Toast.makeText(
                            getActivity(),
                            "You Clicked : " + item.getTitle(),
                            Toast.LENGTH_SHORT
                    ).show();
                    return true;
                }
            });
            popup1.show();
        }}
        );

    button2.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View arg0) {
            DialogFragment newFragment = new SelectDateFragment();
            newFragment.show(getFragmentManager(), "DatePicker");

        }
    });

这就是它的名字:

public class SelectDateFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
   @Override
   public Dialog onCreateDialog(Bundle savedInstanceState) {
       final Calendar calendar = Calendar.getInstance();
       int yy = calendar.get(Calendar.YEAR);
       int mm = calendar.get(Calendar.MONTH);
       int dd = calendar.get(Calendar.DAY_OF_MONTH);
       return new DatePickerDialog(getActivity(), this, yy, mm, dd);
   }

   public void onDateSet(DatePicker view, int yy, int mm, int dd) {
       populateSetDate(yy, mm+1, dd);
   }
   public void populateSetDate(int year, int month, int day) {
       String date = month+"/"+day+"/"+year;
       Toast.makeText(getActivity(), date, Toast.LENGTH_SHORT).show();
       //.setText(month+"/"+day+"/"+year);
   }

}

我想将 button2 替换为名为 date 的字符串,但我无法做到。

【问题讨论】:

  • 那么,您想用相同位置的字符串替换按钮?我对您要更改的值感到困惑。
  • 对不起,我应该澄清一下,我前一段时间不小心将它命名为 button2,但它实际上是一个 textview。
  • 那么,您正试图将所选日期的字符串放入“button2”文本视图中,对吧?
  • 是的,这正是我想要做的
  • 好的。差不多了。你的 Toast 工作正常吗?

标签: java android-fragments dialogfragment


【解决方案1】:

这应该可行。如果没有,请告诉我,我们将深入研究:

public void populateSetDate(int year, int month, int day) {
   String date = month+"/"+day+"/"+year;
   Toast.makeText(getActivity(), date, ).show();
   button2.text = date;

}

只要您的 onDateSet 函数正常工作,并且您在 populateSetDate 中获取 Toast,您就应该能够使用日期字符串来填充您的 textView(并且您应该稍后更改它的变量名称)。

【讨论】:

  • 谢谢你,会试试这个,让你知道。
  • 请做!我相信这应该是您所需要的;但如果这不起作用,可能会有一些潜在的问题。
  • 这不起作用,因为它无法识别 DialogFragment 中的变量 button2。
  • 这是一个简单的范围问题。这是正确的答案。只需将 button2 (应再次重命名)导入您的函数。似乎有比嵌套片段更简单的方法来解决这个问题。但这只是一种意见,我不确定哪种方法会被认为是最佳实践。
猜你喜欢
  • 2014-09-14
  • 1970-01-01
  • 2016-10-22
  • 2021-06-18
  • 1970-01-01
  • 1970-01-01
  • 2014-04-29
  • 1970-01-01
相关资源
最近更新 更多