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