我正在一個項目中,我從活動導航到一個片段,其中包含我的預訂頁面與EditText字段如名稱,電子郵件ID。我想知道的是,是否有可能獲得這些字段的值並將它們作爲電子郵件發送給片段。如果是的話,請幫助。 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);
}
}
這裏是我的片段類
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;
}
}
在片段FragmentContact我計劃包括reseration形式。我搜索「在Android中使用片段的註冊頁面」,但沒有得到任何幫助。
是的,這是可能的,但StackOverflow不是教程服務。請在[編輯]中包含您正在使用的代碼。 –
這就是我所做的,直到現在。我已經創建了XML文件進行預留,但現在我不知道如何使用片段。我可以在活動中做到這一點,但我想要在片段中做到這一點,以減少活動數量 –