TextView.setText()在方法内部无效

时间:2015-01-27 20:09:38

标签: android textview settext

在方法scanning()中,textView.setText("Scanning");不会更新textView的内容。虽然Log.e("Scanning","Inside Scanning");在控制台中有效。请问你说我犯了什么错误?

ImageButton imageButton;
TextView textView;
Vibrator vibrator;
long t1 = 0, t2 = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    initialize();

  RelativeLayout.LayoutParams textview_params = 
(RelativeLayout.LayoutParams)textView.getLayoutParams();
  textView.setLayoutParams(new 
  RelativeLayout.LayoutParams(width,height/4));
    textview_params.addRule(RelativeLayout.ALIGN_PARENT_TOP);

    imageButton.setOnTouchListener(this);

}



private void initialize() {
    imageButton = (ImageButton)findViewById(R.id.image_button);
    textView = (TextView)findViewById(R.id.text_view);
    vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE) ;
}

@Override
public boolean onTouch(View v, MotionEvent event) {

    if(event.getAction()==MotionEvent.ACTION_DOWN){
        scanning();
        t1 = (int)System.currentTimeMillis();
        vibrator.vibrate(1900);
        Log.e("t1 ",t1+"");
    }
    if(event.getAction()==MotionEvent.ACTION_UP){

        t2 = (int)System.currentTimeMillis();

        Log.e("t2 ",t2+"");
        vibrator.cancel();
    }
    calculate();
    return true;
}

private void scanning() {
    Log.e("Scanning","Inside Scanning");

    ;;;;;;;;;;;
    //This textView.setText() is not working

    textView.setText("Scanning");

    ;;;;;;;;;;;;;;

}

private void calculate() {
    Log.e("t2-t1 ", t2-t1+"");

    if(t2-t1>=1900){

        //These both are working properly

        textView.setText("Working Properly");
    }
    else{
        textView.setText("Aborted\n Try Again");
    }
}


}

0 个答案:

没有答案