繁体   English   中英

在Android SDK / Java中,如何从片段中的方法处理XML视图(图像按钮,文本视图)?

[英]In Android SDK/Java, how do I manipulate XML Views(imagebuttons, textviews) from a method in a fragment?

解决了! 感谢大家的帮助。

我正在努力将Fragments作为一个概念,尤其似乎停留在这一件事上。 我想做的是使用片段操作其自己的布局数据。 每次我尝试从Fragment中访问ImageButton时,它都会使应用程序崩溃。 从活动中可以正常工作。 我是否只是从根本上误解了碎片?

代码(按尺寸缩小)-

这是从中调用我的片段的活动的开始:

Display.java

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

        FragmentManager fragmentManager = getFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();

        MyFragmentClass MyFragment = new MyFragmentClass();

        fragmentTransaction.add(R.id.fragment_container, MyFragment);
        fragmentTransaction.commit();

该活动的XML:

activity_display.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                tools:context="com.mycompanyname.myprojectname.Display"
                android:id="@+id/display_layout">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/fragment_container">
    </FrameLayout>

</RelativeLayout>

碎片

MyFragmentClass.java
public class MyFragmentClass extends Fragment
{
 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        return inflater.inflate(R.layout.fragment_layout_screen, container, false);;

    }

    public void MethodTest()
    {

    }

}

片段的XML文件:

fragment_layout_screen.xml

<?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"
    android:id="@+id/fragment_layout_screen"
    tools:context="com.mycompanyname.myprojectname.MyFragmentClass">

    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/my_button"
        android:clickable="true"
        android:onClick="buttonPress"
        android:src="@drawable/button"
        android:layout_alignParentRight="true"
        android:layout_alignParentEnd="true"/>

</RelativeLayout>

基本上,我本来就在Display.java中拥有所有这些功能,但是想添加片段,所以我试图将内容移入和移入片段,但是我仍然需要能够操作xml信息的能力,但我不能。

从Display.java活动内部,我可以轻松地这样调用ImageButton:

ImageButton myButton = (ImageButton) findViewById(R.id.my_button)

但是如果我在片段中的MethodTest中执行相同的操作,则应用程序将崩溃。

我在这里搜索了许多建议,并从此处尝试了各种解决方案: Fragment中的findViewById,但这些似乎都不起作用。

我一直在仔细阅读以下设置信息: http : //developer.android.com/guide/components/fragments.html ,似乎找不到我做错了什么。

任何帮助将不胜感激,如果您对我的设置有任何疑问,请询问。

在片段上声明private View rootView; onCreateView(...)设置rootView = inflater.Inflate(..); 然后以imageButton ImageButton mButton =(ImageButton) rootView.findViewById(..);访问imageButton ImageButton mButton =(ImageButton) rootView.findViewById(..);

Do like this in fragment

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

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


        GridView gridview = (GridView) view.findViewById(R.id.grid);

return view ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM