我有一个 FragmentActivity
,我添加一个片段
。
根据一些情况,我想访问按钮
从他们那片段的布局和变化的点击侦听器。
I have a FragmentActivity
where I add a Fragment
.
Depending on some situation, I want to access Buttons
from that fragment's layout and change click listeners of them.
我做的是:
View view = myFragment.getView();
Button myButton = (Button) view.findViewById(R.id.my_button);
myButton.setOnClickListener(new MyClickListener());
但视图
变量总是空
。
我怎样才能获得活动的看法?
How can I get the view of the activity?
如果您要访问你的组件,并设置一些数据,我建议你做这样的片段里面的方法:
If you want to access your component and set some data, I suggest you to make a method inside the fragment like this:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class DetailFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.details, container, false);
return view;
}
public void setSettings(){
Button button = (Button) getView().findViewById(R.id.my_button);
button.setOnClickListener(new MyClickListener());
}
}
上一篇:Resize 和 SizeChanged 事件之间的区别区别、事件、Resize、SizeChanged
下一篇:如何在 UserControl 中使用 ContentPresenter如何在、UserControl、ContentPresenter