android 自定义View -传值

文章目录

  • 前言
  • 实现

前言

在实现九宫格的过程中,如果想要用一个自定义控件完成设置密码和检查密码两种行为的话,就需要自定义属性。
大概需要如下:

  1. 这是设置密码的view,还是检查密码的view
  2. 设定密码的view需要获取最后的password
  3. 检查密码的view需要传入一个确定的password

学习资料:《Android自定义控件开发入门与实践》 - 第12章

我怎么在fragment中获取和设置自定义属性的值呢?Android主activity向自定义view传递参数

Android——Fragment向Activity传递数据

实现

最后没有用自定义属性- -。因为要在fragment和activity中设置和获取密码。自定义属性不知道怎么弄。然后参考这篇文章:我怎么在fragment中获取和设置自定义属性的值呢?Android主activity向自定义view传递参数。通过设定自定义View的方法来获取

       getSupportFragmentManager().beginTransaction()
                .replace(R.id.fragmentLayout, setFragment)
                .commit();

        button_test.setOnClickListener(v -> {
            this.password = setFragment.getPassword();
            Toast.makeText(this, password, Toast.LENGTH_SHORT).show();
            button_set.setBackgroundColor(this.getColor(R.color.white));
            button_test.setBackgroundColor(this.getColor(R.color.grey));
            this.testFragment = testPasswordFragment.newInstance(password);
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentLayout, testFragment)
                    .commit();
        });

        button_set.setOnClickListener(v -> {
            button_set.setBackgroundColor(this.getColor(R.color.grey));
            button_test.setBackgroundColor(this.getColor(R.color.white));
            getSupportFragmentManager().beginTransaction()
                    .replace(R.id.fragmentLayout, setFragment)
                    .commit();
        });
public class setPasswordFragment extends Fragment {
    private static final String TAG = "setPasswordFragment";
    LockView2 lockView;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_set_password, container, false);
        lockView = view.findViewById(R.id.lockView_set);
        lockView.setIsSetPassword(true);
        return view;
    }
    
    public String getPassword() {
        return lockView.getPassword();
    }
}
public class testPasswordFragment extends Fragment {
    private static final String TAG = "testPasswordFragment";
    String correctPassword;
    private static final String key = "testPasswordFragment";

    public static testPasswordFragment newInstance(String s) {
        testPasswordFragment fragment = new testPasswordFragment();
        Bundle bundle = new Bundle();
        bundle.putString(key, s);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        correctPassword = getArguments().getString(key);
        Log.d(TAG, "获取正确password:" + correctPassword);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_test_password, container, false);
        LockView2 lockView = view.findViewById(R.id.lockView_test);
        lockView.setIsSetPassword(false);
        lockView.setCorrectPassword(correctPassword);
        return view;
    }

}

你可能感兴趣的:(Fragment,Android)