片段验证Android Studio

时间:2016-11-27 08:32:29

标签: android android-fragments

我正在尝试在某些edittext中创建验证,我正在使用我在互联网上找到的示例,但是当我实现代码时,我的findViewById出错(在视图中无法应用于android.view.view。< / p>

public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

    final View view = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);

    View.OnClickListener listener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Fragment fragment = null;
            FragmentManager fragmentManager = getFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

            switch (v.getId()) {
                case R.id.btn_cadastrar_conta:
                    registerAttemptWithRetrofit(1,
                            "3948",
                            "0",
                            "01089842",
                            "3",
                            1);

                    fragment = new ContasBancarias();

                default:
                    break;
            }

            fragmentManager.beginTransaction()
                    .replace(R.id.container, fragment)
                    .commit();

            View view1 = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);

            EditText agencia = (EditText) getView().findViewById(R.id.txt_agencia);

            if ("".equals(agencia.getText().toString().trim())) {
                Toast.makeText(getActivity(), "Campo Obrigatório", Toast.LENGTH_SHORT).show();
                return;

                //

           }
     }
 };
    return view; }
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent" android:layout_height="match_parent">


  <TextView
        android:text="Dados Bancários"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/textView3"
        android:layout_marginTop="16dp"
        android:textSize="18sp"
        android:textColor="@android:color/black"
        android:textColorHighlight="@color/Black"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />

    <EditText
        android:layout_width="130dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/txt_agencia"
        android:hint="Agência"
        android:layout_alignBaseline="@+id/txt_digito_agencia"
        android:layout_alignBottom="@+id/txt_digito_agencia"
        android:layout_toEndOf="@+id/spn_tipo_de_conta"/>

    <EditText
        android:layout_width="95dp"
        android:layout_height="wrap_content"
        android:inputType="number"
        android:ems="10"
        android:id="@+id/txt_digito_conta"
        android:layout_alignBottom="@+id/txt_conta"
        android:layout_toEndOf="@+id/txt_conta"
        android:layout_alignTop="@+id/txt_conta"
        android:hint="Digito"
        android:textStyle="bold" />

    <Spinner
        android:layout_width="150dp"
        android:layout_height="40dp"
        android:id="@+id/spn_tipo_de_conta"
        android:layout_below="@+id/textView3"
        android:layout_alignParentStart="true"
        android:layout_marginTop="23dp" />

    <Button
        android:text="Cancelar"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:id="@+id/btn_cancelar"
        android:layout_alignParentBottom="true"
        android:layout_alignParentStart="true"
        android:layout_marginBottom="21dp"
        android:background="@color/DefaultBlue"
        android:textColor="@android:color/white"
        android:textStyle="normal|bold"
        android:onClick="cancelar (CadastroContasBancarias)" />

    <Button
        android:text="Cadastrar"
        android:layout_width="90dp"
        android:layout_height="40dp"
        android:id="@+id/btn_cadastrar_conta"
        android:background="@color/DefaultBlue"
        android:textColor="@android:color/white"
        android:textStyle="normal|bold"
        android:layout_alignBaseline="@+id/btn_cancelar"
        android:layout_alignBottom="@+id/btn_cancelar"
        android:layout`enter code here`_alignParentEnd="true" />


</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

你可以试试这个解决方案......

View view1 = inflater.inflate(R.layout.fragment_cadastro_contas_bancarias, container, false);

  EditText agencia = (EditText) view1 ().findViewById(R.id.txt_agencia);