AndroidStudio点击事件监听与Toast 本篇为跟随⾕粒学院Android教程学习写的回顾,多有疏漏,欢迎批评指正!
Android Studio的第⼆个应⽤:简单模拟下载
知识点:
1.应⽤的创建与运⾏;
2.界⾯布局的定义与加载;
3.点击事件监听;
4.Toast⽂本⼩提⽰;
效果预览:
MainActivity.java⽂件:
ample.why.work0115;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button btn_main_download;
@Override
protected void onCreate(Bundle savedInstanceState) {
//加载布局,并⽣成对应的视图对象
setContentView(R.layout.activity_main);
点击activity_main_xml ⽂件后,选择Design
视图,到需要的空间拖拽⾄模拟器中。
常⽤控件:
TextView ⽂本框
EditText 可输⼊⽂本框
AutoCompleteTextView ⾃动匹配⽂本内容
MutiAutoCompleteTextView ⽀持多次⾃动匹配⽂本内容
ImageView 图⽚
Botton 按钮 //本次应⽤所使⽤的控件
ImageButton 图⽚按钮
ToggleButton 多状态按钮
CheckBox 复选框
RadioButton 单选按钮
activity_main_xml ⽂件:
//1.得到Button 对象
btn_main_download=(Button)findViewById(R.id.btn_main_download);
//2.给Button 设置点击的监听
btn_main_download.setOnClickListener(new  View.OnClickListener(){
@Override
public  void  onClick(View v){//点击设置监听的Button 时调⽤
//1).提⽰开始下载的⽂本⼩提⽰
Toast.makeText(MainActivity.this  , "开始下载..." , Toast.LENGTH_SHORT).show();
//2).更新Button 显⽰的⽂本
btn_main_download.setText("正在下载中...");
}
});android学习教程
}
}
<?xml version="1.0" encoding="utf-8"?>
<straint.ConstraintLayout xmlns:android ="schemas.android/apk/res/android"xmlns:app ="schemas.android/apk/res-auto"
xmlns:tools ="schemas.android/tools"
android:layout_width ="match_parent"
android:layout_height ="match_parent"
tools:context ="ample.why.work0115.MainActivity">
<Button
android:id ="@+id/btn_main_download"
android:layout_width ="wrap_content"
android:layout_height ="wrap_content"
android:text ="@string/download"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="16dp"/>
</straint.ConstraintLayout>

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。