2016-05-05 44 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

相關問題