单击另一个文件中的提交按钮后如何禁用文件中的卡片视图
How to disable card-view in a file after clicking submit button in another file
当提交按钮处于其他布局时如何访问 CardView
?
所以基本上在我点击 File1.java 中的提交按钮后,我想禁用 Menu.java 中的卡片视图并更改颜色。
File1.java
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextPage();
}
});
private void nextPage(){
Intent intent = new Intent(this, Menu.class);
startActivity(intent);
}
Menu.java
package com.example.test;
//all import statements
public class Menu extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = Menu.class.getSimpleName();
private final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
showStartDialog();
CardView cardView1 = findViewById(R.id.card1);
cardView1.setOnClickListener(this);
}
//try to make cardview unclickable
@Override
public void onClick(View v) {
Intent intent = new Intent(Menu.this, File.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode ==REQUEST_CODE){
if(data != null){
boolean cardView1= data.getBooleanExtra("disable", false);
Log.d(TAG,"code success 1");
if (cardView1) {
Log.d(TAG, "code success 2");
}
}
}
}
}
//popup alert message , create dialog builder
private void showStartDialog(){
String alert1 = "\nAnswer at least 5 surveys now!\n";
String alert2 = "Redeem at information counter";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(Html.fromHtml("<font color='#FF0000'>Collect mystery gift!</font>"));
alert.setIcon(R.drawable.gift);
alert.setMessage(alert1 + "\n" + alert2);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = alert.create();
dialog.show();
Log.d(TAG, "popup appear");
}
}
您可以覆盖 Menu.java
文件中的 onActivityResult
方法,而不是再次启动 Menu.java
activity,完成当前 activity 并使用 setResult()
.
Menu.java
private final int REQUEST_CODE = 101
...
...
@Override
public void onClick(View v) {
Intent intent = new Intent(Menu.this, File1.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == REQUEST_CODE){
if(data != null){
boolean disableCard = data.getBooleanExtra("disableCard", false);
if(disableCard){
cardView1.setEnabled(false);
cardView1.setOnClickListener(null);
} else {
cardView1.setEnabled(true);
cardView1.setOnClickListener(this);
}
}
}
}
}
File1.java
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//set extras here that you want to pass back to Menu.java activity
intent.putExtra("disableCard", true);
setResult(RESULT_OK, intent);
finish();
}
});
当提交按钮处于其他布局时如何访问 CardView
?
所以基本上在我点击 File1.java 中的提交按钮后,我想禁用 Menu.java 中的卡片视图并更改颜色。
File1.java
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
nextPage();
}
});
private void nextPage(){
Intent intent = new Intent(this, Menu.class);
startActivity(intent);
}
Menu.java
package com.example.test;
//all import statements
public class Menu extends AppCompatActivity implements View.OnClickListener {
private static final String TAG = Menu.class.getSimpleName();
private final int REQUEST_CODE = 101;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
showStartDialog();
CardView cardView1 = findViewById(R.id.card1);
cardView1.setOnClickListener(this);
}
//try to make cardview unclickable
@Override
public void onClick(View v) {
Intent intent = new Intent(Menu.this, File.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK){
if (requestCode ==REQUEST_CODE){
if(data != null){
boolean cardView1= data.getBooleanExtra("disable", false);
Log.d(TAG,"code success 1");
if (cardView1) {
Log.d(TAG, "code success 2");
}
}
}
}
}
//popup alert message , create dialog builder
private void showStartDialog(){
String alert1 = "\nAnswer at least 5 surveys now!\n";
String alert2 = "Redeem at information counter";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle(Html.fromHtml("<font color='#FF0000'>Collect mystery gift!</font>"));
alert.setIcon(R.drawable.gift);
alert.setMessage(alert1 + "\n" + alert2);
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
AlertDialog dialog = alert.create();
dialog.show();
Log.d(TAG, "popup appear");
}
}
您可以覆盖 Menu.java
文件中的 onActivityResult
方法,而不是再次启动 Menu.java
activity,完成当前 activity 并使用 setResult()
.
Menu.java
private final int REQUEST_CODE = 101
...
...
@Override
public void onClick(View v) {
Intent intent = new Intent(Menu.this, File1.class);
startActivityForResult(intent, REQUEST_CODE);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if(requestCode == REQUEST_CODE){
if(data != null){
boolean disableCard = data.getBooleanExtra("disableCard", false);
if(disableCard){
cardView1.setEnabled(false);
cardView1.setOnClickListener(null);
} else {
cardView1.setEnabled(true);
cardView1.setOnClickListener(this);
}
}
}
}
}
File1.java
buttonSubmit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent();
//set extras here that you want to pass back to Menu.java activity
intent.putExtra("disableCard", true);
setResult(RESULT_OK, intent);
finish();
}
});