4. The first line of code

        In the previous section, the page construction of the reply function was completed, but the desired function was not realized. The separation of page design (implementing the desired layout in xml) and functionality (page logic, usually implemented in java code) makes it easier for developers to operate. So to achieve the desired functionality, write the first (N) lines of code.

        Where the code is written, the new project is only MainActivity.java, of course this is it. What to write, if you want to realize click reply, of course, you need to introduce the Button, EditText, and TextView to be operated from xml into java code. android:id="@+id/xxx" (in xml) and R.id.xxx (in java code), you can use findViewById(R.id.xxx) (why is View, the parent classes of controls are basically all View) to get the object of the class (control) (what? There is no object ::>_<::) (it is good to treat the object as a variable, but it has properties and methods that ordinary variables do not have).
        Define the class object used before onCreate:



TextView tv_review;
EditText et_review;
Button bt_review;

        Assign value to the object, through findViewById, in onCreate, after setContentView (the xml must be loaded before the controls can be obtained):
tv_review=(TextView)findViewById(R.id.tv_review);
et_review=(EditText)findViewById(R.id.et_review);
bt_review=(Button)findViewById(R.id.bt_review);

        Why do you want to cast, findViewById returns is that View needs to be cast to the desired type. The control in the xml has been assigned to the java object, and now the button click event can be added. The button (Button class) has a method called setOnClickLisrener(OnClickListener listener), which is to add a click listener to the button (after triggering an event, the method in the registered listener will be executed, similar to a callback, which is very useful). After the bt_review assignment:
bt_review.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View view) {
    //  TODO Auto-generated method stub

    }
});

        The parameter of the setOnClickListener function is the OnClickListener object, which is directly new (what is new) here, and an onClick function will be automatically generated. When the bt_review button is clicked, the onClick function is executed, where the logic code to be processed is written, and the text entered in the EditText is displayed on the TextView above. How to get the content entered by EditText, EditText has a method getText(), how to assign it to TextView, the setText method of TextView:
public void onClick(View view) {
//  TODO Auto-generated method stub
    if(!et_review.getText().toString().equals("")){

        tv_review.setText(et_review.getText());

    }
}

        Get the content of et_review, judge whether it is empty or not, assign it to tv_review, run the project, enter the content, and click reply.

Note: This is a .gif animation, ctrl click on the picture to view. The aspect ratio of the picture recording is not good, and it will be corrected after 12 sections.

        The idea of ​​programming has to be understood by oneself, simple and small functions are a bit interesting.

no if - 2016/10/20


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326217735&siteId=291194637