【问题标题】:How to set invisible layout to visible permanently?如何将不可见的布局设置为永久可见?
【发布时间】:2021-12-27 06:06:55
【问题描述】:

在我的应用程序中,在 MainActivity.java 我需要显示两个布局,一个在初始阶段可见,另一个不可见。当按下按钮时,不可见的按钮可见,可见的按钮不可见,这是我编写的代码:

public class MainActivity extends AppCompatActivity {

DrawerLayout drawerLayout;
SupportMapFragment supportMapFragment;
SupportMapFragment supportMapFragment1;
FusedLocationProviderClient client;
View checkoutLayout, checkinLayout;
ImageButton checking_button, checkout_button;
Chronometer chronometer;
Handler handler;
long tMillisec, tStart, tBuff, tUpdate = 0L;
int sec, min, millisec, hr;
TextView tv_location, editText_remark, location_checkout, location_remark, dateView, timeView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);

    String date = new SimpleDateFormat("dd-MM-yyyy", Locale.getDefault()).format(new Date());
    String currentTime = new SimpleDateFormat("HH:mm", Locale.getDefault()).format(new Date());


    checking_button = findViewById(R.id.checkin_btn);
    chronometer = findViewById(R.id.chrono);
    checkoutLayout = findViewById(R.id.checkOut);
    checkinLayout = findViewById(R.id.checkinLayout);
    checkout_button = findViewById(R.id.checkout_btn);
    tv_location = findViewById(R.id.tv_location);
    editText_remark = findViewById(R.id.editText_remark);
    location_checkout = findViewById(R.id.location_checkout);
    location_remark = findViewById(R.id.Remark_checkout);
    dateView = findViewById(R.id.date_pick);
    timeView = findViewById(R.id.time_pick);


    checkinLayout.setVisibility(View.VISIBLE);
    checkoutLayout.setVisibility(View.INVISIBLE);

    handler = new Handler();

    drawerLayout = findViewById(R.id.drawer_layout);
    supportMapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map_view);
    supportMapFragment1 = (SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map_view1);
    client = LocationServices.getFusedLocationProviderClient(this);

    // check permission

    if(ActivityCompat.checkSelfPermission(MainActivity.this,
            Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED){
        // when permission granted call method
        getCurrentLocation();
    }else{
        ActivityCompat.requestPermissions(MainActivity.this,
                new String[]{Manifest.permission.ACCESS_FINE_LOCATION}, 44);
    }

    checking_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if(tv_location.getText().toString().isEmpty() || editText_remark.getText().toString().isEmpty()){
                Toast.makeText(getApplicationContext(), "Please Enter your Location and Remark", Toast.LENGTH_SHORT).show();
            }else {
                checkoutLayout.setVisibility(v.VISIBLE);
                checkinLayout.setVisibility(v.GONE);
                tStart = SystemClock.uptimeMillis();
                handler.postDelayed(runnable, 0);
                chronometer.start();
                location_checkout.setText(tv_location.getText().toString());
                location_remark.setText(editText_remark.getText().toString());
                dateView.setText("Date:"+ date);
                timeView.setText("Punch-In:"+ currentTime);
            }
        }
    });

    checkout_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkinLayout.setVisibility(v.VISIBLE);
            checkoutLayout.setVisibility(v.GONE);
        }
    });


}

代码运行良好,但问题是当我使 checkoutLayot 可见和 checkinLayout 不可见时,当我关闭应用程序并重新打开它时,布局更改为 checkoutLayout 不可见和 checkinLayout 可见,所以我需要使最后访问的页面可见。我是android开发新手,谁能帮我解决这个问题。

【问题讨论】:

    标签: java android xml android-studio


    【解决方案1】:

    您必须为此使用SharePreference,以保存用户关闭应用时您的应用所处的状态。用户按下按钮后立即将状态保存在 sharedpreference 中,然后在应用程序的onStart 方法中,您可以从SharePreference 中获取状态的值,然后相应地显示您的布局。

    【讨论】:

      【解决方案2】:

      首先,您应该将所有变量包括视图(如 Textview、Handler 等私有)。其次,我们的朋友@rootass 是对的,您应该使用SharePrference,这样您就可以保存更改。这是编写和获取SharedPreferences 的示例。编写 SharedPreferences 的代码 ...

      private SharedPreferences visibilityPreference;
      private SharedPreferences.Editor prefEdit;
      private final String visibilityKey = "visibility";
      
      
      private void setVisibility(boolean flag){
        prefEdit = visibilityPreference.edit();
        visibilityPreference = context.getSharedPreferences(visibilityPreferenceName, Context.MODE_PRIVATE);
        visibilityPreference.putBoolean(visibilityKey, flag);
        prefEdit.apply();
      }
      

      这里是用于读取 SharedPreferences 的代码...

      private boolean getVisibility(){
        visibilityPreference = context.getSharedPreferences(visibilityPreferenceName, Context.MODE_PRIVATE);
        return visibilityPreference .getBoolean(visibilityKey, false); //we set false as default value
      }
      

      ps:由于您是 android 新手,我可能应该提到,如果您处于独立活动中,而不是上面提到的代码中的 context,您不需要使用上下文,只需输入 this 或什么都没有,而您不是在独立活动中,如果是fragment,则使用contextrequireActivity(),如果是recyclerview,则使用传递context

      希望对你有所帮助;)

      【讨论】:

        猜你喜欢
        • 2016-07-19
        • 2017-10-01
        • 2015-12-17
        • 1970-01-01
        • 2016-12-27
        • 2016-02-04
        • 1970-01-01
        • 2015-04-08
        • 1970-01-01
        相关资源
        最近更新 更多