gpt4 book ai didi

android - 如何访问viewpager中 fragment 内edittext的值

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:20:20 26 4
gpt4 key购买 nike

我有一个 mainActivity,它由 View 寻呼机中的 3 个 fragment Activity 组成。每个 fragment 都由编辑文本组成。水平滑动 fragment 时如何获取编辑文本的值?我正在尝试这样做。下面的代码

public class MainActivity extends FragmentActivity  {

int value = 0;
Context context;
String a = "";

SectionsPagerAdapter mSectionsPagerAdapter;


ViewPager mViewPager;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.activity_main);

// Set up the action bar.
//final ActionBar actionBar = getActionBar();
// actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mAddButtonB = (Button) findViewById(R.id.addnewB);

mAddSearchV = findViewById(R.id.addnewSearch);
mAddButtonB.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
mAddSearchV.setVisibility(View.GONE);
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.customviewpager);
mViewPager.setAdapter(mSectionsPagerAdapter);


}
});
}



public class SectionsPagerAdapter extends FragmentPagerAdapter {

PageZero p0;
PageOne p1;
PageTwo p2;

public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
p0 = new PageZero();
p1 = new PageOne();
p2 = new PageTwo();
}

@Override
public Fragment getItem(int position) {


switch (position) {
case 0:
return p0;
case 1:
return p1;
case 2:
return p2;
}
return null;
}


@Override
public int getCount() {
// Show 3 total pages.
return 3;
}


}
// PageZero = (Fragment1)getFragmentManager().findFragmentByTag("frag1");
public class PageZero extends Fragment {
private EditText name;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentname, container,
false);
name=(EditText)view.findViewById(R.id.nameTV);

name.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();


}
});/*(new OnKeyListener() {

public boolean onKey(View v, int keyCode, KeyEvent event) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(), a,Toast.LENGTH_SHORT).show();
return false;
}
});*/
return view;
}
}

public class PageOne extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentaddress, container,
false);


return view;
}
}

public class PageTwo extends Fragment {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.componentmobno, container,
false);
return view;
}
}


}

当我尝试访问 pageZero 类中的 eddittext 时,如下所示

 name=(EditText)view.findViewById(R.id.nameTV);
a=name.getText().toString().trim();

我得到的是空值。请帮帮我。在此先感谢您。

最佳答案

像这样更改 pagezero 类 oncreateview 代码......

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view =getActivity().getLayoutInflater().inflate(R.layout.componentname,null);
name=(EditText)view.findViewById(R.id.nameTV);

name.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
a=name.getText().toString().trim();
Toast.makeText(getApplicationContext(),a,Toast.LENGTH_SHORT).show();


}

return view;
});

返回查看好友然后使用默认布局inflater...

关于android - 如何访问viewpager中 fragment 内edittext的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14497317/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com