【问题标题】:how do i print the output from another class in android?如何在 android 中打印另一个类的输出?
【发布时间】:2016-04-23 15:06:34
【问题描述】:

我正在做这个项目。当用户在微调器中选择位置并单击下一步时,它将显示来自另一个类的结果。但是当我跑步时,它什么也没做。事实上,这个按钮也没有任何作用。我不知道是什么问题。如果你们能帮助我,那就太好了。谢谢。这是我的源代码。

这是包含微调器的片段

public class LetsGoFragment extends Fragment implements View.OnClickListener{

//constructor
public static LetsGoFragment newInstance(){
    LetsGoFragment fragment = new LetsGoFragment();
    return fragment;
}

public LetsGoFragment(){
}

//variable to connect
Spinner spinnerTo;
Spinner spinnerFrom;

Button button;

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    //connecting the  fragment to layout
    View rootView = inflater.inflate(R.layout.fragment_lets_go, container, false);

    //spinner and button
    spinnerTo = (Spinner) rootView.findViewById(R.id.spinnerTo);
    String textTo = spinnerTo.getSelectedItem().toString();

    spinnerFrom = (Spinner) rootView.findViewById(R.id.spinnerFrom);
    String textFrom = spinnerTo.getSelectedItem().toString();

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

    return rootView;


}


@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



}

private void setContentView(int activity_main) {
}


@Override
public void onClick(View v) {

    if(spinnerTo.getSelectedItem().equals("Masjid Jamek")&& spinnerFrom.getSelectedItem().equals("Bukit Bintang")){
        MasjidJamekToBukitBintang.class.toString();
    }


}

}

这是我想调用的类

public class MasjidJamekToBukitBintang extends  LetsGoFragment{

private TextView textViewResult;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContectView(R.layout.fragment_result);

    //route 1
    double MJtoPR = 1.20;
    double PRtoHT = 1.20;
    double HTtoIM = 1.20;
    double IMtoBB = 1.20;
    double route1 = MJtoPR + PRtoHT + HTtoIM + IMtoBB;

    //route 2
    double MJtoDW = 1.00;
    double DWtoBN = 0.00;
    double BNtoRC = 1.20;
    double RCtoBB = 1.20;
    double route2 = MJtoDW + DWtoBN + BNtoRC + RCtoBB;

    //route 3
    double MJtoPS = 1.00;
    double PStoKLS = 1.00;
    double KLStoTS = 1.20;
    double TStoML = 1.20;
    double MLtoHT = 1.20;
    double HTtoIM1 = 1.20;
    double IMtoBB1 = 1.20;
    double route3 = MJtoPS + PStoKLS + KLStoTS + TStoML + MLtoHT + HTtoIM1 + IMtoBB1;

    //shortest path
    ArrayList al = new ArrayList();

    al.add(new Double(route1));
    al.add(new Double(route2));
    al.add(new Double(route3));

    System.out.println("Route choices :");
    System.out.println("Route 1 : Masjid Jamek - Plaza Rakyat - Hang Tuah - Imbi - Bukit Bintang");
    System.out.println("Fare : RM" + route1);
    System.out.println("\nRoute 2 : Masjid Jamek - Dang Wangi - Bukit Nanas - Raja Chulan - Bukit Bintang");
    System.out.println("Fare : RM" + route2);
    System.out.println("\nRoute 3 : Masjid Jamek - Pasar Seni - KL Sentral - Tun Sambanthan - Maharaja Lela - Hang Tuah - Imbi - Bukit Bintang");
    System.out.println("Fare : RM" + route3);
    System.out.println("\nSaving cost route : RM" + Collections.min(al));

}

private void setContectView(int fragment_result) {
}

}

【问题讨论】:

    标签: java android class fragment


    【解决方案1】:

    您没有在button 上设置onClickListener

    您需要设置onClickListener 以使button 在“单击”时起作用。

    您需要执行以下操作:

    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Put here the code you want to run everytime the button is clicked
        }
    }
    

    【讨论】:

    • 我确实为片段实现了 onClickListener。 onClick 方法也在那里,但按钮不起作用。这是为什么呢?
    • 你必须将onClickListener设置为按钮,你没有设置它。
    猜你喜欢
    • 2023-03-06
    • 1970-01-01
    • 2017-05-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-10
    • 1970-01-01
    相关资源
    最近更新 更多