Android Development Lesson 2

Table of contents

Activity 

How to modify loginactivity as startup class later?

XML Markup Supplement

Add button to layout

Modify the text of the button

Add a listener event to the button in the java file

layout

Attributes

New layout file


 

Activity 

1. Create an activity

  •  generate a layout file is whether to generate a layout file, click to select
  • Launcher activity represents whether it is used as a startup class, and it is not selected first
  • After the creation is complete, a statement about login will be added in the manifest.xml

Before the dot represents the omitted package name path

How to modify loginactivity as startup class later?

There can only be one startup class . After one is modified, the other startup class should be changed to a non-startup class.

  1. Paste the contents of the <intent-filter> tag into the LoginActivity tag, and then change the false of the exported attribute to true
  2. In the same way, remove the <intent-filter> of the MainActivity that was originally the startup class, and change true to false

The effect is as follows:

before fixing:

After modification: 

<activity
            android:name=".LoginActivity"
            android:exported="true" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:exported="false">

        </activity>

XML Markup Supplement

Header tag, if you don’t need to include anything, just end with a slash: <activity … />

If you need to embed again, you need to add another tail tag</***>, and then insert it

  • For example, in the first activity tag, there is only the header tag, and the attributes are placed in <activity ... />
  • The second activity tag is complete. Shaped like <activity>…</activity>

 

 

Add button to layout

In the activity_login.xml file, drag the button into the layout in design mode, then the following code will appear, and <Button....../> is the attribute of the button

The button's id is button 

 

Modify the text of the button

If you want to modify the text on the button button, you need to define the string in string.xml first, and then use @ reference

1. Define a button named btnLoginTxt in strings.xml

<resources>
    <string name="app_name">红柚子的APP</string>

<!--    定义登录按钮文字的字符串-->
    <string name="btnLoginTxt">点击登录</string>
</resources>

2. Use the form of @string/string name  in the button attribute to reference the string

android:text="@string/btnLoginTxt"

 

Add a listener event to the button in the java file

  1.  Create a button object in java, use the findViewById method to link the button in the layout with the button object in java
  2. Add a listener. The listening object uses an anonymous class. After entering new, the following code is automatically completed to complete the creation of the anonymous class.
  3. Display floating information on the simulator, use the toast function to modify the corresponding text content
  4. To test the button in logcat, use Log.d("btnclick", "button was clicked");

Note :

 The layout files referenced by R.layout.*** in setCountView can be customized, whichever layout file needs to be displayed can be referenced

The buttons connected in the findViewById method can also be replaced according to different button ids

code: 

package cn.edu.sdut.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class LoginActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);


//        加载布局文件,是第一行代码
//        通过activity_login.xml布局文件展现效果,布局效果layout.***可以任意更换
        setContentView(R.layout.login_linearlayout);

//        事件点击,把布局文件中的按钮与java中的对象联系起来,使用fvi方法
        Button btnLogin = findViewById(R.id.button2);

//        添加监听器,监听对象使用匿名类,输入new后自动补全后面的代码,完成对匿名类的创建
        btnLogin.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                在logcat中测试按钮
                Log.d("btnclick", "按钮被点击了");
//                在模拟器上显示浮动信息,使用toast函数,修改对应的text内容
                Toast.makeText(LoginActivity.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
            }
        });
    }
}

Test results: 

layout

view view

ViewGroup is used as a control in the container interface, and the viewgroup and view can be included or juxtaposed, similar to the compartments of a wardrobe

Attributes

1. id attribute: @+id/custom id name

Use findViewById in java to find the id of the space in XML

2. A layout must have height and width properties

Type h and w to automatically complete the strings related to height and width,

Then type m and w to represent match-parent (match parent university) and wrap-content (match itself)

3. Background: You can use color or picture

  • To quote an image: use @drawable/image name 
android:background="@drawable/summer"
  • color:
android:background="@color/teal_200"

 

New layout file

Create a new interface layout file resource file under the layout folder, and the name cannot appear in uppercase letters

 

The newly created layout file is shown in the figure: three buttons are arranged vertically

 If you want to change to a horizontal arrangement:

 

Guess you like

Origin blog.csdn.net/weixin_45662399/article/details/126538366