简体   繁体   中英

can't get the screen width and height values in another class

I'm making an application, in this application I need the width and height of the display device. Initially I made ​​a calculation of the width and height in one class and then instantly displayed. But to make it look more organized, I separate them in a separate class. This code in the class:

public class DisplayMeasurement extends Activity{

public int widthScreen = 0;
public int heightScreen = 0;

@SuppressWarnings("deprecation")
public void DisplayHeightWidthMeasurement(){
      int Measuredwidth = 0;
      int Measuredheight = 0;
      Point size = new Point();
      WindowManager w = getWindowManager();

        if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2){
              w.getDefaultDisplay().getSize(size);

              Measuredwidth = size.x;
              Measuredheight = size.y; 
            }else{
              Display d = w.getDefaultDisplay(); 
              Measuredwidth = d.getWidth(); 
              Measuredheight = d.getHeight(); 
            }
        //int sepertiga = Measuredwidth/3;
        //akhir ngukur layar
        set(Measuredwidth,Measuredheight);
    }

    public void set(int Measuredwidth, int Measureheight){
        this.widthScreen = Measuredwidth;
        this.heightScreen = Measureheight;
    }

    public int getWidthScreen(){
     return widthScreen;
    }

    public int getHeightScreen(){
        return heightScreen;
    }   
}

then this is a piece of code on mainActivity:

  DisplayMeasurement screenValue = new DisplayMeasurement();
    int WidthScreen = screenValue.getWidthScreen();
    int HeightScreen = screenValue.getHeightScreen();


    TextView text1 = (TextView) findViewById(R.id.text1);
    TextView text2 = (TextView) findViewById(R.id.text2);

    text1.setText("lebar" + WidthScreen);
    text2.setText("tinggi" + HeightScreen);

But that I get is the value 0. Is there anyone who has experienced the same thing, or are there who know about this problem.

Sorry, I'm kind of new to Android but I'll give it a go.

I'm not sure why you're trying to organise code by creating a new Activity which seems to over-complicate things.

public class DisplayMeasurement {

    public int widthScreen = 0;
    public int heightScreen = 0;

    public void DisplayMeasurement(Context context){
          Point size = new Point();
          WindowManager w = (WindowManager) mContext.getSystemService(mContext.WINDOW_SERVICE);

          if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.HONEYCOMB_MR2){
               w.getDefaultDisplay().getSize(size);  
               widthScreen = size.x;
               heightScreen = size.y; 
          } else {
               Display d = w.getDefaultDisplay(); 
               widthScreen = d.getWidth(); 
               heightScreen = d.getHeight(); 
          }
    }

    public int getWidthScreen(){
        return widthScreen;
    }

    public int getHeightScreen(){
        return heightScreen;
    }   
}

And then in your main activity;

DisplayMeasurement screenValue = new DisplayMeasurement(this);
int WidthScreen = screenValue.getWidthScreen();
int HeightScreen = screenValue.getHeightScreen();


TextView text1 = (TextView) findViewById(R.id.text1);
TextView text2 = (TextView) findViewById(R.id.text2);

text1.setText("lebar" + WidthScreen);
text2.setText("tinggi" + HeightScreen);

Not sure if it will work though, haven't tested it.

Also, I'm not sure why (have been only coding for 6 months) - but how is DisplayHeightWidthMeasurement() meant to be ever called, when you never call for it. All you do is:

DisplayMeasurement screenValue = new DisplayMeasurement();

and so your method: DisplayHeightWidthMeasurement, which actually has the logic to get the width and height is never called. You would need to call that first, otherwise it would just return 0, which is what you initialised.

你永远不会调用函数DisplayHeightWidthMeasurement()所以它的set()的方法还没有调用,所以高度宽度的值就像你设置的那样为零,就像@TheIT说它不是实例化一个Activity的好方法一样,您可以定义一个获取diplay 宽度高度Class ,而不是使用Activity

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM