需要帮助在一个标签片段中显示一个 Toast。

huangapple go评论83阅读模式
英文:

Need help getting a Toast to appear on a Tab Fragment

问题

我正在尝试在一个选项卡式的片段中当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 ViewGroup container, @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(), "请填写所有字段", Toast.LENGTH_SHORT).show();
                }
                else {
                    Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });

        return inflater.inflate(R.layout.fragment_signup, container, false);
    }
}
英文:

I'm trying to get a Toast to appear in a tabbed fragment when an EditText is left empty.

Here is my MainActivity's 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);        
}
}

My Tabbed Fragment works fine with this code:

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

But it doesn't work when creating the Toast like this:

public class SignupFragment extends Fragment {
EditText textFillCheck;
Button submitCheck;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @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, container, false);
}
}

答案1

得分: 1

你没有访问片段布局的视图,因此你在 onCreateView() 中返回的布局没有附加你的逻辑。你应该设置你的片段如下所示:

public class SignupFragment extends Fragment {

    EditText textFillCheck;
    Button submitCheck;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){

        // 存储对片段膨胀布局的引用
        View root = inflater.inflate(R.layout.fragment_signup, container, false);
        
        // 使用引用来访问片段的视图
        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(), "请填写所有字段", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
                }
            }
        });

        // 返回膨胀的布局
        return root;
    }
}
英文:

You're not accessing the views of your Fragment's layout so the layout you're returning in onCreateView() doesn't have your logic attached to it. You should set up your Fragment to look something like this:

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

答案2

得分: 0

我很惊讶为什么你的代码没有出现任何错误,比如**VIEW DOES NOT EXISTNULL POINTER EXCEPTION**,因为

首先,你在显示Toast之前更改了活动(Activity),其次在初始化视图之后才填充布局,而且没有正确地获取上下文(Context),正确的做法是使用**OnViewCreated()**,像这样:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

    submitCheck = (Button) view.findViewById(R.id.signupBtn);

    textFillCheck = (EditText) view.findViewById(R.id.signupFirstName);

    submitCheck.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            if (TextUtils.isEmpty(textFillCheck.getText().toString())) {
                Toast.makeText(getActivity(), "请填写所有字段", Toast.LENGTH_SHORT).show();

                Intent intent = new Intent(getActivity(), SignupFragment.class);
                startActivity(intent);
            } else {
                Toast.makeText(getActivity(), textFillCheck.getText().toString(), Toast.LENGTH_LONG).show();
            }
        }
    });
}
英文:

I am amazed why your code is not having any error like VIEW DOES NOT EXIST or NULL POINTER EXCEPTION because

First, you change the activity before showing the Toast secondly you are inflating the layout after the initialization of views and not getting the context properly, Right way of doing what you are trying to achieve is using OnViewCreated() like this

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
submitCheck = (Button) view.findViewById(R.id.signupBtn);
textFillCheck = (EditText) view.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();
Intent intent = new Intent(getActivity(), SignupFragment.class);
startActivity(intent);
}
else{
Toast.makeText(getActivity(), textFillCheck.getText().toString(), 
Toast.LENGTH_LONG).show();
}
}
});
}

huangapple
  • 本文由 发表于 2020年9月26日 05:20:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64071439.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定