TextView和EditText

TextView

TextView创建链接

android:textIsSelectable="true"(文字可以被选中)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView android:text="@string/email"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="#6DBBDF"
        android:gravity="center"
        android:textColor="#FCE523"
        android:textSize="25sp"
        android:textStyle="bold"
        android:id="@+id/textview_email"/>

</RelativeLayout>

EditText

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:id="@+id/textview_email"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_alignParentTop="true"
        android:layout_marginStart="84dp"
        android:layout_marginTop="14dp"
        android:background="#6DBBDF"
        android:gravity="center"
        android:text="@string/email"
        android:textColor="#FCE523"
        android:textIsSelectable="true"
        android:textSize="25sp"
        android:textStyle="bold" />

    <EditText
        android:id="@+id/editText_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/textview_email"
        android:layout_alignEnd="@+id/textview_email"
        android:layout_alignParentStart="true"
        android:layout_marginStart="41dp"
        android:layout_marginTop="14dp"
        android:layout_marginEnd="-41dp"
        android:drawableBottom="@drawable/ic_launcher_foreground"
        android:hint="请输入姓名..."
        android:visibility="visible"
        android:inputType="text"/>

</RelativeLayout>
package com.example.android03;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    private TextView textview_email;
    private EditText editText_name;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //绑定布局文件
        setContentView(R.layout.activity_main);

        //根据ID查找组件
        textview_email = (TextView) findViewById(R.id.textview_email);

        textview_email.setText("[email protected]");

        editText_name = (EditText)findViewById(R.id.editText_name);
        editText_name.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {//输入文字前
                System.out.println("beforeTextChanged--"+s);
            }

            @Override//输入文字时
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                System.out.println("onTextChanged--"+s);
            }

            @Override//输入文字后
            public void afterTextChanged(Editable s) {
                System.out.println("afterTextChanged--"+s);
            }
        });
        //监听回车键
        editText_name.setOnEditorActionListener(new TextView.OnEditorActionListener(){
            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event){
                Toast.makeText(MainActivity.this,v.getText().toString(),Toast.LENGTH_LONG).show();
                return true;
            }
        });
    }


}

猜你喜欢

转载自blog.csdn.net/asjklm/article/details/88360550