源码如下:
package com.bn.chap4.screen;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Sample4_1_Activity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//如下代码得到屏幕对象
final DisplayMetrics dm=new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
final EditText et=(EditText)this.findViewById(R.id.EditText01);//EditText对象
final EditText ett=(EditText)this.findViewById(R.id.EditText02);//EditText对象
Button ok=(Button)this.findViewById(R.id.Button01);//按钮对象
ok.setOnClickListener
(
new OnClickListener()
{
public void onClick(View v) {
// TODO Auto-generated method stub
et.setText(dm.heightPixels+"");//引用屏幕的相关系数:分辨率
ett.setText(dm.widthPixels+"");
}
}
);
}
}