第三单元 控件

news/2024/10/10 3:23:22

第三单元控件

Button控件

知识点一:如何设置点击事件

(1)方法一:

首先对xml中想要设置点击的控件添加onclick属性

即android onclick=“点击后要进行的函数名”

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical">
<Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮1"android:onClick="click"/><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮2"/><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮3"/>
</LinearLayout>

 然后在java文件里面写点击函数传入参数View表示实体也就是当前操作他的那个控件

创建私有对象变量Button a再在onCreate方法里面用id获取他->目的是:等会儿想在点击函数里写他怎么变

MainActivity.java
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {
private Button a;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);a=findViewById(R.id.button1);}public  void  click(View v){a.setText("按钮一被点击");}
}

也可以按哪个Button哪个Button变不一定非要Button1,按照下面这么改

MainActivity.java
 package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.widget.Button;
import android.view.View;
public class MainActivity extends AppCompatActivity {@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);}public  void  click(View v){((Button)v).setText("按钮一被点击");}
}

这种方法的关键是要导包import android.view.View;

下面两个案例

1.按按钮3变按钮1文字

activity_main.xml
 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical">
<Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮1"/><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮2"/><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮3"
android:onClick="click1"/>
</LinearLayout>

 

MainActivity.java
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;
import android.widget.Button;public class MainActivity extends AppCompatActivity {
private Button a;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);a=findViewById(R.id.button1);}public void click1(View v){a.setText("我爱你");}
}

 案例2:点击按钮3变最上面的文字(TextView里)

 

activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"android:layout_height="match_parent"xmlns:android="http://schemas.android.com/apk/res/android"android:orientation="vertical"><TextViewandroid:id="@+id/txt1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/love"/>
<Buttonandroid:id="@+id/button1"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮1"/><Buttonandroid:id="@+id/button2"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮2"/><Buttonandroid:id="@+id/button3"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="按钮3"
android:onClick="click1"/>
</LinearLayout>
MainActivity.java
package com.example.myapplication;import androidx.appcompat.app.AppCompatActivity;import android.os.Bundle;import android.view.View;
import android.widget.Button;
import android.widget.TextView;public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);a=findViewById(R.id.button1);t=findViewById(R.id.txt1);}public void click1(View v){a.setText("我爱你");t.setText("qwe");}
}

第二种方法匿名内部类

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click"

/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"

/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"

/>
</LinearLayout>
 
对按钮1实行匿名内部类来实现点击事件
package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
private Button a;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {这个函数名onClick不能变
a.setText("I love you!");
}
});
}

}
 

 

 

方法3 接口

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button a;
private Button b;
private TextView t;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
a=findViewById(R.id.button1);
t=findViewById(R.id.txt1);
a.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
a.setText("I love you!");
}
});
b=findViewById(R.id.button2);
b.setOnClickListener(this);
}

public void click1(View v){
a.setText("我爱你");
t.setText("qwe");
}

@Override
public void onClick(View v) {
b.setText("WMY");
}
}
 
 
 
 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
>
<TextView
android:id="@+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/love"
/>
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"
android:onClick="click"

/>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"
android:onClick="click2"
/>
<Button
android:id="@+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"
android:onClick="click1"
/>
</LinearLayout>
绿笔是关键,先写
b=findViewById(R.id.button2);
b.setOnClickListener(this);
然后this变红报错
然后点中this,Alt+Enter弹出这个选第二个实现接口

然后选第一个

最后在Onclick方法里写点击这个Button后你想干的

public void onClick(View v) {
b.setText("WMY");
}
 

自适应(多语言)

第一步new resource file

第二步local,然后filename取strings

最后选地区ok

然后分别在每个语言的strings.xml下写代码,注意那个没有标语言的是默认strings.xml就是没有选语言默认显示

 

java文件

package com.example.myapplication;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
TextView txt;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txt=findViewById(R.id.txt);

}
public void click001(View v){
txt.setText(R.string.app_name);
//显示strings.xml里greetings里对应那句话
Toast.makeText(MainActivity.this,R.string.greetings,Toast.LENGTH_LONG).show();;

}
}

语言在设置里选

选完再打开app就是对应该语言下应该显示的(在他自己的strings.xml里设置)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.ryyt.cn/news/27737.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈,一经查实,立即删除!

相关文章

Adobe InDesign 2024 v19.4 (macOS, Windows) - 版面设计和桌面出版软件

Adobe InDesign 2024 v19.4 (macOS, Windows) - 版面设计和桌面出版软件Adobe InDesign 2024 v19.4 (macOS, Windows) - 版面设计和桌面出版软件 Acrobat、After Effects、Animate、Audition、Bridge、Character Animator、Dimension、Dreamweaver、Illustrator、InCopy、InDe…

Adobe InCopy 2024 v19.4 (macOS, Windows) - 编写和副本编辑软件

Adobe InCopy 2024 v19.4 (macOS, Windows) - 编写和副本编辑软件Adobe InCopy 2024 v19.4 (macOS, Windows) - 编写和副本编辑软件 Acrobat、After Effects、Animate、Audition、Bridge、Character Animator、Dimension、Dreamweaver、Illustrator、InCopy、InDesign、Lightr…

题解:AT_abc352_C [ABC352C] Standing On The Shoulders

考场憋了很久,最后代码贼短……理想状态下,直接全排列解决问题。但是,\(1 \le n \le 2 \times 10^5\),明显 TLE,试都不用试的。 咋优化呢? 其实,前面的巨人只负责“打地基”,作为“塔尖儿”的巨人有且仅有 1 个。而前面地基随便排列,地基高度(他们的和)都不会变。所…

Apache Shiro 550反序列化漏洞

Shiro 框架提供了一个RememberMe功能,允许用户在下次访问时无需重新登录。这个功能通过在Cookie中设置一个rememberMe字段来实现。Shiro在处理rememberMe字段时,会先进行Base64解码,然后使用AES解密,最后反序列化。然而Shiro的默认AES密钥是硬编码在框架中的。目录漏洞原理…

.Net 8.0 下的新RPC,IceRPC之如何创建连接connection

作者引言很高兴啊,我们来到了IceRPC之如何创建连接connection,基础引导,让自已不在迷茫,快乐的畅游世界。如何创建连接connection学习如何使用IceRPC,创建和接受连接。连接有什么用途? 连接在 IceRPC 中发挥着核心作用: 通过连接向服务端发送请求,然后通过同一连接收到响应…

pde复习笔记 第一章 波动方程 第六节 能量不等式、波动方程解的唯一性和稳定性

能量不等式 这一部分需要知道的是能量的表达式 \[E(t)=\int_{0}^{l}u_{t}^{2}+a^{2}u_{x}^{2} dx \]一般而言题目常见的问法是证明能量是减少的,也就是我们需要证明 \[\dfrac{d}{dt}E(t) \le0 \]在计算\(\dfrac{d}{dt}E(t) \le0\)的时候一定会用的题目给的方程条件去凑微分…

2024 年 5 月 4 日 青年节 周六 多云 常(910 字)

正文看完了《只有街舞》系列的纪录片。每次看完这种类型的片子,总会激发我许多感触。我总是想书写一个庞大而宏伟的故事,通过故事和人物的行动折射背后深沉的主题。使命感、勇气、选择、放弃、未知、疲惫、克制、时间、迷茫、信念、坚持、自我感动、爱、友情、生活等等等等。…

Mac更新python3.12 解决pip3安装报错

Mac使用homebrew更新了python3.12,删除了以前的版本和pip3安装软件时候报错。error: externally-managed-environment This environment is externally managed ╰─> To install Python packages system-wide, try brew installxyz, where xyz is the package you are try…