我创建了一个带有可滑动视图的标签布局。 我正在尝试从片段传递一个字符串 碎片谢谢你提前 Details_customer.java
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.design.widget.TabLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;
public class Details_customer extends AppCompatActivity implements Cust_Details_basic.FragmentDataListener {
SharedPreferences login_pref, IP;
private ProgressDialog pDialog;//For Loading activity..
Bundle dataBundle;
/**
* The {@link PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_details_customer);
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.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
@Override
public void onFragmentDataUpdated(Bundle dataBundle)
{
this.dataBundle=dataBundle;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_main, menu);
//getMenuInflater().inflate(R.menu.menu_details_customer, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_logout:
login_pref = getSharedPreferences("Login Pref", MODE_PRIVATE);
SharedPreferences.Editor editor = login_pref.edit();
editor.putString("username", null);
Intent intent = new Intent(this, MainActivity.class);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
Toast.makeText(getApplicationContext(), "Logout", Toast.LENGTH_SHORT).show();
/*editor.clear();*/
editor.commit();
finish();
break;
}
return super.onOptionsItemSelected(item);
}
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
public Action getIndexApiAction() {
Thing object = new Thing.Builder()
.setName("Details_customer Page") // TODO: Define a title for the content shown.
// TODO: Make sure this auto-generated URL is correct.
.setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
.build();
return new Action.Builder(Action.TYPE_VIEW)
.setObject(object)
.setActionStatus(Action.STATUS_TYPE_COMPLETED)
.build();
}
@Override
public void onStart() {
super.onStart();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client.connect();
AppIndex.AppIndexApi.start(client, getIndexApiAction());
}
@Override
public void onStop() {
super.onStop();
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
AppIndex.AppIndexApi.end(client, getIndexApiAction());
client.disconnect();
}
//deleted
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Cust_Details_basic cust_basic = new Cust_Details_basic();
cust_basic.setArguments(dataBundle);
return cust_basic;
case 1:
Cust_Details_address address = new Cust_Details_address();
address.setArguments(dataBundle);
return address;
/* case 2:
Cust_Details_last details_last = new Cust_Details_last();
return details_last;*/
}
return null;
}
@Override
public int getCount() {
// Show 2 total pages.
return 2;
}
/***
* new addition
***/
public void showFragment(String val) {
System.out.println(val);
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Basic Info";
case 1:
return "Address";
/* case 2:
return "Address";*/
}
return null;
}
}}
Cust_Details_basic.java这是我想要将值发送到下一个片段的Edittext
public class Cust_Details_basic extends Fragment {
EditText finame;
private ProgressDialog pDialog;//For Loading activity..
public interface FragmentDataListener{
void onFragmentDataUpdated(Bundle dataBundle);
}
private FragmentDataListener mFragmentDataListener;
@Override
public void onAttach( Context context )
{
super.onAttach( context );
mFragmentDataListener=(FragmentDataListener)getActivity();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_customer_1, container, false);
finame = (EditText)view.findViewById(R.id.fname);
/**********************************************/
Bundle bundle = new Bundle();
bundle.putString("fname",finame.getText().toString());
mFragmentDataListener.onFragmentDataUpdated(bundle);
/*********************************************/
return view;
}}
这里是Cust_Details_address.java,我想要Edittext值
public class Cust_Details_address extends Fragment implements View.OnClickListener{
Button btnsubmit;
String fname,firstname;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_customer_2, container, false);
Bundle bundle = this.getArguments();
if (bundle != null) {
firstname = bundle.getString("fname",fname);
Toast.makeText(getActivity().getApplicationContext(),"fname:"+firstname,Toast.LENGTH_SHORT).show();
Log.d("First Name:","=======>"+firstname);
}
else{
Toast.makeText(getActivity().getApplicationContext(),"Am Empty:",Toast.LENGTH_SHORT).show();
}
btnsubmit = (Button)view.findViewById(R.id.btnsubmit);
btnsubmit.setOnClickListener(this);
return view;
}}
答案 0 :(得分:1)
在您的活动定义接口,bundle对象中,然后使用片段中的数据调用Activity实例的接口方法:
在 Details_customer 活动中,您可以执行此操作,
public class Details_customer extends AppCompatActivity
implements Cust_Details_basic.FragmentDataListener {
SharedPreferences login_pref,IP;
private ProgressDialog pDialog;//For Loading activity..
Bundle dataBundle;
..... other code
// override method of interface
@override void onFragmentDataUpdated(Bundle dataBundle)
{
this.dataBundle=dataBundle;
}
...... other code
// in SectionsPagerAdapter adapter
@Override public Fragment getItem(int position) {
switch (position) {
case 0:
Cust_Details_basic cust_basic = new Cust_Details_basic();
cust_basic.setArguments(dataBundle); // remember to update
//bundle object as per requirement
return cust_basic;
case 1:
Cust_Details_address address = new Cust_Details_address();
address.setArguments(dataBundle);
return address;
/* case 2:
Cust_Details_last details_last = new Cust_Details_last();
return details_last;*/
}
return null;
}
在 Cus_Details_basic 片段中,声明界面:
public class Cust_Details_basic extends Fragment {
public interface FragmentDataListener{
void onFragmentDataUpdated(Bundle dataBundle);
}
private FragmentDataListener mFragmentDataListener;
// create fragment object and initialize it in onAttach()
@Override public void onAttach( Context context )
{
super.onAttach( context );
mFragmentDataListener=(FragmentDataListener)getActivity();
}
// then update your onCreate as:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details_customer_1, container, false);
Fragment fragment = new Fragment();
Bundle bundle = new Bundle();
bundle.putString("fname",fname);
//fragment.setArguments(bundle);
mFragmentDataListener.onFragmentDataUpdated(bundle);
// this will update dataBundle object in your activity
return view;
}
这将调用onFragmentDataUpdated()
课程的Details_customer
,该课程将更新其dataBundle
对象,现在只要Viewpager
更改,您就可以setArguments()
使用此Bundle
对象,并且该片段的getArguments()
中的onCreate
最终将能够检索数据。
有用的链接:
Communicating between Fragments.
How To Communicate Between Fragments and Activities in Android.
答案 1 :(得分:1)
CallBacks(接口)和 Bundles ,但这是一个快速解决方案:
<强> YourActivity:强>
public void randomMethod(String val){
System.out.println(val);
}
<强>片段1:强>
onClick(){
((YourActivity) getActivity()).randomMethod(yourString1);
}
<强> Fragment2:强>
onClick(){
((YourActivity) getActivity()).randomMethod(yourString2);
}
您可以从活动的任何片段调用该方法并使用更新的值。
答案 2 :(得分:1)
由于所有片段都在视图分页器中,这意味着它们共享相同的活动。因此,只需将值放在intent中,并将其放入该视图分页器的任何片段中,无需在bundle中单独传递数据。
在Cust_Details_basic
片段中(将其另存为)
getActivity().getIntent().putExtra("name",fname);
在Cust_Details_address
片段中(得到它)
String fname = getActivity().getIntent().getStringExtra("name");
答案 3 :(得分:1)
在Cust_Details_address.java中我覆盖了这样的方法
@Override
public void onStart(){
super.onStart();
btnsubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String firstname = ((EditText)getActivity().findViewById(R.id.fname)).getText().toString();
Toast.makeText(getActivity().getApplicationContext(),"Hi "+firstname ,Toast.LENGTH_SHORT).show();
} });
我得到了我接受的结果。非常感谢'Talha'的帮助。