【问题标题】:Refer Custom Class to MainActivity.java将自定义类引用到 MainActivity.java
【发布时间】:2016-05-05 07:47:14
【问题描述】:

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

关于如何解决这部分代码的任何帮助。

【问题讨论】:

  • “连接”是什么意思?为什么你需要一个 Activity 来处理按钮按下?
  • 连接在某种意义上将“自定义类”引用到主活动。因此,当程序运行时,它应该将各个类活动检索到主要活动。如我错了请纠正我。希望你能明白。
  • 我需要在 Main Activity 的上下文中写任何内容到单个类吗?。
  • 对不起,我只是不明白。你有你的 MainActivity 和它的 activity_main 布局。在该布局中,您有一个按钮,并且您想在按下它时执行某些操作。然后只需在 MainActivity 中添加侦听器,为什么需要“自定义类”?如果你想在一个单独的类中处理一些额外的逻辑,那很好,但是那个类不需要是一个活动。
  • 好的。我做了一些改变。如果你能证明它是否正确,我会很高兴。

标签: android class main-activity


【解决方案1】:
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;
        }
    }
  }

【讨论】:

  • 我对实际的 Main Activity 进行了更改。
  • 还有一个查询。我可以对“sin”、“cos”、“rad”、“hyp”等运算符做同样的事情吗?还是应该以其他方式做。也许其他类的东西。请给我建议。
猜你喜欢
  • 2012-03-29
  • 1970-01-01
  • 2015-06-23
  • 2012-01-25
  • 2013-11-30
  • 2021-04-27
  • 2017-08-31
  • 2011-01-17
  • 1970-01-01
相关资源
最近更新 更多