Button 是否可以在片段中添加单击按钮方法?

Button 是否可以在片段中添加单击按钮方法?,button,fragment,onclicklistener,buttonclick,Button,Fragment,Onclicklistener,Buttonclick,我在xml文件中添加了一个按钮。现在我想在片段中打开它。。 我试图在点击按钮时显示股票期权,但未发现VIEWBYID工作,也未在运行后点击按钮时出现任何情况。。 可能这是共享的代码。。我很困惑,请帮忙 我已经尽了最大的努力。任何帮助都将不胜感激。感谢所有的专家 (XML文件) 主要活动: <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com

我在xml文件中添加了一个按钮。现在我想在片段中打开它。。 我试图在点击按钮时显示股票期权,但未发现VIEWBYID工作,也未在运行后点击按钮时出现任何情况。。 可能这是共享的代码。。我很困惑,请帮忙

我已经尽了最大的努力。任何帮助都将不胜感激。感谢所有的专家

(XML文件)

主要活动:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <FrameLayout
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@id/bottom_navigation"
        />

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_navigation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        app:itemIconTint="@color/selector_item_gray_color"
        app:itemTextColor="@color/selector_item_gray_color"
        android:background="#302E2D"
        app:menu="@menu/bottom_navigation_android" />



</RelativeLayout>  
更多:

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class More_Fragment extends Fragment {

    Button btn;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, container,false);}





}
甚至在Fragment\u More中尝试了此代码,但失败了

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class More_Fragment extends Fragment {

    private String a = "";
    private String b = "";

    private LinearLayout linear1;
    private Button btn_share;
    private TextView textview6;

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, container,false);}

    private void initialize(Bundle _savedInstanceState) {

        linear1 = (LinearLayout) getActivity().findViewById(R.id.linear1);
        btn_share = (Button) getActivity().findViewById(R.id.btn_share);
        textview6 = (TextView) getActivity().findViewById(R.id.textview6);

        btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View _view) {
                a = textview6.getText().toString();
                b = textview6.getText().toString();
                Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_SUBJECT, a); i.putExtra(android.content.Intent.EXTRA_TEXT, b); startActivity(Intent.createChooser(i,"Share using"));
            }
        });
    }
    private void initializeLogic() { textview6.setVisibility(View.GONE);
    }

}

要在片段中引用按钮,请在onCreateView中执行此操作:

public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_more, container,false);
     btn_share = (Button) rootView.findViewById(R.id.btn_share);
     btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View _view) {
                a = textview6.getText().toString();
                b = textview6.getText().toString();
                Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_SUBJECT, a); i.putExtra(android.content.Intent.EXTRA_TEXT, b); startActivity(Intent.createChooser(i,"Share using"));
            }
        });
     return rootView;
}

就是这样做的谢谢所有专家

Fragment\u More.java

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class More_Fragment extends Fragment{

    private String a = "";
    private String b = "";

    private LinearLayout linear1;
    private Button btn_sharethis;
    private TextView text;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, container,false);
    }

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

        btn_sharethis = (Button)view.findViewById(R.id.btn_shareme);
        text = (TextView)view.findViewById(R.id.textview61);

        btn_sharethis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = text.getText().toString();
                b = text.getText().toString();
                Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_SUBJECT, a); i.putExtra(android.content.Intent.EXTRA_TEXT, b); startActivity(Intent.createChooser(i,"Share using"));
            }
        });
    }
}

谢谢你的帮助,至少碎片正在打开,但当我点击按钮时..我卡住了,请帮助我。日志上怎么说?我的解决方案不完整,只是举例说明了按钮初始化。在onClick中,单击引用textview6的示例。您需要以相同的方式初始化它,因此
textview6=(TextView)rootView.findViewById(R.id.textview6)哦,很好,先生..非常感谢。。完成了谢谢你给我时间我就是这样做的。。
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
     View rootView = inflater.inflate(R.layout.fragment_more, container,false);
     btn_share = (Button) rootView.findViewById(R.id.btn_share);
     btn_share.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View _view) {
                a = textview6.getText().toString();
                b = textview6.getText().toString();
                Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_SUBJECT, a); i.putExtra(android.content.Intent.EXTRA_TEXT, b); startActivity(Intent.createChooser(i,"Share using"));
            }
        });
     return rootView;
}
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public class More_Fragment extends Fragment{

    private String a = "";
    private String b = "";

    private LinearLayout linear1;
    private Button btn_sharethis;
    private TextView text;


    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_more, container,false);
    }

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

        btn_sharethis = (Button)view.findViewById(R.id.btn_shareme);
        text = (TextView)view.findViewById(R.id.textview61);

        btn_sharethis.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                a = text.getText().toString();
                b = text.getText().toString();
                Intent i = new Intent(android.content.Intent.ACTION_SEND); i.setType("text/plain"); i.putExtra(android.content.Intent.EXTRA_SUBJECT, a); i.putExtra(android.content.Intent.EXTRA_TEXT, b); startActivity(Intent.createChooser(i,"Share using"));
            }
        });
    }
}