我正在使用Tabbed Activity。 在每日提醒/特别提醒中,保存按钮不起作用。 我给了正确的id,链接和OnClickListener。但它只是不起作用。
Reminders Activity is a simple activity with List ReminderTabActivity s Tabbed Activity 这里是onCreateView of ReminderTabActivity,其中正在调用片段。
public class ReminderTabActivity extends AppCompatActivity implements DailyReminderFragment.OnMessageSetListener {
/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
String type = "";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_reminder_tab);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Intent intent = getIntent();
if(intent.hasExtra("key")) {
String key = intent.getStringExtra("key");
type = intent.getStringExtra("etype");
Log.d("mKey", key);
if(type.equals("special")) {
mViewPager.setCurrentItem(2);
SpecialReminderFragment srf = new SpecialReminderFragment();
Bundle bundle = new Bundle();
bundle.putString("key", key);
srf.setArguments(bundle);
//srf.updateSpecial(key);
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_reminder_tab, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setMessage(String message) {
Log.d("messageSet", message);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(getArguments().getInt(ARG_SECTION_NUMBER)==1) {
View rootView = inflater.inflate(R.layout.fragment_daily_reminder, container, false);
return rootView;
}
else if(getArguments().getInt(ARG_SECTION_NUMBER)==2) {
View rootView = inflater.inflate(R.layout.fragment_special_reminder, container, false);
return rootView;
}
else {
View rootView = inflater.inflate(R.layout.fragment_reminder_tab, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "DAILY REMINDER";
case 1:
return "SPECIAL REMINDER";
}
return null;
}
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d("kedar", "ONLOAD");
rootView = inflater.inflate(R.layout.fragment_daily_reminder, container, false);
mAuth = FirebaseAuth.getInstance();
btn_save = (Button) rootView.findViewById(R.id.btn_save);
etTitle = (EditText) rootView.findViewById(R.id.etTitle);
picker = (TimePicker) rootView.findViewById(R.id.timePicker);
mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
saveReminder();
OnMessageSetListener.setMessage("test");
}
});
return rootView;
}
这是布局 - `
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etTitle"
android:layout_below="@+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Title"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<TimePicker
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/etTitle"
android:id="@+id/timePicker"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp" />
<Button
android:text="SAVE"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btn_save"
android:layout_below="@+id/timePicker"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
/>
</RelativeLayout>
`
我可以看到布局,但它没有在控制台中显示Log.d消息,也没有按钮onClickListener正在运行。
你们有什么人认为我在这里做错了?
答案 0 :(得分:1)
哪个片段有你在帖子的最后添加的oncreateview方法?我认为你应该将onclicklistener和其他视图创建移动到oncreateView of placeholderfragment相应的视图,以便工作,如果不是