2016-08-19 1 views
-1

I 클래스 Mainactivity에 존재 edttxt 값에 액세스하는 것을 시도하고있다, 이것은 클래스 Mainactivity에 포함되어 있습니다 :액세스 값은

내가 필요로하는 액세스에 edttxt 값으로 100 개 값을 대체하는 것입니다
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()); 
      } 
     }); 

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); 
    } 
+1

을 그리고 질문은 :

public class MainActivity extends AppCompatActivity { private String delay; .... 

그리고 다른 클래스는 다음과 같이 보일 수 있습니다? –

+0

정적 전역 변수를 선언하고 편집 텍스트 값을 그 안에 저장하십시오. 그런 다음 'ClassName.yourStaticvariableName'을 사용하여 원하는 모든 클래스의 정적 변수에 액세스하십시오. –

+0

일부 정적 변수를 선언하고 해당 값을 저장하고 클래스에 액세스하려고 시도하십시오. –

답변

0

나는 위의 대화에서, 또한 당신이 다른 클래스에 전달하려는 어떤 코드의 컨텍스트에서 알 수있는 바와 같이이 값입니다 (: 다른 클래스는, 이것은 내가 다른 클래스를 수정해야 할 부분이다 str)뿐만 아니라보기 자체에 EditText보기에 포함되어 있습니다. 이 경우 다른 옵션을 가지고 있지만 할 수있는 쉬운 방법 중 하나는 다음과 같은 constractor를 사용하여 객체 생성에 다른 클래스에이 '가치'를 전달하는 것입니다 :의 글로벌 변수로

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 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); 
    } 
} 
+0

코드를 수정했지만 모바일에 업로드 할 때 응용 프로그램이 시작되기 전에 작동을 멈 춥니 다. – Aloweiwi

+0

코드의 다른 부분에 버그가있을 수 있으므로 최대한 도와 주시면 도와 드리겠습니다. 또한 응용 프로그램이 멈추었을 때 logcat에 오류 메시지가 표시되어 디버깅이 매우 쉬워 지므로 제공하십시오. – Addis