-
部分细节是自己改写了。
-
记住:dataBinding生成的Binding类时,会引用到自定义的model或者view。切记包名不可以大写,否则会报错:错误: 程序包Model不存在!!!!!!来源
-
if (getSupportActionBar() != null){
getSupportActionBar().hide();
}
用来隐藏一个Activity的ActionBar合适。 -
数字块可以画辅助线(%)
-
字符串资源要放起来,好翻译和管理。
-
不带参数的nextInt()会生成所有有效的整数(包含正数,负数,0)
带参的nextInt(int x)则会生成一个范围在0~x(不包含X)内的任意正整数
例如:int x=new Random.nextInt(100);
则x为一个0~99的任意整数
BasicAC.java
package com.test.kousuanDemo;
import android.content.DialogInterface;
import android.os.Bundle;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import com.test.R;
public class BasicAC extends AppCompatActivity {
NavController controller;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_basic_ac);
// if (getSupportActionBar() != null){
// getSupportActionBar().hide();
// }
controller= Navigation.findNavController(this,R.id.fragment5);
NavigationUI.setupActionBarWithNavController(this,controller);
}
@Override
public boolean onSupportNavigateUp() {
if (controller.getCurrentDestination().getId()==R.id.questionFragment){
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setTitle("注意!");
builder.setPositiveButton("退出", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
controller.navigateUp();
}
});
builder.setNegativeButton("算了", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alertDialog=builder.create();
alertDialog.show();
}else {
controller.navigate(R.id.titleFragment);
}
return super.onSupportNavigateUp();
//@return true if Up navigation completed successfully and this Activity was finished,
// * false otherwise.
}
@Override
public void onBackPressed() {
// if(外面还有页面){可以退}
onSupportNavigateUp();
}
}
Title.java
package com.test.kousuanDemo;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import com.test.R;
import com.test.databinding.FragmentTitleBinding;
public class TitleFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
FragmentTitleBinding binding;
VM vm= ViewModelProviders.of(getActivity(),new SavedStateViewModelFactory(getActivity().getApplication()