在哪里添加片段的方法?
Where to add methods of a fragment?
我有一个名为 fragment_login.xml
的 fragment
和一个名为 LoginPageFragment.java
的相应 class。在我的 fragment
中有 2 个 EditTexts
和一个按钮。
我想为我的按钮编写一个 onClick
方法,但我想在我的 class
中使用它,这样我就可以在我使用 fragment
的任何地方使用它(动态)。我在 LoginPageFragment
class 中写了方法的主体,但程序无法识别它。不确定这里的问题是什么?
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>
xml 中的 onClick 标记需要由 activity 处理。在一个片段中,最简单的事情就是以编程方式添加点击监听器。另一种方法是让 activity 处理它,然后在片段上调用函数,但这很麻烦,如果您更改片段,可能会出现问题。
enter code here
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);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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);
}
}
});
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);
}
}
}
我有一个名为 fragment_login.xml
的 fragment
和一个名为 LoginPageFragment.java
的相应 class。在我的 fragment
中有 2 个 EditTexts
和一个按钮。
我想为我的按钮编写一个 onClick
方法,但我想在我的 class
中使用它,这样我就可以在我使用 fragment
的任何地方使用它(动态)。我在 LoginPageFragment
class 中写了方法的主体,但程序无法识别它。不确定这里的问题是什么?
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>
xml 中的 onClick 标记需要由 activity 处理。在一个片段中,最简单的事情就是以编程方式添加点击监听器。另一种方法是让 activity 处理它,然后在片段上调用函数,但这很麻烦,如果您更改片段,可能会出现问题。
enter code here
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);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(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);
}
}
});
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);
}
}
}