Button

触发事件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"> <Button
android:id="@+id/btn"
android:onClick="myClick"
android:background="@drawable/btn_selector"
android:backgroundTint="@color/btn_color_selector"
android:layout_width="200dp"
android:layout_height="100dp"/> </LinearLayout>
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button; public class MainActivity extends AppCompatActivity { private static final String TAG = "MainActivity"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn); // 点击触发
// btn.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// Log.e(TAG, "onClick:");
// }
// }); // 长按触发
btn.setOnLongClickListener(new View.OnLongClickListener() {
@Override
public boolean onLongClick(View view) {
Log.e(TAG, "onLongClick:");
return false; // 改为true时,执行onLongClick后不会执行onClick
}
}); // 触摸事件
btn.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
Log.e(TAG, "onTouch:" + motionEvent.getAction());
return false; // 改为true时,执行onTouch后不会执行onLongClick和onClick
}
});
} public void myClick(View view) {
Log.e(TAG, "onClick:");
}
}

EditText

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <EditText
android:id="@+id/et"
android:hint="输入账号"
android:inputType="number"
android:drawableLeft="@drawable/ic_baseline_person_24"
android:drawablePadding="20dp"
android:paddingLeft="20dp"
android:textColorHint="#95a1aa"
android:layout_width="200dp"
android:layout_height="100dp"/> <EditText
android:hint="输入密码"
android:inputType="textPassword"
android:textColorHint="#95a1aa"
android:layout_width="200dp"
android:layout_height="100dp"/> <Button
android:id="@+id/btn"
android:text="获取账号"
android:layout_width="200dp"
android:layout_height="100dp"/> </LinearLayout>
package com.example.myedittext;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText; public class MainActivity extends AppCompatActivity { private static final String TAG = "wmj";
private EditText et; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); Button btn = findViewById(R.id.btn);
et = findViewById(R.id.et); btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String str = et.getText().toString();
Log.e(TAG, "输入的账号为:" + str);
}
});
}
}

ImageView

<ImageView
android:src="@drawable/pic1"
android:scaleType="centerInside"
android:layout_width="200dp"
android:layout_height="200dp"/> <ImageView
android:src="@drawable/pic2"
android:maxHeight="300dp"
android:maxWidth="300dp"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

ProgressBar

<ProgressBar
android:id="@+id/pb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <Button
android:onClick="myClick"
android:text="显示隐藏进度条"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> <ProgressBar
android:id="@+id/pb2"
style="@style/Widget.AppCompat.ProgressBar.Horizontal"
android:max="100"
android:indeterminate="true"
android:layout_width="300dp"
android:layout_height="100dp"/> <Button
android:onClick="load"
android:text="模拟下载"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
package com.example.myprogressbar;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.ProgressBar; public class MainActivity extends AppCompatActivity { private ProgressBar progressBar;
private ProgressBar progressBar2; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); progressBar = findViewById(R.id.pb);
progressBar2 = findViewById(R.id.pb2);
} public void myClick(View view) {
if(progressBar.getVisibility() == View.GONE){
// 若隐藏则显示
progressBar.setVisibility(View.VISIBLE);
}else{
progressBar.setVisibility(View.GONE);
}
} public void load(View view) {
int progress = progressBar2.getProgress();
progress += 10;
progressBar2.setProgress(progress);
}
}

ToolBar

  • 在themes.xml中关闭toolbar:parent="Theme.MaterialComponents.DayNight.NoActionBar
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <androidx.appcompat.widget.Toolbar
android:id="@+id/tb"
android:background="#ffff00"
app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
app:title="主标题"
app:titleTextColor="#ff0000"
app:titleMarginStart="50dp"
app:subtitle="子标题"
app:subtitleTextColor="#00ff00"
app:logo="@mipmap/ic_launcher"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"/> <androidx.appcompat.widget.Toolbar
android:id="@+id/tb2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp"
android:background="#ffff00"/> <androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_marginTop="10dp"
app:navigationIcon="@drawable/ic_baseline_arrow_back_24"
android:background="#ffff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:text="标题"/>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
package com.example.mytoolbar;

import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar; import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View; public class MainActivity extends AppCompatActivity { @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // 注意是import androidx.appcompat.widget.Toolbar;
Toolbar toolbar = findViewById(R.id.tb);
Toolbar toolbar2 = findViewById(R.id.tb2); toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "点击了导航栏返回按钮");
}
}); toolbar2.setNavigationIcon(R.drawable.ic_baseline_arrow_back_24);
toolbar2.setTitle("标题");
toolbar2.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "点了");
}
});
}
}

AlertDialog

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="#FFFF00"
xmlns:android="http://schemas.android.com/apk/res/android"> <ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@mipmap/ic_launcher"/> <TextView
android:text="哈哈"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="显示对话框"
android:onClick="myClick"/>
</LinearLayout>
package com.example.myalterdialog;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void myClick(View view) { View dialog_view = getLayoutInflater().inflate(R.layout.dialog_view, null); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.mipmap.ic_launcher)
.setTitle("对话框")
.setMessage("对话框内容")
.setView(dialog_view)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "确定");
}
})
.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "取消");
}
})
.setNeutralButton("中间", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Log.e("wmj", "中间");
}
}) // 以上返回的都是Builder,可以变换顺序
.create() // 返回的是AlertDialog,不能放在前面
.show();
}
}

PopupWindow

popup_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@mipmap/ic_launcher"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:id="@+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮1"
android:padding="5dp"
android:textSize="18sp"/> <Button
android:id="@+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="按钮2"
android:padding="5dp"
android:textSize="18sp"/> </LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="horizontal"
xmlns:android="http://schemas.android.com/apk/res/android"> <Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="myClick"
android:text="弹出窗口"/>
</LinearLayout>
package com.example.mypopupwindow;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.PopupWindow; public class MainActivity extends AppCompatActivity { @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
} public void myClick(View view) {
View popupview = getLayoutInflater().inflate(R.layout.popup_view, null); Button btn1 = popupview.findViewById(R.id.btn1);
Button btn2 = popupview.findViewById(R.id.btn2); // 最后一个参数加上后,点击空白处popupwindow会消失
PopupWindow popupWindow = new PopupWindow(popupview, ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, true); popupWindow.setBackgroundDrawable(getResources().getDrawable(R.drawable.ic_baseline_airline_seat_recline_extra_24)); // popupWindow.showAsDropDown(view);
popupWindow.showAsDropDown(view, view.getWidth(), -view.getHeight()); btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "按钮1");
popupWindow.dismiss();
}
}); btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Log.e("wmj", "按钮2");
popupWindow.dismiss();
}
});
}
}

基础控件(Button,Edittext,ImageView,ProgressBar,ToolBar,AlertDialog,PopupWindow)的更多相关文章

  1. UWP&WP8.1 基础控件—Button

    Button作为最常用的控件,没有特别难的用法,是一个非常简单,可以很快就掌握的控件. Button 基础用法: 同样,在UWP项目中,可以从工具箱中拖拽到面板中进行使用.也可以使用XAML语法进行编 ...

  2. Android基础控件Button的使用

    1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...

  3. Android_基础控件

    目录 一.文本控件TextView 二.按钮控件Button 三.图片控件ImageView 四.输入控件EditText 一.文本控件TextView 1.布局文件 <TextView and ...

  4. React Native环境搭建以及几个基础控件的使用

    之前写了几篇博客,但是没有从最基础的开始写,现在想了想感觉不太合适,所以现在把基础的一些东西给补上,也算是我从零开始学习RN的经验吧! 一.环境搭建 首先声明一下,本人现在用的编辑器是SublimeT ...

  5. MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...

  6. android学习日记03--常用控件button/imagebutton

    常用控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件的一些简单而可见的功能.所有控件都是继承View类 介绍android原生提供几种常用的控件bu ...

  7. Delphi一共封装(超类化)了8种Windows基础控件和17种复杂控件

    超类化源码: procedure TWinControl.CreateSubClass(var Params: TCreateParams; ControlClassName: PChar); con ...

  8. Python Tkinter基础控件入门实例

    分享一个Python Tkinter基础控件用法的入门例子,包括窗口的显示.显示内置图片.弹出窗口.菜单等. 例子,Python Tkinter基础控件的用法 # -*- coding: utf-8 ...

  9. VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)

    言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...

  10. SilverLight:基础控件使用(1)

    ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...

随机推荐

  1. MindSpore 框架的官方预训练模型的加载 —— MindSpore / hub 的安装

    MindSpore计算框架提供了一个官方版本的预训练模型存储库,或者叫做官方版本的预训练模型中心库,那就是 MindSpore / hub . 首先我们需要明确概念: 第一个就是 mindspore_ ...

  2. Hutool常用工具类

    1.背景 实际开发中经常用到很多的工具类,这里hutool提供了一系列的工具类,下面重点介绍常用的工具类. 2.使用步骤 官方文档:https://hutool.cn/docs/#/ 添加依赖 < ...

  3. Apache DolphinScheduler 如何实现自动化打包+单机/集群部署?

    Apache DolphinScheduler 是一款开源的分布式任务调度系统,旨在帮助用户实现复杂任务的自动化调度和管理.DolphinScheduler 支持多种任务类型,可以在单机或集群环境下运 ...

  4. 题解:SP22382 ETFD - Euler Totient Function Depth

    题目链接: link,点击这里喵. 前置知识: [模板]线性筛素数,欧拉函数,点击这里喵. 题意简述: 给定整数 $l,r,k$,求出 $[l,r]$ 中有多少个整数不断对自己取欧拉函数刚好 $k$ ...

  5. 斐讯 N1 刷机记录

    Prerequisites USB 公对公线 Windows 操纵系统 AMLogic USB Burning Tool,安装后名字为 Aml_Burn_Tool. 降级 打开终端,输入 hdwwiz ...

  6. macOS 查看网络接口信息

    networksetup -listallhardwareports 执行结果: Hardware Port: Ethernet Adapter (en4) Device: en4 Ethernet ...

  7. MySQL的索引原理及使用

    MySQL中的索引模型 Mysql中的索引使用的数据结构一般为搜索树,这里的搜索树,一般使用B树,这里补一下数据结构中的B树结构:说B树之前,先顺一个前置的知识点,平衡二叉树: 平衡二叉树 二叉树应该 ...

  8. Spark Dataframe 转 Json

    import org.apache.spark.sql.DataFrame import org.apache.spark.sql.functions._ import org.apache.spar ...

  9. Coursera, Big Data 5, Graph Analytics for Big Data, Week 1/2

    Graph表示 1. adjacency matrix最简单的一种表示:行是From 列是To, 这种表示是稀疏矩阵 2. 另一种表示,如下图,很多graph数据库用这种,是的数据库操作更有效率 us ...

  10. vim命令之多行注释

    vim常用命令之多行注释和多行删除 vim中多行注释和多行删除命令,这些命令也是经常用到的一些小技巧,可以大大提高工作效率. 1.多行注释: 1. 首先按esc进入命令行模式下,按下Ctrl + v, ...