簡介:
APP設計最基礎的Button,是我們常用的元件之一,以下介紹基本用法。
用法:
1.在layout中新增Button。
2.在 java 程式中加入Button 物件,並使用"findViewById"建立對應的Button名稱。
3.新增"setOnClickListener"方法來監聽按鈕狀態。
4.在"setOnClickListener"中設定當按鈕觸發時所執行的動作。
layout 部分:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/textview"
android:text=""
/>
<Button
android:id="@+id/button"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_x="60px"
android:layout_y="60px"
android:text="Hello APP!"
/>
</AbsoluteLayout>
Java 部分:
package com.button;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class button extends Activity {
private TextView text;
private Button button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
text = (TextView)findViewById(R.id.textview);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new Button.OnClickListener(){
@Override
public void onClick(View v)
{
//設定按鈕觸發時所執行的動作
text.setText("Hello user!!");
}
});
}
}
執行結果:
按下"Hello APP!"的按鈕後,畫面會顯示"Hello user!!"
此為 Button 的基本用法。
---------------------------------------------
謝謝你來到筆者的部落格
此部落格為記錄一些研究的筆記,希望有幫助到你\(^.^)/
如果喜歡筆者的文章請在下方留言處給我個推歐~
有任何意見&問題歡迎至下方留言,一起討論、一起進步!
再次感謝你~
請先 登入 以發表留言。