Android Studio點擊事件監聽與Toast


本篇為跟隨谷粒學院Android教程學習寫的回顧,多有疏漏,歡迎批評指正!

Android Studio的第二個應用:簡單模擬下載

知識點:

1.應用的創建與運行;

2.界面布局的定義與加載;

3.點擊事件監聽;

4.Toast文本小提示;

效果預覽:

 

 MainActivity.java文件:

package com.example.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) {
super.onCreate(savedInstanceState);
//加載布局,並生成對應的視圖對象
setContentView(R.layout.activity_main);

//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("正在下載中...");
}
});
}
}

點擊activity_main_xml文件后,選擇Design視圖,找到需要的空間拖拽至模擬器中。

 

 常用控件:

TextView 文本框

EditText 可輸入文本框

AutoCompleteTextView 自動匹配文本內容

MutiAutoCompleteTextView 支持多次自動匹配文本內容

ImageView 圖片

Botton 按鈕 //本次應用所使用的控件

ImageButton 圖片按鈕

ToggleButton 多狀態按鈕

CheckBox 復選框

RadioButton 單選按鈕

activity_main_xml文件:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.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" />

</android.support.constraint.ConstraintLayout>

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM