我正在开发一个项目,我在其中从活动导航到一个片段,其中包含我的预订页面,其中包含EditText字段,如Name,Email 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中,我计划包含一个移植表格。我在android"中使用片段搜索"注册页面但没有得到任何帮助。
答案 0 :(得分:1)
片段与活动中的代码没有什么不同。您可以像以前一样使用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) { }
}