簡體   English   中英

無法在另一個類中獲取屏幕寬度和高度值

[英]can't get the screen width and height values in another 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;
    }   
}

那么這是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);

但我得到的是價值0.是否有人經歷過相同的事情,或者有誰知道這個問題。

對不起,我對Android很新,但我會試一試。

我不確定你為什么要通過創建一個似乎過於復雜的新Activity來組織代碼。

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;
    }   
}

然后在你的主要活動中;

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);

不確定它是否會起作用,還沒有測試過。

此外,我不確定為什么(僅編碼6個月) - 但是當你從未打電話時,DisplayHeightWidthMeasurement()是如何被調用的。 你所做的就是:

DisplayMeasurement screenValue = new DisplayMeasurement();

所以你的方法:DisplayHeightWidthMeasurement,它實際上具有獲取寬度和高度的邏輯永遠不會被調用。 您需要先調用它,否則它將返回0,這是您初始化的內容。

你永遠不會調用函數DisplayHeightWidthMeasurement()所以它的set()的方法還沒有調用,所以高度寬度的值就像你設置的那樣為零,就像@TheIT說它不是實例化一個Activity的好方法一樣,您可以定義一個獲取diplay 寬度高度Class ,而不是使用Activity

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM