我有两项活动(MainActivity
和SecondActivity
)。我的MainActivity
有两个按钮,我的SecondActivity
有一个TextView
。这两个按钮将导致第二个活动。我想要做的是,当我点击第一个按钮时,TextView
可见性将消失。这可能吗?
public void hi(View v) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
public void hello(View v) {
Intent intent = new Intent(this, SecondActivity.class);
startActivity(intent);
}
答案 0 :(得分:3)
是的,这是可能的。
实现此效果的一种简单方法是向extra
添加Intent
。
public void hi(View v) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("textViewVisible", true);
startActivity(intent);
}
public void hello(View v) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("textViewVisible", false);
startActivity(intent);
}
在SecondActivities onCreate()
方法中,您获得布尔值并根据它设置可见值。
public void onCreate() {
...
if(getIntent().getBooleanExtra("textViewVisible", true)) {
textView.setVisibility(View.VISIBLE);
} else {
textView.setVisibility(View.INVISIBLE);
}
}
答案 1 :(得分:0)
您可以发送布尔值
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("key",true);
startActivity(intent);
点击第二个按钮,你可以使用intent.putExtra(“key”,false); 然后在SecondActivity
boolean value = getIntent().getBooleanExtra("key",false);
根据值设置SecondActivity
中textview的可见性。
if(value==true)
{
textView.setVisibility(View.INVISIBLE);
}
http://developer.android.com/reference/android/content/Intent.html
public boolean getBooleanExtra (String name, boolean defaultValue)
Added in API level 1
Retrieve extended data from the intent.
Parameters
name The name of the desired item.
defaultValue the value to be returned if no value of the desired type is stored with the given name.
Returns
the value of an item that previously added with putExtra() or the default value if none was found.
答案 2 :(得分:0)
是的,它可能。您可以通过将boolean
值传递给SecondActivity
来执行此操作...如果确实如此,则隐藏TextView
。
public void hi(View v) {
Intent intent = new Intent(this, SecondActivity.class);
intent.putExtra("isDismiss", true);
startActivity(intent);
}
在SecondActivity
...
boolean isDismiss = getIntent().getBooleanExtra("isDismiss", false);
if (isDismiss) {
textView.setVisibility(View.GONE);
}