查找不同设备的高度和宽度

时间:2012-07-18 07:28:13

标签: android android-layout

我一直在开发一款需要查找不同设备高度和宽度的应用。如果我删除高度和宽度的代码,如果我包含应用程序崩溃的代码,应用程序将起作用。

你能否说一下这段代码的错误。

以下是代码:

public class Newone extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newone);
    }

    final Context context = this;
    public String cntry = "";
    int w= getWindowManager().getDefaultDisplay().getWidth();
    int h = getWindowManager().getDefaultDisplay().getHeight();

    DisplayMetrics metrics = context.getResources().getDisplayMetrics();
    int w = metrics.widthPixels;
    int h = metrics.heightPixels;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        MotionEvent object holds X-Y values
        if(event.getAction() == MotionEvent.ACTION_DOWN) {              
            String text = "You click at x = " + event.getX() + " and y = " + event.getY();
            //Toast.makeText(this, text, Toast.LENGTH_LONG).show();

            if((event.getX()>((w/1280)*850)) && (event.getX()<((h/752)*900)) && (event.getY()>((w/1280)*380)) && (event.getY()<((h/752)*450)))
                if((event.getX()>850) && (event.getX()<900) && (event.getY()>380) && (event.getY()<450)) {
                    Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                    display(cntry);
                }

                if((event.getX()>280) && (event.getX()<400) && (event.getY()>200) && (event.getY()<300)) {
                    //Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                    cntry = "US";
                    display(cntry);
                }

                if((event.getX()>900) && (event.getX()<1200) && (event.getY()>500) && (event.getY()<650))
        }
        return super.onTouchEvent(event);
    }   

    private void display(final String cntry) {
        final Dialog dialog = new Dialog(context);
        dialog.show();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_newone, menu);
        return true;
    }

}

2 个答案:

答案 0 :(得分:1)

你必须在onCreate中移动以下内容。

final Context context = this;
public String cntry = "";


DisplayMetrics metrics = context.getResources().getDisplayMetrics();
int w = metrics.widthPixels;
int h = metrics.heightPixels;

此外,getWindowManager().getDefaultDisplay().getWidth();getHeight现已弃用。请勿使用它们。

以下是更正后的摘录。

public class Newone extends Activity {
    final Context context;
    public String cntry = "";
    int w, h;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_newone);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        context = this;

        DisplayMetrics metrics = context.getResources().getDisplayMetrics();
        w = metrics.widthPixels;
        h = metrics.heightPixels;
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {

        if (event.getAction() == MotionEvent.ACTION_DOWN) {

            String text = "You click at x = " + event.getX() + " and y = "
                    + event.getY();
            // Toast.makeText(this, text, Toast.LENGTH_LONG).show();

            if ((event.getX() > ((w / 1280) * 850))
                    && (event.getX() < ((h / 752) * 900))
                    && (event.getY() > ((w / 1280) * 380))
                    && (event.getY() < ((h / 752) * 450)))
                if ((event.getX() > 850) && (event.getX() < 900)
                        && (event.getY() > 380) && (event.getY() < 450))

                {
                    Toast.makeText(this, text, Toast.LENGTH_LONG).show();

                    display(cntry);
                }

            if ((event.getX() > 280) && (event.getX() < 400)
                    && (event.getY() > 200) && (event.getY() < 300))

            { // Toast.makeText(this, text, Toast.LENGTH_LONG).show();
                cntry = "US";
                display(cntry);
            }

            if ((event.getX() > 900) && (event.getX() < 1200)
                    && (event.getY() > 500) && (event.getY() < 650)) {

            }

        }

        return super.onTouchEvent(event);
    }

    private void display(final String cntry) {

        final Dialog dialog = new Dialog(context);
        dialog.show();

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_newone, menu);
        return true;
    }

}

答案 1 :(得分:0)

如果你移动getWindowManager().getDefaultDisplay().getWidth()怎么办?你getHeight()内的metrics = context.getResources().getDisplayMetrics();onCreate?我不确定context和其他人在Activity的施工时间是否有空。