【apk】android studio界面介绍及按键使用

前言

根据git项目学习

res目录

重点,layout目录下的activity_main.xml
这个是应用打开时显示的界面
注意这里有两个窗口,一个是text,一个是design
text是xml格式显示,design可以看到页面布局

快捷键(在design窗口下):
按tab,可以依次选择各模块
ctrl+鼠标左击——多选

在这里插入图片描述

在这里插入图片描述
在这里插入图片描述

布局

在design窗口,选择button放置到界面中,提示没有约束条件,因为它的左右上下没有限定,可能会跑到坐标(0,0)上,即左上角,所以在提醒上方点击四个加号,会自动弹出限制,可以设定数值

其中id可以修改,这个id就是代码中访问该按键的身份证
text是显示的内容,可以修改为自己需要的
在这里插入图片描述

运行及测试

在这里插入图片描述
从左至右,红色框住分别是编译,运行,虚拟机测试
代码编写好后,依次点击编译,运行,自动弹出虚拟机窗口
当然也可以在编译后,点击虚拟机测试,运行
在这里插入图片描述
在这里插入图片描述

代码

mainActivity.java

package com.example.demo;
//根据自己创建的apk名字

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

	//定义两个按钮
    private Button suspendon;
    private Button suspendoff;//closefloatballbtn
    private Button closeappbtn;
    //定义文本
    private TextView text;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initUI();	
    }

    //参考:https://blog.csdn.net/u011324501/article/details/51543042

    private void initUI() {
        // TODO Auto-generated method stub
        suspendon = findViewById(R.id.floatballbtn);	//这里的id是在activity_main.xml中定义的,在design下可以看到,在text下就是id/XXX
        suspendon.setOnClickListener(new suspendListener());	//创建按键监听,当按键按下时,会执行suspendListener定义的onClick函数。每个按键都有自己的监听类
        suspendoff = findViewById(R.id.closefloatballbtn);
        suspendoff.setOnClickListener(new suspend2Listener());
        closeappbtn = findViewById(R.id.exitbtn);
        closeappbtn.setOnClickListener(new closeappListener());
        text = (TextView)findViewById(R.id.textView);			//根据id查找到文本框的窗口
        text.setText("init");									//设置文本显示内容
    }
    public class suspendListener implements OnClickListener {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text = (TextView)findViewById(R.id.textView);
            text.setText("button on");
            //启动悬浮窗口关闭本窗口
//            startService(intent);
//            finish();
        }
    }
    public class suspend2Listener implements OnClickListener {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            text = (TextView)findViewById(R.id.textView);
            text.setText("button off");
            //启动悬浮窗口关闭本窗口
//            startService(intent);
//            finish();
        }
    }
    public class closeappListener implements OnClickListener {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            android.os.Process.killProcess(android.os.Process.myPid());	//获取当前pid,然后退出app
            //启动悬浮窗口关闭本窗口
//            startService(intent);
//            finish();
        }
}

布局文件
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintHorizontal_bias="0.491"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.598" />

    <Button
        android:id="@+id/floatballbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="162dp"
        android:layout_marginEnd="167dp"
        android:layout_marginRight="167dp"
        android:layout_marginBottom="146dp"
        android:text="弹出悬浮窗"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="1.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.328" />

    <Button
        android:id="@+id/closefloatballbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="150dp"
        android:layout_marginLeft="150dp"
        android:layout_marginTop="23dp"
        android:layout_marginEnd="173dp"
        android:layout_marginRight="173dp"
        android:layout_marginBottom="75dp"
        android:text="关闭悬浮窗"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/floatballbtn"
        app:layout_constraintVertical_bias="0.0" />

    <Button
        android:id="@+id/exitbtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="153dp"
        android:layout_marginLeft="153dp"
        android:layout_marginTop="20dp"
        android:layout_marginEnd="170dp"
        android:layout_marginRight="170dp"
        android:layout_marginBottom="42dp"
        android:text="关闭app"
        app:layout_constraintBottom_toTopOf="@+id/textView"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/closefloatballbtn" />

</androidx.constraintlayout.widget.ConstraintLayout>

结果

初始化界面文本框显示init
在这里插入图片描述

点击”弹出悬浮窗“按钮,文本显示button on
在这里插入图片描述

点击”关闭悬浮窗“按钮,文本显示button off
在这里插入图片描述
点击”关闭app“按钮,推出应用


版权声明:本文为qq_38091632原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。