我是一名新的Android开发人员和Java编程初学者。我正在开发一个拼车应用程序,但一些问题已经出现。我在3页上使用了Tabbed Activity,但只有一个会给我带来麻烦。我想在单击“浮动操作”按钮时检索EditText数据,但没有任何工作。
修改:通过添加PlaceholderFragment frag = (PlaceholderFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.containerNovo + ":" + mViewPager.getCurrentItem());
解决问题,其中 containerNovo 是我的ViewPager mViewPager 的ID。
我的主要XML文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/azul"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="@+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/azulclaro"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="@+id/containerNovo"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="@+id/fabNovo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/fab_margin"
android:src="@drawable/check" />
我的主要java代码:
package petma.testesappcarona;
public class NovaCarona extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
FloatingActionButton fab;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nova_carona);
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.containerNovo);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
fab = (FloatingActionButton) findViewById(R.id.fabNovo);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(NovaCarona.this, "input_nome.getText().toString()", Toast.LENGTH_SHORT).show();
}
});
}
片段代码:
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
EditText input_nome,input_data, input_horario, input_saida, input_chegada, input_Recado;
Spinner input_pessoas;
SessionManager session;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_nova_carona, container, false);
session = new SessionManager(getContext());
session.checkLogin();
input_data = (EditText)rootView.findViewById(R.id.input_data);
assert input_data != null;
input_data.setOnClickListener(this);
input_horario = (EditText) rootView.findViewById(R.id.input_horario);
assert input_horario != null;
input_horario.setOnClickListener(this);
input_chegada = (EditText)rootView.findViewById(R.id.input_chegada);
input_saida = (EditText)rootView.findViewById(R.id.input_saida);
input_nome = (EditText)rootView.findViewById(R.id.input_nome);
input_pessoas = (Spinner)rootView.findViewById(R.id.input_pessoas);
input_Recado = (EditText) rootView.findViewById(R.id.input_Recado);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayShowHomeEnabled(true);
((AppCompatActivity)getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
return rootView;
}
@Override
public void onClick(View view) {
if (view == input_data){
int mYear, mMonth, mDay;
// Get Current Date
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePickerDialog = new DatePickerDialog(getContext(),
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
input_data.setText(year + "-" + (monthOfYear + 1) + "-" + dayOfMonth);
}
}, mYear, mMonth, mDay);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
datePickerDialog.show();
}
else if (view == input_horario){
int mHour, mMinute;
final Calendar c = Calendar.getInstance();
mHour = c.get(Calendar.HOUR_OF_DAY);
mMinute = c.get(Calendar.MINUTE);
// Launch Time Picker Dialog
TimePickerDialog timePickerDialog = new TimePickerDialog(getContext(),
new TimePickerDialog.OnTimeSetListener() {
@Override
public void onTimeSet(TimePicker view, int hourOfDay,
int minute) {
if(minute <= 9){
input_horario.setText(hourOfDay + ":"+"0"+minute);
}
else{
input_horario.setText(hourOfDay + ":" + minute);
}
}
}, mHour, mMinute, false);
timePickerDialog.show();
}
}
}
我想替换“input_nome.getText()。toString()”部分。我试图使用PlaceholderFragment,但我不知道片段的ID是什么。我没有找到人们使用PlaceholderFragment和Tabbed Activity的代码。
答案 0 :(得分:0)
您需要覆盖选项卡适配器的getItem()方法,以检索在该选项卡中添加的片段的实例,以便您可以从片段中调用方法,该片段从editText中检索文本
public class TabAdapter extends FragmentStatePagerAdapter {
private final List<Fragment> fragments = new ArrayList<>();
private final List<String> fragmentsTitles = new ArrayList<>();
private Context context;
public TabAdapter (FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
@Override
public Fragment getItem(int position) {
return fragments.get(position);
}
.
.
.
}
adapter.getItem(position).getEditTextString();
答案 1 :(得分:0)
通过添加:PlaceholderFragment frag = (PlaceholderFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.containerNovo + ":" + mViewPager.getCurrentItem()); , where containerNovo is the id of my ViewPager mViewPager.
解决了问题,其中containerNovo是我的ViewPager mViewPager的id。