如何解决需要帮助以使Toast出现在选项卡片段上
当一个EditText留空时,我试图让Toast出现在选项卡式片段中。
这是我的MainActivity的onCreate:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SectionsPagerAdapter sectionsPagerAdapter = new SectionsPagerAdapter(this,getSupportFragmentManager());
ViewPager viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(sectionsPagerAdapter);
TabLayout tabs = findViewById(R.id.tabs);
tabs.setupWithViewPager(viewPager);
}
}
我的选项卡式片段可以在以下代码中正常工作:
public class SignupFragment extends Fragment {
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_signup,container,false);
}
}
但是当这样创建Toast时,它不起作用:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable Bundle savedInstanceState) {
submitCheck = (Button) submitCheck.findViewById(R.id.signupBtn);
textFillCheck = (EditText) textFillCheck.findViewById(R.id.signupFirstName);
submitCheck.setonClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
Intent intent = new Intent(getActivity(),SignupFragment.class);
startActivity(intent);
Toast.makeText(getActivity(),"Please fill in all fields",Toast.LENGTH_SHORT).show();
}
else{Toast.makeText(getActivity(),textFillCheck.getText().toString(),Toast.LENGTH_LONG).show();
}
}
});
return inflater.inflate(R.layout.fragment_signup,false);
}
}
解决方法
您没有访问Fragment布局的视图,因此您在onCreateView()
中返回的布局没有附加逻辑。您应该将片段设置为如下所示:
public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater,@Nullable ViewGroup container,@Nullable Bundle savedInstanceState){
// Store a reference to your Fragment's inflated layout
View root = inflater.inflate(R.layout.fragment_signup,container,false);
// Use the reference to access your Fragment's views
submitCheck = (Button) root.findViewById(R.id.signupBtn);
textFillCheck = (EditText) root.findViewById(R.id.signupFirstName);
submitCheck.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if(TextUtils.isEmpty(textFillCheck.getText().toString())) {
Toast.makeText(getActivity(),"Please fill in all fields",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(getActivity(),textFillCheck.getText().toString(),Toast.LENGTH_LONG).show();
}
}
});
// Return the inflated layout
return root;
}
}
,
我很惊讶为什么您的代码没有出现 true
或 rendevous
之类的错误,因为
首先,您需要在显示Toast之前更改活动,其次,在初始化视图之后又要夸大布局,并且无法正确获取上下文,要实现的正确方法是使用 {{1} } 这样
VIEW DOES NOT EXIST
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。