import android.app.Activity; import android.os.Bundle; import android.util.DisplayMetrics; import android.view.View; import android.view.View.OnClickListener; import android.view.Window; import android.view.WindowManager; import android.widget.Button; import android.widget.TextView; public class ActivityMain extends Activity implements OnClickListener{ /** Called when the activity is first created. */ private TextView mtv; private DisplayMetrics DisMet = new DisplayMetrics(); private Button btn1; private Button btn2; private Button btn3; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); hideStatusBar(); setContentView(R.layout.main); mtv = (TextView)findViewById(R.id.tv); btn1 = (Button)findViewById(R.id.btn1); btn2 = (Button)findViewById(R.id.btn2); btn3 = (Button)findViewById(R.id.btn3); // 按钮监听实现1 函数方式实现 btn1.setOnClickListener(new Button.OnClickListener(){ public void onClick(View v) { getWindowManager().getDefaultDisplay().getMetrics(DisMet); mtv.setText("当前屏幕分辨率为:"+DisMet.widthPixels+"*"+DisMet.heightPixels); } }); btn2.setOnClickListener(new btnOnClickListener()); btn3.setOnClickListener(this); } // 隐藏标题 public void hideTitle() { requestWindowFeature(Window.FEATURE_NO_TITLE); } // 全屏幕 public void hideStatusBar() { hideTitle(); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); } // 按钮监听实现2 包内类实现 public class btnOnClickListener implements OnClickListener { public void onClick(View arg0) { // TODO Auto-generated method stub getWindowManager().getDefaultDisplay().getMetrics(DisMet); mtv.setText("当前屏幕分辨率为:"+DisMet.widthPixels+"*"+DisMet.heightPixels); } } // 按钮监听实现3 类接口函数实现 public void onClick(View v) { // TODO Auto-generated method stub getWindowManager().getDefaultDisplay().getMetrics(DisMet); mtv.setText("当前屏幕分辨率为:"+DisMet.widthPixels+"*"+DisMet.heightPixels); } }
按钮监听事件实现(三种方式实现)
最新推荐文章于 2022-07-30 15:54:00 发布