0

我在 Android 中遇到一个问题,我的按钮在片段中没有响应。这是 MainFragment.java 代码:

package com.example.currencyconverter;


import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.DecimalFormat;

import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.TextView;


public class MainFragment extends Fragment implements OnClickListener {

ProgressDialog progressBar;
EditText edittextdollars;
EditText edittexteuros;
TextView dollars;
TextView euros;
RadioButton dtoe;
RadioButton etod;
RadioGroup radiogroup;
Spinner spinner1;
Spinner spinner2;
Button calculate;

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

}
private void addListenerOnButton() {
    spinner1 = (Spinner)getView().findViewById(R.id.spinner1);
    spinner2 = (Spinner)getView().findViewById(R.id.spinner2);
    calculate = (Button)getView().findViewById(R.id.calculate);
    edittextdollars = (EditText)getView().findViewById(R.id.edittextdollars);
    edittexteuros = (EditText)getView().findViewById(R.id.edittexteuros);
    calculate.setOnClickListener(myhandler1);

}

public interface CurrencyConverter {
    public double convert(String currencyFrom, String currencyTo) throws Exception;
}

public class YahooCurrencyConverter implements CurrencyConverter{
     public double convert(String currencyFrom, String currencyTo) throws IOException {
            HttpClient httpclient = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet("http://quote.yahoo.com/d/quotes.csv?s=" + currencyFrom + currencyTo + "=X&f=l1&e=.csv");
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httpGet, responseHandler);
            httpclient.getConnectionManager().shutdown();
            return Double.parseDouble(responseBody);
}


}
 View.OnClickListener myhandler1 = new View.OnClickListener() {
    public void onClick(View v) {
        String text1 = spinner1.getSelectedItem().toString().trim();
        String text2 = spinner2.getSelectedItem().toString().trim();


        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("USD", "EUR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("EUR","USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("EUR","USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("USD","EUR");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertEurostoEuros();
        }
        if (text1.equals("Euro - EUR") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            OppositeOfConvertEurostoEuros();
        }
        if (text1.equals("US Dollar - USD") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertDollarstoDollars();
        }
        if (text1.equals("US Dollar - USD") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            OppositeOfConvertDollarstoDollars();
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertEurostoEuros();
        }          
        if (text1.equals("Indian Rupee - INR") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            OppositeOfConvertDollarstoDollars();
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("INR","USD");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("USD","INR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("INR","EUR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("EUR","INR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("USD","INR");
        }
        if (text1.equals("US Dollar - USD") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("INR","USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("INR","EUR");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("EUR","INR");
        }



        if (text1.equals("Japanese Yen - JPY") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("JPY","JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("JPY","JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("US Dollar - USD") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("JPY","USD");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("US Dollar - USD") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("USD","JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Indian Rupee - INR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("JPY","INR");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Indian Rupee - INR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("INR","JPY");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Euro - EUR") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("JPY","EUR");
        }
        if (text1.equals("Japanese Yen - JPY") && text2.equals("Euro - EUR") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("EUR","JPY");
        }



        if (text1.equals("US Dollar - USD") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("USD","JPY");
        } 
        if (text1.equals("US Dollar - USD") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("JPY","USD");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("EUR","JPY");
        }
        if (text1.equals("Euro - EUR") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("JPY","EUR");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Japanese Yen - JPY") && edittextdollars.length() > 0 && edittexteuros.length()==0) {
            convertvalues("INR","JPY");
        }
        if (text1.equals("Indian Rupee - INR") && text2.equals("Japanese Yen - JPY") && edittexteuros.length() > 0 && edittextdollars.length()==0) {
            convertvalues2("JPY","INR");
        }

        else {
            System.out.println("");
        }
    }                    
                public String convertvalues(String convertfrom, String convertto) {
                    double current;
                    double val = Double.parseDouble(edittextdollars.getText().toString());
                    DecimalFormat df = new DecimalFormat(".##");
                     YahooCurrencyConverter ycc = new YahooCurrencyConverter();                  
                         try {
                            current = ycc.convert(convertfrom, convertto);
                            edittexteuros.setText(df.format(val*current)); 
                            return "passed";
                            }
                        catch (Exception e) {
                            StringWriter errors = new StringWriter();
                            e.printStackTrace(new PrintWriter(errors));
                            return errors.toString();
                        } 
    }
                public String convertvalues2(String convertfrom2, String convertto2) {
                    double current;
                    double val = Double.parseDouble(edittexteuros.getText().toString());
                    DecimalFormat df = new DecimalFormat(".##");
                     YahooCurrencyConverter ycc = new YahooCurrencyConverter();                  
                         try {
                            current = ycc.convert(convertfrom2, convertto2);
                            edittextdollars.setText(df.format(val*current)); 
                            return "passed";
                            }
                        catch (Exception e) {
                            StringWriter errors = new StringWriter();
                            e.printStackTrace(new PrintWriter(errors));
                            return errors.toString();
                        } 

                }

protected void convertEurostoDollars() {
    double val = Double.parseDouble(edittexteuros.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittextdollars.setText(df.format(val*1.33));

    }

protected void convertEurostoEuros() {
    double val = Double.parseDouble(edittextdollars.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittexteuros.setText(df.format(val*1));
}

protected void convertDollarstoDollars() {
    double val = Double.parseDouble(edittextdollars.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittexteuros.setText(df.format(val*1));
}

protected void OppositeOfConvertEurostoDollars() {
    double val = Double.parseDouble(edittextdollars.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittexteuros.setText(df.format(val*1.33));
}
protected void OppositeOfConvertDollarstoEuros() {
    double val = Double.parseDouble(edittexteuros.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittextdollars.setText(df.format(val*0.75));
}
protected void OppositeOfConvertEurostoEuros() {
    double val = Double.parseDouble(edittexteuros.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittextdollars.setText(df.format(val*1));
}
protected void OppositeOfConvertDollarstoDollars() {
    double val = Double.parseDouble(edittexteuros.getText().toString());
    DecimalFormat df = new DecimalFormat(".##");
    edittextdollars.setText(df.format(val*1));
}




};


@Override
public void onClick(View v) {
    // TODO Auto-generated method stub

}
}

这是我的按钮的 xml 部分:

<Button
android:id="@+id/calculate"
android:layout_width="320dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edittexteuros"
android:text="@string/calculate" />

这里发生了什么?我的 LogCat 没有显示错误。任何帮助是极大的赞赏。

4

1 回答 1

0

覆盖 onResume()并调用addListenerOnButton()inside。

于 2013-06-30T03:35:37.183 回答