英文:
Button not visible in main activity
问题
我在主活动中创建了三个按钮。我使用了以下代码来处理按钮一:
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(GONE);
btn1.setVisibility(GONE);
btn2.setVisibility(GONE);
FragmentManager fm = getSupportFragmentManager();
TestFragment fragment = new TestFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).addToBackStack(null).commit();
}
});
问题是,当我按返回按钮时,按钮在主活动中不可见。
英文:
I have create three button in main activity. i used following code for button one
`btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(GONE);
btn1.setVisibility(GONE);
btn2.setVisibility(GONE);
FragmentManager fm = getSupportFragmentManager();
TestFragment fragment = new TestFragment();
fm.beginTransaction().add(R.id.fragment_container,fragment).addToBackStack(null.commit();`
the problem is that when i pressed back button than buttons not visible in main activity.
答案1
得分: 1
你已将它们的可见性设置为GONE,所以如果你想再次让它们出现,你应将它们的可见性设置为VISIBLE。
英文:
You have set their visibility to GONE, so if you want to have them again you should set their visibility to VISIBLE
答案2
得分: 1
你必须使用事件总线。它用于从片段传递数据到活动。
关于你的片段返回按键方法,你需要在你的第一个活动中传递布尔值,然后在你的活动中获取布尔值。在那个时候,你需要将你的按钮设为可见。
这是事件总线的链接:EventBus
编码愉快!
英文:
you have to use event bus. it's used for pass data from fragment to activity.
your fragment back press method, you have to pass boolean value on your first activity and get the boolean value in your activity. that time you have to visible your button.
this is link for event bus:= EventBus
Happy Coding
答案3
得分: 1
如果您按下返回按钮,您会返回到之前的活动,但是您已经将按钮的可见性设置为Gone,这就是为什么您看不到这些按钮。您需要处理返回按钮并将按钮的可见性再次设置为Visible。
public class MainActivity extends AppCompatActivity {
Button btn, btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(GONE);
btn1.setVisibility(GONE);
btn2.setVisibility(GONE);
FragmentManager fm = getSupportFragmentManager();
TestFragment fragment = new TestFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).addToBackStack(null).commit();
}
});
}
// 使用onKeyDown方法处理返回按钮。
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
// 检查是否按下了返回按钮。如果按下了执行以下操作:
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed(); // 返回到之前的活动。
btn.setVisibility(View.VISIBLE); // 将按钮的可见性设置为Visible。
btn1.setVisibility(View.VISIBLE); // 将btn1的可见性设置为Visible。
btn2.setVisibility(View.VISIBLE); // 将btn2的可见性设置为Visible。
return true;
}
return super.onKeyDown(keyCode, event);
}
}
英文:
If you press back button you return your activity back but you've already set your buttons' visibilities as Gone that's why you can't see those buttons. You have to handle back button and set buttons' visibilities as Visible again.
public class MainActivity extends AppCompatActivity {
Button btn, btn1, btn2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = findViewById(R.id.btn);
btn1 = findViewById(R.id.btn1);
btn2 = findViewById(R.id.btn2);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
btn.setVisibility(GONE);
btn1.setVisibility(GONE);
btn2.setVisibility(GONE);
FragmentManager fm = getSupportFragmentManager();
TestFragment fragment = new TestFragment();
fm.beginTransaction().add(R.id.fragment_container, fragment).addToBackStack(null).commit();
}
});
}
//Handle your Back Button with onKeyDown method.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//Check for if Back Button is pressed. If pressed do below:
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed(); //Return your activity back.
btn.setVisibility(View.VISIBLE); //Set your btn's visibility as Visible.
btn1.setVisibility(View.VISIBLE); //Set your btn1's visibility as Visible.
btn2.setVisibility(View.VISIBLE); //Set your btn2's visibility as Visible.
return true;
}
return super.onKeyDown(keyCode, event);
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论