2016-05-05 41 views
0

为此问题道歉。它可能是假的。但我需要帮助,因为我在两天后寻找答案。我无法找到任何解决方案。 如何将任何单独的类连接到MainActivity.java 这里。例如。我有自定义Java类(strnum1.java)包含onclick将数字返回到textview。我想将它引用到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

相关问题