是否可以检查是否在 Java 的不同页面中单击了按钮?
Is it possible to check if a button is clicked in a different page in Java?
我想检查在 Java 中的前一个 activity 中是否单击了按钮。如果第一个按钮被点击,那么它会进入第二个页面跟随功能'a'。如果点击第二个按钮,那么它会进入第二页,跟随函数 'b' 等等。
基本上只有2个主要活动,第1页和第2页。如果在第一页中单击任何按钮,它将转到第二页,但每个按钮将在第二页中执行不同的代码行(或功能)。这可能吗?或者代码是什么?
你可以这样做:
第一页:
btn1.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",1)));
btn2.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",2)));
btn3.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",3)));
btn4.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",4)));
btn5.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",5)));
第二页:
现在你必须得到点击的内容。在创建时添加:
switch(getIntent().getIntExtra("click",1)){
case 1:
Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
}
从第一个 activity 开始,您传递使用 intent 单击了哪个按钮。现在在第二个activity,你得到它,然后写你想要的代码。
如果你想要我的全部 activity 代码,它在下面
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.b1),btn2 = findViewById(R.id.b2),btn3 = findViewById(R.id.b3),btn4 = findViewById(R.id.b4),btn5 = findViewById(R.id.b5);
btn1.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",1)));
btn2.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",2)));
btn3.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",3)));
btn4.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",4)));
btn5.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",5)));
}
}
现在是我的第二个 activity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
switch(getIntent().getIntExtra("click",1)){
case 1:
Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
}
}
}
我想检查在 Java 中的前一个 activity 中是否单击了按钮。如果第一个按钮被点击,那么它会进入第二个页面跟随功能'a'。如果点击第二个按钮,那么它会进入第二页,跟随函数 'b' 等等。
基本上只有2个主要活动,第1页和第2页。如果在第一页中单击任何按钮,它将转到第二页,但每个按钮将在第二页中执行不同的代码行(或功能)。这可能吗?或者代码是什么?
你可以这样做:
第一页:
btn1.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",1)));
btn2.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",2)));
btn3.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",3)));
btn4.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",4)));
btn5.setOnClickListener(v -> startActivity(new Intent(this,YoursecondactivityName.class).putExtra("click",5)));
第二页:
现在你必须得到点击的内容。在创建时添加:
switch(getIntent().getIntExtra("click",1)){
case 1:
Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
}
从第一个 activity 开始,您传递使用 intent 单击了哪个按钮。现在在第二个activity,你得到它,然后写你想要的代码。
如果你想要我的全部 activity 代码,它在下面
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = findViewById(R.id.b1),btn2 = findViewById(R.id.b2),btn3 = findViewById(R.id.b3),btn4 = findViewById(R.id.b4),btn5 = findViewById(R.id.b5);
btn1.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",1)));
btn2.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",2)));
btn3.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",3)));
btn4.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",4)));
btn5.setOnClickListener(v -> startActivity(new Intent(this,SecondActivity.class).putExtra("click",5)));
}
}
现在是我的第二个 activity
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
public class SecondActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
switch(getIntent().getIntExtra("click",1)){
case 1:
Toast.makeText(this,"button 1 click",Toast.LENGTH_SHORT).show();
break;
case 2:
Toast.makeText(this,"button 2 click",Toast.LENGTH_SHORT).show();
break;
case 3:
Toast.makeText(this,"button 3 click",Toast.LENGTH_SHORT).show();
break;
case 4:
Toast.makeText(this,"button 4 click",Toast.LENGTH_SHORT).show();
break;
case 5:
Toast.makeText(this,"button 5 click",Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(this, "something went wrong", Toast.LENGTH_SHORT).show();
}
}
}