TextView控件可以向用户展现文本信息,在xml文件中分别定义了TextView和Button,并为Button添加了绑定事件,通过按钮事件改变TextView显示的内容。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/TextView1"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center"
android:background="#00ffff"
android:text="TextView"
/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="false"
android:text="ChangeText"
android:onClick="ChangeText"
/>
</LinearLayout>
在Activity中实现按钮绑定事件:
package com.example.whs.textandroid;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity{
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
textView = (TextView)findViewById(R.id.TextView1);
}
public void ChangeText(View view){
textView.setText("ChengText");
}
}