新增button控件为其添加onClick事件及Text
分类: Android平台
一、如何向一个XML文件添加button控件并添加onClick事件及button Text?
以layout形式打开activity_main.xml,拖出一个Button控件,再进入activity_main.xml的XML形式:
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView1"
- android:layout_below="@+id/textView1"
- android:layout_marginLeft="33dp"
- android:layout_marginTop="43dp"
- android:text="@string/hello_button"
- android:onClick="Btn1_Click" />
1、设置onClick事件
(1)、在activity_main.xml相应位置添加行android:onClick="Btn1_Click"
(2)、进入MainActivity.java文件,依次执行下面的语句
- …
- import android.widget.*; // 引入JAVA包
- …
- Button button; // 创建一个button对象
- int count = 0;
- …
- 在onCreate()方法中添加如下语句:
- button = (Button)findViewById(R.id.button1);
编写onClick方法
- public void Btn1_Click(View view)
- {
- String str = "---->" + count;
- textView.setText(str);
- count++;
- }
2、设置button界面的text
(1)、在activity_main.xml相应位置添加android:text="@string/hello_button"
注意,这里是为string定义了一个名字,hello_button,它映射着一堆字符串。见(2)中介绍。
(2)、进入values/string.xml文件,对相应的进入变量进行修改
PROJECT_062503
Settings
Hello world,hello world
Hello man,hello man
Hello 062503
二、运行效果
三、几个完整的代码文件
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" >
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignLeft="@+id/textView1"
- android:layout_below="@+id/textView1"
- android:layout_marginLeft="33dp"
- android:layout_marginTop="43dp"
- android:text="@string/hello_button"
- android:onClick="Btn1_Click" />
- <TextView
- android:id="@+id/textView1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentLeft="true"
- android:layout_alignParentTop="true"
- android:layout_marginLeft="46dp"
- android:layout_marginTop="14dp"
- android:text="@string/hello_world" />
- </RelativeLayout>
MainActivity.java
- package com.example.project_062503;
- import android.os.Bundle;
- import android.app.Activity;
- import android.view.Menu;
- import android.view.View;
- import android.widget.Button;
- import android.widget.*;
- public class MainActivity extends Activity {
- Button button;
- TextView textView;
- int count = 0;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- button = (Button)findViewById(R.id.button1);
- textView = (TextView)findViewById(R.id.textView1);
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
-
- public void Btn1_Click(View view)
- {
- String str = "---->" + count;
- textView.setText(str);
- count++;
- }
- }