安卓开发之控件TextView和EditView

本文详细介绍了Android开发中TextView和EditText的基本用法及注意事项,包括如何在代码中使用这两个控件,设置提示文本,获取输入值等。同时,还提供了在Android Studio中添加注释的方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

前置小知识点
有关widget的一些介绍:
如何在Android Studio中添加注释:

https://jingyan.baidu.com/article/86f4a73e85f44c37d65269fb.html

TextView和EditView

1.清单文件(AndroidMainfiest)中为默认
2.类中文件代码如下:

package com.bluelesson.myeditview;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.EditText;//这个头文件必须得有,否则报can't resolve symbol "edittext"之类的错误
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //获取EditText对象
        EditText editText = findViewById(R.id.edit_text);
        editText.setHint("请输入你的电话号");

        editText.setHint(R.string.hInt);

        //获取EditText中的值
        String edit = editText.getText().toString();


        editText.setText("1300019383");
        //获取TextView对象
        TextView textView = findViewById(R.id.text_view);
        textView.setText("请输入你的电话号码:");
    }
}

布局文件(layout下的activity_main.xml)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
    android:padding="10dp"
    tools:context="com.bluelesson.myeditview.MainActivity">

    <TextView
        android:id="@+id/text_view"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="牛人:" />

    <EditText
        android:id="@+id/edit_text"
        android:inputType="phone"
        android:hint="请输入你的大名!!"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />


</LinearLayout>

values里的strings.xml文件:

<resources>
    <string name="app_name">MyEditor</string>
    <string name="hInt">请输入您的电话</string>
</resources>

其他文件为默认

小结:
  • 一般一个安卓模块会涉及到清单文件,类文件,以及资源文件,(这里没有涉及到全部,知识部分),如下图:
    这里写图片描述

  • TextView控件中的文字一般不好编辑,EditView控件里的文字是可以编辑的

最终效果图:
这里写图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值