在工具栏上有一个按钮。单击该按钮时,会弹出一个对话框弹出窗口。在对话框中有2个微调器和一个“选择”按钮。我需要在Fragment中获取当前所选的微调器项。我该怎么办?
public class MainActivity extends AppCompatActivity {
//Toolbar
Toolbar toolbar;
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
//Navigation Drawer
DrawerLayout drawerLayout;
ActionBarDrawerToggle actionBarDrawerToggle;
NavigationView navigationView;
Spinner spinner1, spinner2;
Dialog dialog;
Button toolbarButton;
public String sp11;
public String sp22;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbarButton = (Button) findViewById(R.id.toolBarButton);
toolbar = (Toolbar) findViewById(R.id.toolBar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar,R.string.drawer_open, R.string.drawer_close);
drawerLayout.addDrawerListener(actionBarDrawerToggle);
navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem item) {
return false;
}
});
tabLayout = (TabLayout) findViewById(R.id.tabLayout);
viewPager = (ViewPager) findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new TimeTableFragment(), "MONDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "TUESDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "WEDNESDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "THURSDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "FRIDAY");
viewPagerAdapter.addFragments(new TimeTableFragment(), "SATURDAY");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
}
public void myDialogPopUp(View view)
{
dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_design);
spinner1 = (Spinner) dialog.findViewById(R.id.spinner1);
MyCustAdapter adapter = new MyCustAdapter(MainActivity.this, getResources().getStringArray(R.array.sp1));
spinner1.setAdapter(adapter);
spinner2 = (Spinner) dialog.findViewById(R.id.spinner2);
adapter = new MyCustAdapter(MainActivity.this, getResources().getStringArray(R.array.sp2));
spinner2.setAdapter(adapter);
dialog.setCancelable(true);
dialog.show();
}
public void onButtonSelect(View view)
{
sp11 = spinner1.getSelectedItem().toString();
sp22 = spinner2.getSelectedItem().toString();
//Toast.makeText(MainActivity.this, sp11+"\n"+sp22, Toast.LENGTH_SHORT).show();
dialog.cancel();
toolbarButton.setText(sp11+"\t\t\t˅\n"+sp22);
}
@Override
protected void onPostCreate(@Nullable Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actionBarDrawerToggle.syncState();
}
}
public class TimeTableFragment extends Fragment {
public TimeTableFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Toast.makeText(getActivity(), "\nonCreateView", Toast.LENGTH_SHORT).show();
return inflater.inflate(R.layout.fragment_time_table, container, false);
}
}
答案 0 :(得分:0)
您可以创建自己的对话框并扩展Dialog。 然后在对话框中添加一个方法:
public void setIndex(int index) {
this.index = index;
}
然后在你的活动中:
dialog = new CustomDialog(MainActivity.this);
dialog.setContentView(R.layout.custom_dialog_design);
dialog.setCancelable(true);
dialog.setIndex(index);
dialog.show();
答案 1 :(得分:0)
在TimeTableFragment中创建一个方法来获取这两个值。
public class TimeTableFragment extends Fragment {
public void spinnerSelection(String one,String two){
}
}
现在您需要从视图寻呼机获取当前选定的片段。为此使用以下代码。
TimeTableFragment ttFrag=(TimeTableFragment)viewPagerAdapter.instantiateItem(viewPager , currentPosition)
ttFrag.spinnerSelection("Spinner1","Spinner2");
将其置于Spinner项目单击事件中。
可以使用以下方法获得当前位置
viewPager.getCurrentItem()
或
viewPageListener = new PageListener();
viewPager.setOnPageChangeListener(viewPageListener);