IntelliJ IDEA 安卓点击按钮修改文字

1.前端xml设计

xml学一些基本的就可以,实际应用中我们可以用UI界面操作(其实Androidstudio也是有UI界面的)
这样还能让我对于开发产生点兴趣,否则写代码太抽象了,一点意思没有。我就放弃了
在这里插入图片描述
一般我都不看代码视图,当然代码敲得很6的除外
在这里插入图片描述

2.改变文字TextView

关于逻辑方面我们要编写的是java的文件
1.先给文字添加一个id
2.然后获取这个页面的id
3.然后改变文本内容

添加ID我们在设计视图就可以添加啊
在这里插入图片描述
然后代码视图就变成了这样

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
  <TextView
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="Hello World!"
          app:layout_constraintBottom_toBottomOf="parent"
          app:layout_constraintLeft_toLeftOf="parent"
          app:layout_constraintRight_toRightOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          android:id="@+id/hello"/>
</androidx.constraintlayout.widget.ConstraintLayout>

然后我们编辑MainActivity文件

package com.weijun901.app;

import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*上面的内容不用管它,那是页面初始化的内容
      我们也要在页面初始化里面写个内容,以方便展示
    */
    //创建一个TextView对象,通过findViewById方法得到TextView对象id为hello的值,给hello变量
    TextView hello = findViewById(R.id.hello);
    //使用setText()方法修改文本
    hello.setText("Hello,My World!");
  }
}

在这里插入图片描述
果然Hello World!
变成了Hello,My World!
在这里插入图片描述
这是在项目初始化完毕的时候,文字就自动改变了。
如果想点击按钮后改变呢?

3.点击按钮,改变文字

我们需要在前端添加一个按钮,并且给按钮也加个id
在这里插入图片描述
当我尝试修改的按钮上的文字的时候,把"button"改成"改变"的时候,软件提示我要提取字符串资源,我不明白是啥意思,现在明白了。
它是把按钮的文字当成了字符串数据,一起汇总到了这里
在这里插入图片描述
就是这个文件夹下的xml文件,点开看看,就是这个结构
在这里插入图片描述
点开编辑,还可以手动进行添加,用于管理字符串资源数据,看来按钮的text属性是按照字符串自动打包的
在这里插入图片描述
不用管它,我们继续,修改MainActivity文件

package com.weijun901.app;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    /*上面的内容不用管它,那是页面初始化的内容
      我们也要在页面初始化里面写个内容,以方便展示
    */
    //创建一个TextView对象,通过findViewById方法得到TextView对象id为hello的值,给hello变量
    TextView hello = findViewById(R.id.hello);
    //创建一个Button对象,
    Button button1 = findViewById(R.id.button1);
    //对按钮点击事件进行监听,按钮被点击时,改变id为hello的文本内容
    button1.setOnClickListener(new View.OnClickListener(){
      @Override
      public void onClick(View v){
        //使用setText()方法修改文本
        hello.setText("Hello,My World!");
      }
    });

  }
}

软件给了个黄叹号提示
在这里插入图片描述
我点击右键显示快速修复,替换为lambda
在这里插入图片描述
发现了语法糖,箭头函数
之前这样

button1.setOnClickListener(new View.OnClickListener(){
  @Override
  public void onClick(View v){
    //使用setText()方法修改文本
    hello.setText("Hello,My World!");
  }
});

现在变成了这样

button1.setOnClickListener(v -> {
  //使用setText()方法修改文本
  hello.setText("Hello,My World!");
});

省了好多啊,我去,还真好用
重载一下,果然点击按钮进行了改变
在这里插入图片描述
如果想点击按钮,两个文字来回变呢?

4.点击按钮,生成随机数

直接贴代码了
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".MainActivity">
  <LinearLayout
          android:orientation="vertical"
          android:layout_width="200dp"
          android:layout_height="wrap_content"
          app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"
          app:layout_constraintTop_toTopOf="parent"
          app:layout_constraintBottom_toBottomOf="parent" android:gravity="center">
    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/hello" android:gravity="center" android:text="Hello World!" android:textSize="75dp"
            android:textColor="#E91E63"/>
    <Button
            android:text="@string/change"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" android:id="@+id/button"/>
  </LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

MainActivity

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //上面的内容不用管它,那是页面初始化的内容
        //我们也要在页面初始化里面写个内容,以方便展示
        TextView hello = findViewById(R.id.hello);
        //创建一个TextView对象,通过findViewById方法得到TextView对象id为hello的值,给hello变量
        Button button = findViewById(R.id.button);
        //创建一个Button对象,通过findViewById方法得到Button对象id为button的值,给button变量

        //对按钮点击事件进行监听,按钮被点击时,改变id为hello的文本内容
        button.setOnClickListener(v -> {
            int num = (int) (Math.random()*100);
            hello.setText(String.valueOf(num));
        });
    }
}

换了个手机
在这里插入图片描述

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

望天吼

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值