Examples of Component Object
make key function module calculator interface on a relationship;
To
numbers in the figures show the "Display results" position.
Define objects:
//定义对象
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);
}
Here is binding instance of Component Object listening for events
// 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);*/
The above two are the code of violence to solve problems; here it will make the code optimization:
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;
}
}
}
The code above are implemented in MainActivity.java in
a good, relevant content is to achieve these objects and events in the monitor.
Some additional knowledge:
listener definition:
the listener is actually a class that implements a particular interface, then this class is described in the web.xml file so that the server at startup can instantiate the class , start the listener. When the range of the state of the object changes, the server automatically listener object method invocation.
Definition of the interface:
the interface definition keyword: interface
defined format:
public interface 接口名 extends 接口,... {
//定义常量
public static final 数据类型 常量名 = 值;
//定义抽象方法
public abstract 返回值数据类型 方法名(数据类型 参数名,...);
}
Listener methods:
btn1.setOnClickListener(new myClick());