基础控件(Button,Edittext,ImageView,ProgressBar,ToolBar,AlertDialog,PopupWindow)
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)的更多相关文章
- UWP&WP8.1 基础控件—Button
Button作为最常用的控件,没有特别难的用法,是一个非常简单,可以很快就掌握的控件. Button 基础用法: 同样,在UWP项目中,可以从工具箱中拖拽到面板中进行使用.也可以使用XAML语法进行编 ...
- Android基础控件Button的使用
1.相关属性 Android的按钮有Button和ImageButton(图像按钮),Button extends TextView, ImageButton extends ImageView! a ...
- Android_基础控件
目录 一.文本控件TextView 二.按钮控件Button 三.图片控件ImageView 四.输入控件EditText 一.文本控件TextView 1.布局文件 <TextView and ...
- React Native环境搭建以及几个基础控件的使用
之前写了几篇博客,但是没有从最基础的开始写,现在想了想感觉不太合适,所以现在把基础的一些东西给补上,也算是我从零开始学习RN的经验吧! 一.环境搭建 首先声明一下,本人现在用的编辑器是SublimeT ...
- MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box)等.命令按钮就是我们前面多次提到的侠义的 ...
- android学习日记03--常用控件button/imagebutton
常用控件 控件是对数据和方法的封装.控件可以有自己的属性和方法.属性是控件数据的简单访问者.方法则是控件的一些简单而可见的功能.所有控件都是继承View类 介绍android原生提供几种常用的控件bu ...
- Delphi一共封装(超类化)了8种Windows基础控件和17种复杂控件
超类化源码: procedure TWinControl.CreateSubClass(var Params: TCreateParams; ControlClassName: PChar); con ...
- Python Tkinter基础控件入门实例
分享一个Python Tkinter基础控件用法的入门例子,包括窗口的显示.显示内置图片.弹出窗口.菜单等. 例子,Python Tkinter基础控件的用法 # -*- coding: utf-8 ...
- VS2010/MFC编程入门之二十二(常用控件:按钮控件Button、Radio Button和Check Box)
言归正传,鸡啄米上一节中讲了编辑框的用法,本节继续讲解常用控件--按钮控件的使用. 按钮控件简介 按钮控件包括命令按钮(Button).单选按钮(Radio Button)和复选框(Check Box ...
- SilverLight:基础控件使用(1)
ylbtech-SilverLight-Basic-Control:基础控件使用(1) 本文详解控件有: Label, TextBox, PasswordBox, Image, Button , Ra ...
随机推荐
- NVIDIA显卡原生管理查询功能nvidia-smi的部分使用功能
本文是使用NVIDIA原生管理工具查询NVIDIA显卡使用情况的一些记录(使用远程管理工具的效率没有使用原生管理接口nvml的效率高,有效率需求者建议使用python版本捆绑的nvml库,具体:htt ...
- MySQL手动执行rollback,内部实现分析
-- 测试手动回滚操作 -- 1手动开启事务 START TRANSACTION -- 2执行更新操作语句 UPDATE FraBakNtuAnalysize SET IsDeleted = 0 WH ...
- 无缝融入,即刻智能[一]:Dify-LLM大模型平台,零编码集成嵌入第三方系统,42K+星标见证专属智能方案[含ollama部署]
无缝融入,即刻智能[一]:Dify-LLM大模型平台,零编码集成嵌入第三方系统,42K+星标见证专属智能方案 1.Dify 简介 1.1 功能情况 Dify,一款引领未来的开源大语言模型(LLM)应用 ...
- Codeforces Round 911 (Div. 2) D
Codeforces Round 911 (Div. 2) D D. Small GCD 题意 定义\(f(a,b,c)\)为\(a,b,c\)中较小两个数的\(gcd\),给定数组\(a_{1... ...
- 安装nvm,并通过nvm安装nodejs
转载请注明出处: 1.安装nvm 打开终端,然后运行以下命令来下载并安装nvm: curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39 ...
- Chrome 开启多线程下载
打开 chrome://flags/#enable-parallel-downloading,将 Parallel downloading 设置为 Enabled 参考:为什么Chrome浏览器下载速 ...
- 【Java】之获取CSV文件数据以及获取Excel文件数据
一.获取CSV文件数据 import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Sheet; impor ...
- How to set keyboard for xshell 8 beta? 快捷键设置
今天发现xshell8 不能使用Ctrl + v,就找了一下攻略,发现版本不对,都是低版本的,针对还处于测试期间的xshell 8,没有攻略. 那就自己摸索吧,看了几眼,发现:xshell 8 挪位置 ...
- windows docker(25.0.3) 运行 1.4.1 nacos 容器
Docker Desktop 设定图标 -> Docker Engine 设定国内镜像源 添加配置: { "builder": { "gc": { &qu ...
- python pyqt6 颜色弹窗 QColorDialog
def setColor(self): # 避免窗口置顶后,Dialog被主窗口覆盖,所以需要传递self # 设定默认颜色使用getColor的第一个参数(使用setCurrentColor不生效) ...