2016-05-05 4 views
0

この質問をお詫び申し上げます。それは偽のかもしれない。私は2日後からの答えを探しているので、私は助けが必要です。私は解決策を見つけることができません。 個々のクラスをMainActivity.javaに接続する方法 ここにあります。例えば。私はカスタムJavaクラス(strnum1.java)をonclickを含むテキストビューに数値を返します。私はMainActivity.javaを参照してプログラムを実行したいと思います。MainActivity.javaにカスタムクラスを参照

Strnum1.java(カスタムクラス)

package com.marsh.calculator.calculator; 

import android.app.Activity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.view.View.OnClickListener; 
import android.widget.TextView; 

public class Strnum1 extends Activity implements View.OnClickListener { 

    View rootview; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button b = (Button) findViewById(R.id.btnOne); 

     b.setOnClickListener(this); 
    } 

    @Override 
    public void onClick(View v) { 
     switch (v.getId()) 
     { 
      case R.id.btnsin: 
       ((TextView)findViewById(R.id.txtScreen)).setText("1"); 
       break; 
     } 
    } 
} 

私はこのコードの一部を解決できる方法上の任意の助け。

+2

「接続する」とはどういう意味ですか?なぜあなたはボタン・プレスを扱うだけのアクティビティが必要ですか? – Onheiron

+0

は、「カスタムクラス」をメインアクティビティに参照する意味で接続します。プログラムが実行されると、個々のクラスアクティビティをメインアクティビティに取得する必要があります。私が間違っている場合は私を修正してください。あなたがそれを得ることを望みます。 – Marsh

+0

私はメインアクティビティの中で個々のクラスに関連して何かを書く必要がありますか? – Marsh

答えて

0
package com.marsh.calculator.calculator; 

import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends AppCompatActivity implements View.OnClickListener { 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     Button bzero = (Button) findViewById(R.id.btnOne); 
     Button bone = (Button) findViewById(R.id.btnZero); 
     Button btwo = (Button) findViewById(R.id.btnTwo); 
     Button bthree = (Button) findViewById(R.id.btnThree); 
     Button bfour = (Button) findViewById(R.id.btnFour); 
     Button bfive = (Button) findViewById(R.id.btnFive); 
     Button bsix = (Button) findViewById(R.id.btnSix); 
     Button bseven = (Button) findViewById(R.id.btnSeven); 
     Button beight = (Button) findViewById(R.id.btnEight); 
     Button bnine = (Button) findViewById(R.id.btnNine); 
     bzero.setOnClickListener(this); 
     bone.setOnClickListener(this); 
     btwo.setOnClickListener(this); 
     bthree.setOnClickListener(this); 
     bfour.setOnClickListener(this); 
     bfive.setOnClickListener(this); 
     bsix.setOnClickListener(this); 
     bseven.setOnClickListener(this); 
     beight.setOnClickListener(this); 
     bnine.setOnClickListener(this); 
    } 
    @Override 
    public void onClick(View v) { 
     switch (v.getId()) { 
      case R.id.btnZero: 
       ((TextView) findViewById(R.id.txtScreen)).setText("0"); 
       break; 
      case R.id.btnOne: 
       ((TextView) findViewById(R.id.txtScreen)).setText("1"); 
       break; 
      case R.id.btnTwo: 
       ((TextView) findViewById(R.id.txtScreen)).setText("2"); 
       break; 
      case R.id.btnThree: 
       ((TextView) findViewById(R.id.txtScreen)).setText("3"); 
       break; 
      case R.id.btnFour: 
       ((TextView) findViewById(R.id.txtScreen)).setText("4"); 
       break; 
      case R.id.btnFive: 
       ((TextView) findViewById(R.id.txtScreen)).setText("5"); 
       break; 
      case R.id.btnSix: 
       ((TextView) findViewById(R.id.txtScreen)).setText("6"); 
       break; 
      case R.id.btnSeven: 
       ((TextView) findViewById(R.id.txtScreen)).setText("7"); 
       break; 
      case R.id.btnEight: 
       ((TextView) findViewById(R.id.txtScreen)).setText("8"); 
       break; 
      case R.id.btnNine: 
       ((TextView) findViewById(R.id.txtScreen)).setText("9"); 
       break; 
     } 
    } 
    } 
+0

実際のメインアクティビティに変更を加えました。もう一度クエリ – Marsh

+0

。 「sin」、「cos」、「rad」、「hyp」などのような演算子でも同じことをすることができますか他の方法で行うべきですか?多分他のクラスのもの。私を提案してください。 – Marsh

関連する問題