2017-07-07 21 views
0

這是啓動時的MainActivity。用戶輸入一個數字,然後單擊該按鈕,Intent將他們帶到他們輸入的數字的滑動視圖中。將EditText輸入從活動移動到滑動視圖的片段

public class MainActivity extends AppCompatActivity{ 
String number; 
Button continueButton; 
EditText NumberET; 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    continueButton = (Button) findViewById(R.id.ContinueButton); 
    NumberET = (EditText) findViewById(R.id.etTotalAmount); 
    continueButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      number = NumberET.getText().toString(); 
      if (number.equals("")) { 
       Toast.makeText(getApplicationContext(), "Enter Number", Toast.LENGTH_SHORT).show(); 
      } else { 
       Bundle bundle = new Bundle(); 
       bundle.putString("AMOUNT_KEY", number); 
       FixedCosts fragment = new FixedCosts(); 
       fragment.setArguments(bundle); 
       FragmentTransaction t = getSupportFragmentManager().beginTransaction(); 
       t.replace(R.id.container, fragment).commit(); 
       Intent intent = new Intent(MainActivity.this, FragmentActivity.class); 
       intent.putExtra("NUMBER", number); 
       startActivity(intent); 
      } 
     } 
    }); 
} 

在片段中,我想設置文本視圖的文本來顯示該數字。我無法將此號碼從此Activity類發送到Fragment。我怎樣才能解決這個問題?

片段:

ublic class FixedCosts extends Fragment { 
String amount; 
@Nullable 
@Override 
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { 
    View view = inflater.inflate(R.layout.fixed_costs_fragment, container, false); 
    amount = this.getArguments().getString("AMOUNT_KEY"); 


    TextView TitletextView = (TextView) view.findViewById(R.id.amountTitle); 
    TitletextView.setText(amount); 
    return view;}} 

下面是設置滑動視圖FragmentActivity:

public class FragmentActivity extends AppCompatActivity { 
public static final String MY_CUSTOM_FRAGMENT_KEY = "I_GOT_THE_KEYS"; 

String MoneyAmount; 

/** 
* The {@link android.support.v4.view.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 android.support.v4.app.FragmentStatePagerAdapter}. 
*/ 
private SectionsPagerAdapter mSectionsPagerAdapter; 

/** 
* The {@link ViewPager} that will host the section contents. 
*/ 
private ViewPager mViewPager; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fragment); 

    /*MoneyAmount = getIntent().getExtras().getString("AmountMoney");*/ 

    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); 

} 
private Fragment createCustomFragment(){ 
    Bundle bundle = new Bundle(); 
    bundle.putString(MY_CUSTOM_FRAGMENT_KEY, MoneyAmount); 

    PlaceholderFragment placeholderFragment = new PlaceholderFragment(); 
    placeholderFragment.setArguments(bundle); 
    return placeholderFragment; 
} 


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

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment { 
    /** 
    * The fragment argument representing the section number for this 
    * fragment. 
    */ 
    private static final String ARG_SECTION_NUMBER = "section_number"; 

    public PlaceholderFragment() { 
    } 

    /** 
    * Returns a new instance of this fragment for the given section 
    * number. 
    */ 
    public static PlaceholderFragment newInstance(int sectionNumber, String MoneyAmount) { 
     PlaceholderFragment fragment = new PlaceholderFragment(); 
     Bundle args = new Bundle(); 
     args.putInt(ARG_SECTION_NUMBER, sectionNumber); 
     args.putString(MY_CUSTOM_FRAGMENT_KEY, MoneyAmount); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_main, container, false); 
     Bundle bundle = getArguments(); 
     /*String amount = bundle.getString(MY_CUSTOM_FRAGMENT_KEY); 
     TextView TitletextView = (TextView) FixedCosts.class.findViewById(R.id.amountTitle); 
     TitletextView.setText(amount);*/ 

     TextView textView = (TextView) rootView.findViewById(R.id.section_label); 
     textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER))); 
     return rootView; 
    } 
} 

/** 
* 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: 
       FixedCosts fixedCosts = new FixedCosts(); 

       return fixedCosts; 
     } 
     return PlaceholderFragment.newInstance(position + 1, MoneyAmount); 
    } 
    @Override 
    public int getCount() { 
     // Show 3 total pages. 
     return 3; 
    } 
    @Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
      case 0: 
       return "Fixed Costs"; 
      case 1: 
       return "Long term investments"; 
      case 2: 
       return "SECTION 3"; 
     } 
     return null; 
    } 
} 

}

回答

0

拆下活動起始碼在continueButton.setOnClickListener聽者和在相同的佈局爲母體添加view container活性不在片段活性中:

public class MainActivity extends AppCompatActivity{ 
String number; 
Button continueButton; 
EditText NumberET; 

@Override 
public void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    continueButton = (Button) findViewById(R.id.ContinueButton); 
    NumberET = (EditText) findViewById(R.id.etTotalAmount); 
    continueButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      number = NumberET.getText().toString(); 
      if (amount.equals("")) { 
       Toast.makeText(getApplicationContext(), "Enter Number", Toast.LENGTH_SHORT).show(); 
      } else { 
       Bundle bundle = new Bundle(); 
       bundle.putString("AMOUNT_KEY", number); 
       FixedCosts fragment = new FixedCosts(); 
       fragment.setArguments(bundle); 
       FragmentTransaction t = getSupportFragmentManager().beginTransaction(); 
       t.replace(R.id.container, fragment).commit(); 
      } 
     } 
    }); 
} 

希望它能幫助你。

+0

我加入的意向開始什麼的代碼。我想我不應該把這件事留下。我的錯。 – Ryan

+0

你的主要問題是什麼?你現在想要什麼? –

+0

我已經能夠將數據發送到FragmentActivity.class中的onCreateView類。我有辦法將它從那裏發送到我的片段課程? @Chetan Pushpad – Ryan

0

我通過將值保存到SharedPreferences中解決了問題。
在MainActivity.class:

SharedPreferences saveAmount = getSharedPreferences("MyPrefs", 0); 
    final SharedPreferences.Editor editor = saveAmount.edit(); 
editor.putString("AMOUNT", amount); 
       editor.commit(); 

片段中的類:

SharedPreferences sharedPrefs = this.getActivity().getSharedPreferences("MyPrefs", 0); 

    String amount = (sharedPrefs.getString("AMOUNT", "Ummmmm")); 
相關問題