我是小白,刚学编程没多久,完全自学,这些也是在网上看的,加上自己总结,如有错误请指正。

再在MainActivity.java里添加实现代码,如:
- public void changeStr(View view) {
- textView.setText("按了第1个按钮。");
这个方法适合单个按钮,而且我觉得这个方法好像比较好理解,跟其他编程语文实现按钮功能差不多。
全部代码:
- findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
- public void onClick(View view) {
- textView.setText("按了第2个按钮");
- }
- });
方法1、2例程:
布局文件:
- <FrameLayout 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"
- tools:context=".MainActivity">
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="300dp"
- android:text="Hello World!"
-
- android:textSize="40dp" />
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="changeStr"
- android:text="onclick" />
-
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="50dp"
- android:text="listener" />
-
- FrameLayout>
MainActivity.java
- package com.example.button;
-
- 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;
-
- import java.io.DataOutputStream;
-
- public class MainActivity extends AppCompatActivity {
- private TextView textView;
- private Button button1;
- private Button button2;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- button1=(Button)findViewById(R.id.button1);
- textView=(TextView)findViewById(R.id.textView);
-
- findViewById(R.id.button2).setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- textView.setText("按了第2个按钮");
- }
- });
- }
-
- public void changeStr(View view) {
- textView.setText("按了第1个按钮。");
- }
- }
1.在oncreate中为控件绑定方法:
- findViewById(R.id.button1).setOnClickListener(this);
- findViewById(R.id.button2).setOnClickListener(this);
- findViewById(R.id.button3).setOnClickListener(this);
然后再在this上按alt+enter,调出下图,第2项回车后会再弹出一个窗口,再回车。我只知道这个补全代码,到底怎么回事我也不知道。


2.重写onclick.
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.button1:
- textView.setText("按了第1个按钮");
- break;
- case R.id.button2:
- textView.setText("按了第2个按钮");
- break;
- case R.id.button3:
- textView.setText("按了第3个按钮");
- break;
- }
- }
方法3例程:
布局文件:
- <FrameLayout 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"
- tools:context=".MainActivity">
-
- <TextView
- android:id="@+id/textView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="300dp"
- android:text="Hello World!"
-
- android:textSize="40dp" />
-
- <Button
- android:id="@+id/button1"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
-
- android:text="onclick" />
-
- <Button
- android:id="@+id/button2"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="50dp"
- android:text="listener" />
-
- <Button
- android:id="@+id/button3"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginTop="100dp"
- android:text="重写onclick" />
-
- FrameLayout>
MainActivity.java
- package com.example.button;
-
- 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;
- import java.io.DataOutputStream;
-
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private TextView textView;
- private Button button1;
- private Button button2;
- private Button button3;
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- findViewById(R.id.button1).setOnClickListener(this);
- findViewById(R.id.button2).setOnClickListener(this);
- findViewById(R.id.button3).setOnClickListener(this);
- textView=(TextView)findViewById(R.id.textView);
-
- }
-
- @Override
- public void onClick(View view) {
- switch (view.getId()){
- case R.id.button1:
- textView.setText("按了第1个按钮");
- break;
- case R.id.button2:
- textView.setText("按了第2个按钮");
- break;
- case R.id.button3:
- textView.setText("按了第3个按钮");
- break;
- }
- }
- }