安卓菜鸟的自学笔记——1:edittext,button,TextView的简单应用

思路原理:
编辑框输入文本,按确定键,会在下方显示,按清除键,会同时清除编辑框和下面的内容
直接上效果图
在这里插入图片描述
在这里插入图片描述
核心代码:

<EditText
        android:id="@+id/edit_1"
        android:layout_width="280dp"
        android:layout_height="50dp"
        android:hint="请输入文字,按确定键结束"
        android:singleLine="true"
        />
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        >

        <Button
            android:id="@+id/btn_1"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="确定"
            android:textSize="20dp"
            />
        <Button
            android:id="@+id/btn_2"
            android:layout_width="100dp"
            android:layout_height="50dp"
            android:text="清除"
            android:textSize="20dp"
            />

    </LinearLayout>

    <TextView
        android:id="@+id/text_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="20dp"
        android:layout_marginStart="10dp"
        android:textSize="30dp"

        />

java部分:
此段代码添加在 ***setContentView(R.layout.activity_main);***下面

/**
 * 按键实例化
 */
        Button button=(Button)findViewById(R.id.btn_1);
        Button button2=(Button)findViewById(R.id.btn_2);
/**
 * 确定按键的监听以及事件处理
 */
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditText editText=(EditText)findViewById(R.id.edit_1);
                String temp=editText.getText().toString();                  //获取编辑框的内容
                TextView textView =(TextView)findViewById(R.id.text_1);
                textView.setText(temp);                 //设置文本
            }
        });
/**
 * 清除按键的监听以及事件处理
 */
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TextView textView =(TextView)findViewById(R.id.text_1);
                textView.setText(null);                                     //设置   空  文本
                EditText editText=(EditText)findViewById(R.id.edit_1);
                editText.setText(null);

            }
        });

核心知识:
Button的相关事件

Button button=(Button)findViewById(R.id.btn_1);
//第1步:实例化button
//第2步:使用findViewById对button进行绑定

Edittext相关事件

EditText editText=(EditText)findViewById(R.id.edit_1);
String temp=editText.getText().toString();                  //获取编辑框的内容赋值给temp

TextView相关事件

TextView textView =(TextView)findViewById(R.id.text_1);
textView.setText("这是TextView的内容");                 //设置文本

经常更新自己的学习笔记,博客里有什么问题提都可以评论留言,我们一起学习进步。

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值