2016-04-01 6 views
0

私は、アクティビティから名前、電子メールIDなどのEditTextフィールドを持つ予約ページを含むフラグメントに移動しているプロジェクトに取り組んでいます。私が知りたいのは、それらのフィールドの値を取得し、それらをフラグメントとして電子メールとして送信できるかどうかです。はいの場合は助けてください。 MainActivity.javaフラグメントを使用して登録ページを作成することができます

public class MainActivity extends AppCompatActivity { 
    FloatingActionButton fab; 
    RelativeLayout aboutUs; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     aboutUs = (RelativeLayout) findViewById(R.id.aboutUs); 

     Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 

     fab = (FloatingActionButton) findViewById(R.id.fab); 
     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View view) { 
       Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
         .setAction("Action", null).show(); 
      } 
     }); 
    } 


    //click methods goes here 

    public void clickAboutUs(View view){ 

     /*android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager(); 
     android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); 
     FragmentAboutUs fragmentAboutUs = new FragmentAboutUs(); 
     fragmentTransaction.replace(R.id.fragment_container, fragmentAboutUs); 
     fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN); 
     fragmentTransaction.addToBackStack(null); 

     fragmentTransaction.commit();*/ 

     FragmentAboutUs fragmentAboutUs = new FragmentAboutUs(); 
     replaceFragment(fragmentAboutUs , true); 



    } 



    public void clickEvents(View view){ 


     FragmentEvents fragmentEvents = new FragmentEvents(); 
     replaceFragment(fragmentEvents,true); 



    } 

    public void clickMedia(View view){ 


     FragmentMedia fragmentMedia = new FragmentMedia(); 
     replaceFragment(fragmentMedia,true); 



    } 

    public void clickContact(View view){ 


     FragmentContact fragmentContact = new FragmentContact(); 
     replaceFragment(fragmentContact,true); 




    } 

    public boolean popFragment() { 
     boolean isPop = false; 

     Fragment currentFragment = getSupportFragmentManager() 
       .findFragmentById(R.id.fragment_container); 

     if (getSupportFragmentManager().getBackStackEntryCount() > 0) { 
      isPop = true; 
      getSupportFragmentManager().popBackStackImmediate(); 
     } 

     return isPop; 
    } 

    @Override 
    public void onBackPressed() { 
     if (!popFragment()) { 
      finish(); 
     } 
    } 

    public void replaceFragment(Fragment fragment, boolean addToBackStack) { 

     android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager() 
       .beginTransaction(); 

     if (addToBackStack) { 
      transaction.addToBackStack(null); 

     } else { 
      getSupportFragmentManager().popBackStack(null, 
        FragmentManager.POP_BACK_STACK_INCLUSIVE); 

     } 
     transaction.replace(R.id.fragment_container, fragment); 
     transaction.commit(); 
     getSupportFragmentManager().executePendingTransactions(); 

    } 





    @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_main, 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); 
    } 


} 

は、ここで私はreserationフォームを含めることを計画していたフラグメントFragmentContactで

FragmentContact.java

public class FragmentContact extends Fragment 
{ 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.contac_us, container,false); 
    return view; 
} 
} 

私のフラグメントクラスです。私は "アンドロイドの断片を使って登録ページ"を検索するが、何の助けも得ていない。

+1

はい、可能ですが、StackOverflowはチュートリアルサービスではありません。作業中のコードを[編集]に含めてください。 –

+0

私は今まで何をしたのですか?私は予約のためのXMLファイルを作成しましたが、今はどのようにフラグメントを使用するのか分かりません。私は活動でそれを行うことができますが、私は断片的に活動の数を減らすためにそれをしたい –

答えて

0

フラグメントとアクティビティのコードの違いは何もありません。あなたは今までと同じようにfindViewByIdを使うことができますが、膨張したビューを使うだけでいいです。

public class FragmentContact extends Fragment 
{ 
    private EditText address, body; 
    private Button sendEmailButton; 

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     View view = inflater.inflate(R.layout.contac_us, container,false); 

     sendEmailButton = (Button) view.findViewById(R.id.send_email); 
     sendEmailButton.setOnClickListener(...); // TODO: Implement 

     return view; 
    } 

    public void sendEmail(String address, String body) { } 
} 
+0

ありがとう。私の疑問は、onCreateViewメソッドで値を取得する必要があるかどうか、または私がアクティビティで行うようにonCreate(Bundle)メソッドを作成する必要があるかどうかでした。 –

+0

フラグメントでは、ビューにアクセスする前に 'onCreate'が呼び出されます。 –

+0

私があなたに何を見せようとしているのか分かりませんが、コードのコメントは使用しないでください。 –

関連する問題