java - 传递值

标签 java android-fragments dialogfragment

所以我有一个应用程序,我需要为过滤器选择日期,并且选择工作正常,但我似乎无法更改选择屏幕上的值。这就是我调用 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);
   }

}

我想用名为 date 的字符串替换 button2,但我无法做到这一点。

最佳答案

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

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(并且您应该稍后更改该变量名称)。

关于java - 传递值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58307032/

相关文章:

java - 为什么这个Java小程序会让MacOS重启?

android - Android 中 Fragments 和 Activity 之间的数据共享

java - 使用 jdbc 连接到数据库的安全问题

java - 将多个表/列放入 CSV 文件

android - 如何修复 FragmentManager 已经在 FragmentTransaction 中执行事务?

java - 在 BottomNavigationView 项目触摸时,Fragment 中的 RecyclerView 有时为空

android - DialogFragment与addToBackstack方法连接

android - BottomSheetDialogFragment - 听用户事件解除

Android:禁用 DialogFragment 确定/取消按钮

java - JTable 渲染器不会为单元格着色