Android編程之Button控件配合Toast控件用法分析
本文實例講述了Android編程之Button控件配合Toast控件用法。分享給大家供大家參考,具體如下:
在本章教程中,我們將會學習Button控件的使用,同時順便說一下Toast提示控件。
在Android程序開發(fā)中,我們使用最多的用戶交互控件可能就是Button的了,而我們使用最多的事件估計也就是onclick事件了。
這些事件也是最簡單的事件,我們一般通過google自帶的API接口就可以調用了,我們具體看看怎么做吧。
第一步。新建一個工程Ep.Toast,活動和主視圖名稱我都使用默認的了,設置一下視圖activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/textView1"
android:layout_marginRight="61dp"
android:text="確定" />
</RelativeLayout>
里面就是一個文字標簽和一個按鈕控件。
第二步。我們寫核心文件——MainActivity.java,在這里我會盡量多給你們注釋:
package com.example.ep.toast;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
private TextView txtview1;
private Button btn1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//綁定文字標簽控件
txtview1=(TextView)findViewById(R.id.textView1);
//綁定按鈕控件
btn1=(Button)findViewById(R.id.button1);
//添加一個點擊事件監(jiān)聽器并實例化一個點擊事件的監(jiān)聽
btn1.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//點擊按鈕后設置文字標簽
txtview1.setText("Joven");
//點擊按鈕后彈出提示框,里面的參數(shù)是(綁定活動,提示內容,顯示時間)
Toast.makeText(MainActivity.this, "Joven", Toast.LENGTH_LONG).show();
}
});
}
}
最后我們就可以看看效果了。
好了本章就講到這里了,這些雖然都是些基礎得不能再基礎得東西。但是不管多么龐大而復雜的項目,其實都是在這些東西上面建立起來的。知識都是積累才會變精華。
希望本文所述對大家Android程序設計有所幫助。
- android之自定義Toast使用方法
- Android控件系列之Toast使用介紹
- Android 5.0以上Toast不顯示的解決方法
- android自定義toast(widget開發(fā))示例
- android開發(fā)教程之實現(xiàn)toast工具類
- Android Service中使用Toast無法正常顯示問題的解決方法
- Android Toast的用法總結(五種用法)
- 超簡單實現(xiàn)Android自定義Toast示例(附源碼)
- Android超實用的Toast提示框優(yōu)化分享
- Android實現(xiàn)Toast提示框圖文并存的方法
- Android編程實現(xiàn)Toast只顯示最后一條的方法
相關文章
Android中應用前后臺切換監(jiān)聽的實現(xiàn)詳解
這篇文章主要給大家介紹了關于Android中應用前后臺切換監(jiān)聽實現(xiàn)的相關資料,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面跟著小編來一起學習學習吧。2017-07-07

