0

私は 3 つのボタンを含む単純なアプリケーションを作成しています。各ボタンには、ユーザー入力を受け取り、結果を toast に表示するよりも数学計算を行うリスナーがあります。

しかし、3 番目のボタンは、intent を使用して 2 番目のアクティビティの結果を表示します。

最初のボタンは正常に機能しますが、2 番目と 3 番目のボタンは機能しません。2 番目はトーストを表示せず、3 番目は 2 番目のアクティビティの結果を表示しないことを意味します。

誰でも私を助けることができますか???

MainActivity.java

package com.tipcalc.assignment1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener {

    EditText txt;
    Button btn1, btn2, btn3;

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

        txt = (EditText) findViewById(R.id.edittxt);
        btn1 = (Button) findViewById(R.id.button1);
        btn2 = (Button) findViewById(R.id.button2);
        btn3 = (Button) findViewById(R.id.button3);

        btn1.setOnClickListener(this);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

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

        String input;
        String result = null;
        Double dbl;

        if (arg0 == btn1) {
            input = txt.getText().toString();
            dbl = Double.parseDouble(input) * 0.1;

            result = String.valueOf(dbl);


        } else if (arg0 == btn2) {

            input = txt.getText().toString();
            dbl = Double.parseDouble(input) * 0.15;

            result = String.valueOf(dbl);

        }
            Toast.makeText(this, result, Toast.LENGTH_LONG).show();
        if(arg0 == btn3){

            Intent intent = new Intent(MainActivity.this, SecondPage.class);
            intent.putExtra("com.tipcalc.assignment1.showResult", result);
            startActivity(intent);

        }

    }

}

SecondActivity.java

package com.tipcalc.assignment1;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.TextView;

public class SecondPage extends Activity {

    TextView txt_result;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second_page);

        txt_result = (TextView) findViewById(R.id.txtResult);

        Bundle extras = getIntent().getExtras();
        if (extras != null) {
            String value = extras
                    .getString("com.tipcalc.assignment1.showResult");
            txt_result.setText(value);

        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.second_page, menu);
        return true;
    }

}
4

1 に答える 1

1

現在、最初のボタンにのみ OnClickListener を設定しています。これを変える:

    btn1.setOnClickListener(this);

これに:

    btn1.setOnClickListener(this);
    btn2.setOnClickListener(this);
    btn3.setOnClickListener(this);

UPDATE(コメントに基づく)

そのブロックで値を処理していないため、btn3 を押しても結果は次のアクティビティに送信されません。arg0 == btn3 の場合に onClick ハンドラーのロジックに従うと、結果が設定されないことがわかります。目的や期待される結果はわかりませんが、btn1 をクリックすると 1 つの値が計算され、btn2 をクリックすると別の値が計算されますが、btn3 をクリックすると何も計算されません。

于 2013-11-18T23:58:55.020 に答える