android组件:Button
实现的主要功能是当button按下的时候,将开始设置的TextView中的文本换成自己设定的文本,我这里是将文本设置成click。
先上效果:点击之前
button点击之后的效果:
layout中的xml文件:
layout中是设置了一个文本框和三个按钮
- <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"
- tools:context=".MainActivity" >
- <TextView
- android:id="@+id/myText" //给TextView取个名字
- android:layout_width="wrap_content" //设置TextView的宽度为字体包裹
- android:layout_height="wrap_content"
- android:text="@string/hello_world" //设置文本框中的内容是从strings.xml中读取的名为hello_world的文本
- android:background="#00ff00" //文本框背景的颜色
- android:textColor="#ff00ff" //设置字体颜色
- />
- //下面是添加Button,基本设置和TextView相同
- <Button
- android:id="@+id/myButton3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentRight="true"
- android:layout_below="@+id/myText"
- android:layout_marginRight="35dp"
- android:layout_marginTop="32dp"
- android:text="click3" />
- <Button
- android:id="@+id/myButton2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/myButton3"
- android:layout_alignBottom="@+id/myButton3"
- android:layout_marginRight="38dp"
- android:layout_toLeftOf="@+id/myButton3"
- android:text="click2" />
- <Button
- android:id="@+id/myButton"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignBaseline="@+id/myButton2"
- android:layout_alignBottom="@+id/myButton2"
- android:layout_alignParentLeft="true"
- android:layout_marginLeft="16dp"
- android:text="click1" />
- </RelativeLayout>
ButtonActivaty.java 文件:
- package com.example.buttonproject;
- import android.os.Bundle;
- import android.app.Activity;
- import android.text.method.ScrollingMovementMethod;
- import android.view.View;
- import android.widget.Button;
- import android.widget.TextView;
- import android.view.View.OnClickListener;
- public class MainActivity extends Activity {
- private Button bt1;
- private Button bt2;
- private Button bt3;
- private TextView tV;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- super.setContentView(R.layout.activity_main);
- //通过id号找到我们在layout布局中的main.xml文件中设置的一个TextView和三个Button
- tV = (TextView)findViewById(R.id.myText);
- bt1 = (Button)findViewById(R.id.myButton);
- bt2 = (Button)findViewById(R.id.myButton2);
- bt3 = (Button)findViewById(R.id.myButton3);
- //设置click事件监听,当有click事件发生时,将标题栏和我们设置的TextView的
- //文本设置成被按下的button的内容
- OnClickListener l = new OnClickListener() {
- @Override
- public void onClick(View v) {
- // 设置标题栏
- setTitle("Result:"+((TextView)v).getText());
- tV.setText("Result:"+((TextView)v).getText());
- }
- };
- //将三个button都绑定上刚刚设置的监听事件
- bt1.setOnClickListener(l);
- bt2.setOnClickListener(l);
- bt3.setOnClickListener(l);
- tV.setMovementMethod(ScrollingMovementMethod.getInstance());
- }
- }
转载于:https://blog.51cto.com/jackcheng/1124305