实例化对象和监听事件-------android学习笔记四

实例化组建对象
让计算器界面上的按键与功能模块发生关系;
要将在这里插入图片描述
图中的数字显示到“显示结果”的位置。
先定义对象:

  //定义对象
    Button btn1, btn2, btn3, btn4, btn5,btn6,btn7,btn8,btn9,btn0;
    Button[] btn = new Button[10];//button类对象
    int[]  btnId = {R.id.btn0, R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9};
    TextView txtResult;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.calc);
        this.findViewById(R.id.btn1);
        txtResult = (TextView) this.findViewById(R.id.textResult);
        }

下面是绑定监听事件实例化组建对象

//        initView();//绑定监听事件
//        myClick myClick = new myClick();
//        for(int i=0;i<btn.length;i++){
//            btn[i].setOnClickListener(myClick);
//        }

//        btn1.setOnClickListener(new myClick());
//        btn2.setOnClickListener(new myClick());
//        btn3.setOnClickListener(new myClick());
//        btn4.setOnClickListener(new myClick());
//        btn5.setOnClickListener(new myClick());
//        btn6.setOnClickListener(new myClick());
//        btn7.setOnClickListener(new myClick());
//        btn8.setOnClickListener(new myClick());
//        btn9.setOnClickListener(new myClick());
//        btn0.setOnClickListener(new myClick());

//    void initView(){//实例化组建对象
//        txtResult = (TextView) this.findViewById(R.id.textResult);
//        for(int i = 0;i<btn.length;i++){
//            btn[i] =(Button) this.findViewById(btnId[i]);
//        }
//    }
        /*
        btn1 = (Button) this.findViewById(R.id.btn1);//实例化组建对象
        btn2 = (Button) this.findViewById(R.id.btn2);
        btn3 = (Button) this.findViewById(R.id.btn3);
        btn4 = (Button) this.findViewById(R.id.btn4);
        btn5 = (Button) this.findViewById(R.id.btn5);
        btn6 = (Button) this.findViewById(R.id.btn6);//实例化组建对象
        btn7 = (Button) this.findViewById(R.id.btn7);
        btn8 = (Button) this.findViewById(R.id.btn8);
        btn9 = (Button) this.findViewById(R.id.btn9);
        btn0 = (Button) this.findViewById(R.id.btn0);*/

上面的两个都是暴力解决问题的代码;在此呢将对代码做出优化:

public class MainActivity extends AppCompatActivity {

    TextView tv;
    //alt+enter自动导入包
    //定义对象
    Button btn1, btn2, btn3, btn4, btn5,btn6,btn7,btn8,btn9,btn0;
    Button[] btn = new Button[10];
    int[]  btnId = {R.id.btn0, R.id.btn1,R.id.btn2,R.id.btn3,R.id.btn4,R.id.btn5,R.id.btn6,R.id.btn7,R.id.btn8,R.id.btn9};
    TextView txtResult;

    @Override

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setContentView(R.layout.calc);
        this.findViewById(R.id.btn1);
        txtResult = (TextView) this.findViewById(R.id.textResult);
    }
     //定义一个单击方法检测并返回按键的值
    public void myOnclick(View  v){
        switch (v.getId()) {
            case R.id.btn1://通过传来的Id进行检测并返回这个按钮的值
                txtResult.setText("1");//在显示结果的地方返回值  1  
                break;
            case R.id.btn2:
                txtResult.setText("2");
                break;
            case R.id.btn3:
                txtResult.setText("3");
                break;
            case R.id.btn4:
                txtResult.setText("4");
                break;
            case R.id.btn5:
                txtResult.setText("5");
                break;
            case R.id.btn6:
                txtResult.setText("6");
                break;
            case R.id.btn7:
                txtResult.setText("7");
                break;
            case R.id.btn8:
                txtResult.setText("8");
                break;
            case R.id.btn9:
                txtResult.setText("9");
                break;
            case R.id.btn0:
                txtResult.setText("0");
                break;
        }
    }
}

以上代码都在MainActivity.java中实现
好了,这些就是实现对象化和监听事件的相关内容了。
附加一些知识:
监听器的定义:
监听器实际上是一个类,这个类实现了特定的接口,然后将这个类在 web.xml 文件中进行描述,这样服务器在启动的时候就可以实例化这个类,启动监听器。当范围对象的状态发生变化的时候,服务器自动调用监听器对象中的方法。
接口的定义:
定义接口的关键字:interface
定义格式:

public interface 接口名  extends 接口,...  {
   //定义常量
   public static final 数据类型  常量名 =;
   
   //定义抽象方法
   public abstract 返回值数据类型  方法名(数据类型 参数名,...);
  }

监听方法:

btn1.setOnClickListener(new myClick());
发布了4 篇原创文章 · 获赞 0 · 访问量 77

猜你喜欢

转载自blog.csdn.net/qq_40953864/article/details/104503288
今日推荐