新增button控件为其添加onClick事件及Text

来源:互联网 发布:叙永网络歌手大赛歌名 编辑:程序博客网 时间:2024/05/29 01:54
新增button控件为其添加onClick事件及Text

分类: Android平台

一、如何向一个XML文件添加button控件并添加onClick事件及button Text?

    以layout形式打开activity_main.xml,拖出一个Button控件,再进入activity_main.xml的XML形式:

image


  1. <Button
  2.     android:id="@+id/button1"
  3.     android:layout_width="wrap_content"
  4.     android:layout_height="wrap_content"
  5.     android:layout_alignLeft="@+id/textView1"
  6.     android:layout_below="@+id/textView1"
  7.     android:layout_marginLeft="33dp"
  8.     android:layout_marginTop="43dp"
  9.     android:text="@string/hello_button"
  10.     android:onClick="Btn1_Click"/>


1、设置onClick事件

(1)、在activity_main.xml相应位置添加行android:onClick="Btn1_Click"

(2)、进入MainActivity.java文件,依次执行下面的语句



  1. import android.widget.*;// 引入JAVA包

  2. Button button;// 创建一个button对象
  3. int count = 0;

  4. 在onCreate()方法中添加如下语句:
  5. button = (Button)findViewById(R.id.button1);


编写onClick方法


  1. public void Btn1_Click(Viewview)
  2. {
  3.     String str ="---->" + count;
  4.     textView.setText(str);
  5.     count++;
  6. }


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

二、运行效果

image

image

三、几个完整的代码文件

activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2.     xmlns:tools="http://schemas.android.com/tools"
  3.     android:layout_width="match_parent"
  4.     android:layout_height="match_parent"
  5.     android:paddingBottom="@dimen/activity_vertical_margin"
  6.     android:paddingLeft="@dimen/activity_horizontal_margin"
  7.     android:paddingRight="@dimen/activity_horizontal_margin"
  8.     android:paddingTop="@dimen/activity_vertical_margin"
  9.     tools:context=".MainActivity">

  10.     <Button
  11.         android:id="@+id/button1"
  12.         android:layout_width="wrap_content"
  13.         android:layout_height="wrap_content"
  14.         android:layout_alignLeft="@+id/textView1"
  15.         android:layout_below="@+id/textView1"
  16.         android:layout_marginLeft="33dp"
  17.         android:layout_marginTop="43dp"
  18.         android:text="@string/hello_button"
  19.         android:onClick="Btn1_Click"/>


  20.     <TextView
  21.         android:id="@+id/textView1"
  22.         android:layout_width="wrap_content"
  23.         android:layout_height="wrap_content"
  24.         android:layout_alignParentLeft="true"
  25.         android:layout_alignParentTop="true"
  26.         android:layout_marginLeft="46dp"
  27.         android:layout_marginTop="14dp"
  28.         android:text="@string/hello_world"/>

  29. </RelativeLayout>


MainActivity.java

  1. package com.example.project_062503;

  2. import android.os.Bundle;
  3. import android.app.Activity;
  4. import android.view.Menu;
  5. import android.view.View;
  6. import android.widget.Button;
  7. import android.widget.*;


  8. public class MainActivityextends Activity{

  9.     Button button;
  10.     TextView textView;
  11.     int count= 0;
  12.    
  13.     @Override
  14.     protected void onCreate(Bundle savedInstanceState){
  15.         super.onCreate(savedInstanceState);
  16.         setContentView(R.layout.activity_main);
  17.         button =(Button)findViewById(R.id.button1);
  18.         textView = (TextView)findViewById(R.id.textView1);
  19.     }

  20.     @Override
  21.     public boolean onCreateOptionsMenu(Menumenu) {
  22.         // Inflate the menu; this adds items to the action bar if it is present.
  23.         getMenuInflater().inflate(R.menu.main,menu);
  24.         return true;
  25.     }
  26.    
  27.     public void Btn1_Click(Viewview)
  28.     {       
  29.         String str = "---->" +count;
  30.         textView.setText(str);
  31.         count++;
  32.     }

  33. }
0 0
原创粉丝点击