java - 无法获取另一个类中的屏幕宽度和高度值

标签 java android screen

我正在制作一个应用程序,在这个应用程序中我需要显示设备的宽度和高度。最初我在一个类中计算了宽度和高度,然后立即显示。但为了让它看起来更有条理,我将它们分成一个单独的类。类中的这段代码:

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

然后在您的主要 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);

不确定它是否有效,尚未测试。

另外,我不知道为什么(只编码了 6 个月)——但是当你从不调用 DisplayHeightWidthMeasurement() 时,它是如何被调用的。您所做的就是:

DisplayMeasurement screenValue = new DisplayMeasurement();

因此您的方法: DisplayHeightWidthMeasurement 实际上具有获取宽度和高度的逻辑,但从未被调用。您需要先调用它,否则它只会返回 0,这就是您初始化的值。

关于java - 无法获取另一个类中的屏幕宽度和高度值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21070604/

相关文章:

android - 屏幕锁定时如何显示Activity?

jquery - 监视器显示设置的边距动画问题

opengl - 你怎么知道你所显示的是完全绘制在屏幕上的?

java - 从 Web 应用程序 GUI 创建数据库备份

java - SOAP 还是休息?具体项目

android - 混洗现有 View

android - 使用二维码渲染 3D 模型

java - 单击时禁用按钮,然后重新启用它

java - GET 请求中将使用多少个套接字

java - J2EE 中的应用程序管理事务