这里写自定义目录标题
实现Android按钮功能的几种方法:
1.在activity上实现public class Activity2 extends AppCompatActivity implements View.OnClickListener
2.在监听类上实现 class Listener implements View.OnClickListener
3.用Textview实现Button功能(新版的Android似无法使用switch寻址不同id的button)
第一种方法:public class Activity2 extends AppCompatActivity implements View.OnClickListener
设置监听后,用id进行分别寻址
package com.example.test921;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private TextView textView;
private EditText editText;
private Button bnt;
private Button bnt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=this.findViewById(R.id.textView);
editText=this.findViewById(R.id.editText);
bnt=this.findViewById(R.id.button);
bnt2=this.findViewById(R.id.button2);
//设置按钮的监听,使用共同的监听方法,再用if else根据id寻址不同的button
bnt.setOnClickListener(this);
bnt2.setOnClickListener(this);
}
@Override
public void onClick(View view)
{
if(view.getId()==R.id.button)
{System.out.println("button1被按下");
String name=editText.getText().toString().trim();
textView.setText(name);}
else if(view.getId()==R.id.button2)
System.out.println("button2被按下");
}
}
第二种方法:将implements写在监听类上
package com.example.test921;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import androidx.appcompat.app.AppCompatActivity;
public class Activity2 extends AppCompatActivity {
private TextView textView;
private EditText editText;
private Button bnt;
private Button bnt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView=this.findViewById(R.id.textView);
editText=this.findViewById(R.id.editText);
bnt=this.findViewById(R.id.button);
bnt2=this.findViewById(R.id.button2);
//两个按钮绑定到一个监听类,两种写法都可以
Listener listener=new Listener();
bnt.setOnClickListener(listener);
//如果不写Listener listener=new Listener();就直接在括号内new
bnt2.setOnClickListener(new Listener());
}
class Listener implements View.OnClickListener{
@Override
public void onClick(View view) {
if(view.getId()==R.id.button2)
System.out.println("button2");
else if (view.getId()==R.id.button) {
System.out.println("button");
}
}
}
}
第三种方法:由于新版似乎不允许button根据id用switch语句,所以可以改用textview
public class MainActivity extends AppCompatActivity {
private TextView input;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = this.findViewById(R.id.input);
}
public void oneOnClick(View view){
if(view instanceof TextView){
String oneText = ((TextView)view).getText().toString();
switch (oneText){
case "+/-":
break;
case "%":
break;
case "1":
break;
}
}
}
在对应的布局文件里也需要为textview添加一个属性
<TextView
android:id="@+id/C"
android:onClick="oneOnClick"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:text="C"
/>