分别在这两个地方写代码
写完代码在这里新建一个虚拟机并启动开机:
运行代码:
题目一:
Activity_main.xml中的代码如下图所示,请在MainActivity.java文件中编写代码,实现如下功能:
(1)点击按钮1时,在按钮1的地方显示文字“按钮1已被点击”(点击事件采用在布局文件中指定Button控件的onClick属性方式);
(2)点击按钮2时,在最下方显示文字“按钮2已被点击”,一段时间后文字消失(点击事件采用接口方式);
(3)点击按钮3时,在三个按钮下方显示文件“按钮3已被点击”(点击事件采用匿名内部类方式)。
<!--三个按钮点击事件XML-->
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<Button
android:id="@+id/btn_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮1"/>
<Button
android:id="@+id/btn_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮2"/>
<Button
android:id="@+id/btn_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="按钮3"/>
<TextView
android:id="@+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffff0000"
android:textSize="30sp"/>
</LinearLayout>
//三个按钮点击事件java
package com.example.a201906160055chenjinjian;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private Button btn01,btn02,btn03;
private TextView tw0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tw0=(TextView) findViewById(R.id.tv);
btn01=(Button) findViewById(R.id.btn_1);
btn02=(Button) findViewById(R.id.btn_2);
btn02.setOnClickListener(this::click);
btn03=(Button) findViewById(R.id.btn_3);
btn03.setOnClickListener(this::onClick);
//实现按钮1的点击
btn01.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
tw0.setText("按钮1被点击");
}
});
}
//按钮2点击
public void click(View view){
Toast.makeText(MainActivity.this, "按钮2被点击", Toast.LENGTH_SHORT).show();
}
//按钮3点击
public void onClick(View v){
switch (v.getId()){
case R.id.btn_3:
btn03.setText("按钮3被点击");
break;
}
}
}
题目二:
如图:
<