-3
我試圖在一些edittext中創建驗證,我使用了一個我在互聯網上找到的例子,但是當我實現代碼時,findViewById出錯了(在視圖中無法應用到android.view 。.view片段驗證Android Studio
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>
顯示包含R.id.txt_agencia的xml –
因爲我有一個包含當前銀行信息的片段。 當你點擊一個按鈕時,它會用edittext(必須有驗證)打開另一個片段,並在保存時返回到前一個片段(或第一個片段列表)。我發現這個例子試圖實現 –