У меня есть fragment
с именем fragment_login.xml
и соответствующий класс с именем LoginPageFragment.java
. В моем fragment
есть 2 EditTexts
и кнопка.
Я хочу написать onClick
метод для моей кнопки, но я хочу, чтобы он был в моем class
, чтобы я мог использовать его везде, где я использую fragment
(динамически). я написал тело метода в своем классе LoginPageFragment
, но программа не распознает его. Не уверены, в чем здесь проблема?
import android.app.Fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class LoginPageFragment extends Fragment {
private Button btnLogin ;
private EditText inputUsername , inputPassword ;
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.fragment_login, container , false);
inputUsername = (EditText) fragmentView.findViewById(R.id.input_username);
inputPassword = (EditText) fragmentView.findViewById(R.id.input_password);
btnLogin = (Button) fragmentView.findViewById(R.id.btn_login);
return fragmentView ;
}
public void onLoginClick(View v){
String usernameString = inputUsername.getText().toString() ;
String passwordString = inputPassword.getText().toString() ;
if(Utility.areEditTextsEmpty(inputUsername , inputPassword)){
Toast.makeText(LoginActivity.fa , "Some Fields are EMPTY",Toast.LENGTH_SHORT).show();
}
User tempUser = Utility.doesTheUserExist(usernameString , passwordString);
if(tempUser != null){
Utility.sendUserInfoToAnotherActivity(tempUser);
}
else{
Toast.makeText(LoginActivity.fa , "User Not Defined" , Toast.LENGTH_SHORT);
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".LoginActivity"
android:padding="40dp"
android:orientation="vertical"
>
<EditText
android:id="@+id/input_username"
android:inputType="number"
style="@style/LoginElements"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@mipmap/username_icon2" />
<EditText
android:id="@+id/input_password"
style="@style/LoginElements"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableRight="@android:drawable/ic_secure" />
<Button
android:id="@+id/btn_login"
style="@style/LoginElements"
android:text="@string/loginButtonText"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onLoginClick"
/>
</LinearLayout>