gpt4 book ai didi

java - Android:处理 Fragment 内的用户输入

转载 作者:行者123 更新时间:2023-12-01 18:37:23 25 4
gpt4 key购买 nike

我的测试项目的主要 Activity 如下:

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.display_message, menu);
return true;
}

public void sendMessage(View view){
FragmentManager frManager = getFragmentManager();
FragmentTransaction transaction = frManager.beginTransaction();
Frag fr= new Frag();
transaction.add(R.id.stuuf, fr);
transaction.addToBackStack(null);
transaction.commit();
}
}

以及以下主要布局:

<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/stuuf"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:paddingTop="?android:attr/actionBarSize" >

<Button android:id="@+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"/>

</TableLayout>

当用户单击该按钮时,将显示以下包含第二个按钮的 fragment :

<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".Frag" >

<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="180dp"
android:onClick="buttonClicked"
android:text="Button" />
</RelativeLayout>

以及应该控制它的类:

public class Frag extends Fragment {

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
return inflater.inflate(R.layout.frag_test, null);
}

public void buttonClicked(){
System.out.println("Hello!");
}

}

现在,有一个问题,当我单击第二个按钮时,系统会在MainActivity中查找buttonClicked()方法,而不是在Frag中查找,从而出现以下错误:

java.lang.IllegalStateExcetion: Could not find method buttonClicked(View) in the activity class com.spacitron.mytestproject.MainActivity for onClick handler on view class android.widget.Button with id 'button1'

显然,我应该能够从 fragment 类本身处理 fragment 中发生的事情,所以我一定做错了一些事情,我只是无法确定是什么。

最佳答案

问题是您在 fragment 布局中使用 onClick 方法,并且 Android 希望在 Activity 中找到该方法。有两种方法可以解决此问题:

  1. 最健康的方法:从 XML 中删除 onClick 属性并依赖 OnClickListener 实现。
  2. 将 Activity 的调用委托(delegate)给 fragment - 在 Activity 中声明方法并将其委托(delegate)给 fragment 。如下所示:

在 Activity 类中有这个:

public void buttonClicked(View view){
Fragment current = getFragmentManager().findFragmentById(R.id.stuuf);
if(current != null) {
((Frag)current).buttonClicked();
}
}

从长远来看,我强烈建议不要使用 onClick XML 属性。原因在this SO answer中有解释。 .

关于java - Android:处理 Fragment 内的用户输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21351659/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com