从片段跳转到活动,使用按钮。Android Studio

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

Going from fragment to activity with a button. Android Studio

问题

以下是您提供的内容的中文翻译:

我正在尝试在Android Studio中实现下面的代码,但它不起作用。
我想要通过一个按钮从一个Fragment(GalleryFragment)跳转到一个Activity(postropa)。
我已经将按钮与函数(BotonPulsado)关联起来,但我不知道在设计视图中出了什么问题。

设计视图

代码:

import (...)

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        final TextView textView = root.findViewById(R.id.text_gallery);
        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    public void BotonPulsado(View view) {
        Intent intent = new Intent(getContext(), postropa.class);
        startActivity(intent);
    }
}
英文:

I'm trying to implent the next code in Android Studio and it does not work.
I want to pass from a Fragment (GalleryFragment) to an Activity (postropa) with a button.
I have linked the botton with the function (BotonPulsado) and I don't know what is wrong (In the design view).

Design View

Code:

import (...)

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        final TextView textView = root.findViewById(R.id.text_gallery);
        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        return root;
    }

    public void BotonPulsado(View view) {
        Intent intent = new Intent(getContext(), postropa.class);
        startActivity(intent);
    }
}

答案1

得分: 0

你应该将 Button 变量创建为类字段。

private GalleryViewModel galleryViewModel;
Button button; // <<-------

然后你需要在 `onCreateView()` 方法中定义它

```java
button = (Button) findViewById(R.id.button2);

并在这个按钮上设置 `onClickListener()` 来处理调用在这里你必须调用启动活动的方法

```java
button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            BotonPulsado();
        }
    });

你的最终代码

```java
import (...)

public class GalleryFragment extends Fragment {

    private GalleryViewModel galleryViewModel;
    Button button;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {
        galleryViewModel =
                ViewModelProviders.of(this).get(GalleryViewModel.class);
        View root = inflater.inflate(R.layout.fragment_gallery, container, false);
        final TextView textView = root.findViewById(R.id.text_gallery);
        galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
            @Override
            public void onChanged(@Nullable String s) {
                textView.setText(s);
            }
        });
        button = (Button) findViewById(R.id.button2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                BotonPulsado();
            }
        });
        return root;
    }

    public void BotonPulsado() {
        Intent intent = new Intent(getContext(), postropa.class);
        startActivity(intent);
    }
}
英文:

You should create Button variаble as a class field.

private GalleryViewModel galleryViewModel;
Button button; &lt;&lt;-------

After that you need to define it in method onCreateView()

 button = (Button) findViewById(R.id.button2);

And set onClickListener() on this button to handle the call.
There you must call the method that starts the activity.

button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BotonPulsado();
}
});

Your final code:

import (...)
public class GalleryFragment extends Fragment {
private GalleryViewModel galleryViewModel;
Button button;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
galleryViewModel =
ViewModelProviders.of(this).get(GalleryViewModel.class);
View root = inflater.inflate(R.layout.fragment_gallery, container, false);
final TextView textView = root.findViewById(R.id.text_gallery);
galleryViewModel.getText().observe(getViewLifecycleOwner(), new Observer&lt;String&gt;() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
button = (Button) findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
BotonPulsado();
}
});
return root;
}
public void BotonPulsado() {
Intent intent = new Intent(getContext(), postropa.class);
startActivity(intent);
}
}

huangapple
  • 本文由 发表于 2020年5月5日 01:49:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/61598503.html
匿名

发表评论

匿名网友

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

确定