Zero foundation Android development study notes based on Android Studio No.1 login interface

Synchronize with the learning of ios development based on Xcode.
Start with zero foundation.
I hope I can do more!

avatars / avatars

1.xml file and java file

The Android layout is written in the res/layout folder in the form of xml, so that not only the front-end interface can be distinguished from the business logic

Add the associated code in the onCreate method of the java file, such as setContentView

The xml file is the UI layout of the APP, which shows the display effect of the entire app. The code of the java file is responsible for logic processing, controlling the function of each control in the xml, and the connection between the controls

The java file is the Pascal nomenclature: also known as the big hump nomenclature, the first letter of all words is capitalized; the XML file is the underscore nomenclature: the word and the word are separated by underscores.

Define members in the MainActivity class

	private EditText et_username;
    private EditText et_password;
    private Button bt_log;
    private Button bt_reg;	

Obtain the control instance through the resource identifier in the onCreate method

//通过资源标识获得控件实例
et_username = (EditText) findViewById(R.id.et_username);
et_password = (EditText) findViewById(R.id.et_password);
bt_log = (Button) findViewById(R.id.bt_log);
bt_reg = (Button) findViewById(R.id.bt_reg);

2.Toast

Toast is a reminder method provided by the Android system, which does not occupy any screen space, so some brief information can be notified to the user through the toast method, and the information will disappear automatically after a period of time.

The first parameter: MainActivity.this, the current context. The second parameter: the string to be displayed, which is what you want to display on the screen. The
third parameter: the length of time to display, which is the string on the screen. The duration of the display. Toast has two default LENGTH_LONG (long) and LENGTH_SHORT (short).
show(); show

Toast.makeText(MainActivity.this,"登陆成功!欢迎回来!用户"+strUsername, Toast.LENGTH_SHORT).show();

3.AVD:Android Virtual Device

4.Error:MissingConstraints

Solution: Add constraints to the control:

app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"

5. Add id attribute to the label

In the xml file

android:id="@+id/textView"

You can use Code mode and Design mode, Design mode corresponds to storyboard and xib in ios development

Write events in java files

//给注册按钮注册监听器,实现监听器接口,编写事件
        bt_reg.setOnClickListener(new View.OnClickListener() {
    
    
            @Override
            public void onClick(View v) {
    
    
                //获取用户输入的数据
                String strUsername = et_username.getText().toString();
                String strPassword = et_password.getText().toString();

                Toast.makeText(MainActivity.this,"注册成功!你申请的账户是:\n用户名:"+strUsername+" 密码:"+strPassword,Toast.LENGTH_SHORT).show();

            }
        });

Realize the effect:

Insert picture description here
Insert picture description here

Reference article: https://blog.csdn.net/qq_41145101/article/details/82656170

Guess you like

Origin blog.csdn.net/weixin_56336619/article/details/115036922