【问题标题】:Accessing values from different classes访问不同类的值
【发布时间】:2016-08-19 11:26:04
【问题描述】:

我正在尝试访问存在于类Mainactivity 中的edttxt 值,它包含在类Mainactivity 中:

Button btn=(Button)findViewById(R.id.btn);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                EditText edttxt =(EditText)findViewById(R.id.edttxt);
                TextView txtview =(TextView)findViewById(R.id.textView);
                txtview.setText(edttxt.getText().toString());
            }
        });

我需要的访问是用其他类中的edttxt值替换100个值,这是我需要修改其他类的部分:

public void switchFlash() {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (isFlashOn()) {
                    turnOffFlash();
                    x++;    
                } else if (x>20) {
                    turnOffFlash();
                }
                else
                {
                    turnOnFlash();
                }
                handler.postDelayed(this,100);
            }
        };
        handler.postDelayed(runnable,100);
    }

【问题讨论】:

  • 问题是?
  • 声明静态全局变量并将您的编辑文本值存储在其中。然后使用 ClassName.yourStaticvariableName 在任何你想要的任何类中访问该静态变量。
  • 声明一些静态变量并存储该值并尝试将其访问到您的类中
  • 你还需要更多的澄清,你能把事情解释得更详细和描述性吗
  • 在意图中传递变量,不要为此使用静态变量。如果您需要如何执行此操作,我可以分享。

标签: java android class


【解决方案1】:

正如我从上述对话和代码上下文中了解到的,您想要传递给其他类的是 EditText 视图中包含的值(字符串),而不仅仅是视图本身。在这种情况下,您有不同的选择,但一种简单的方法是在创建对象时使用如下构造器将此“值”传递给其他类:

Button btn=(Button)findViewById(R.id.btn);
final EditText edttxt =(EditText)findViewById(R.id.edttxt);
OtherClass otherClass = new OtherClass(delay);
btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
        delay = edttxt.getText().toString();
        TextView txtview =(TextView)findViewById(R.id.textView);
        txtview.setText(edttxt.getText().toString());
    }
});

将延迟声明为 MainActivity 类的全局变量,如:

public class MainActivity extends AppCompatActivity {

    private String delay;
    ....

你的其他班级可能看起来像这样:

public class OtherClass {
    int delay;
    public OtherClass(String delay){
        this.delay = Integer.parseInt(delay);
    }
    //...your code
    public void switchFlash() {
        final Runnable runnable = new Runnable() {
            @Override
            public void run() {
                if (isFlashOn()) {
                    turnOffFlash();
                    x++;
                } else if (x>20) {
                    turnOffFlash();
                }
                else
                {
                    turnOnFlash();
                }
                handler.postDelayed(this,delay);
            }
        };
        handler.postDelayed(runnable,delay);
    }
}

【讨论】:

  • 我修改了代码,但是当我将其上传到手机时,应用程序在启动之前就停止了工作。
  • 您的代码的其他部分可能存在错误,因此请尽可能提供,以便我们为您提供帮助。当您的应用程序停止时,logcat 中还会显示一条错误消息,这使得调试非常容易,所以也请提供。
猜你喜欢
  • 2016-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-07-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多