簡體   English   中英

如何從選項卡片段上的EditText獲得getText()

[英]How getText() from EditText on tab fragment

我是一名新的Android開發人員,也是Java編程的初學者。 我正在開發一個拼車應用程序,但是出現了一些問題。 我在3頁上使用了“選項卡式活動”,但是只有一個正在給我帶來麻煩。 我想在單擊“浮動操作按鈕”時檢索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的代碼。

您需要重寫選項卡適配器的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();

通過添加來解決問題: PlaceholderFragment frag = (PlaceholderFragment) getSupportFragmentManager().findFragmentByTag("android:switcher:" + R.id.containerNovo + ":" + mViewPager.getCurrentItem()); , where containerNovo is the id of my ViewPager mViewPager. 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。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM