我正在创建一个片段,其中我使用可点击的linearLayout作为按钮 我已经创建了所有必要的方法并实现了类的OnClickListener接口,但是当我单击布局时没有任何反应 这是来自xml和java代码的LinearLayout和TextView。
<LinearLayout
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_width="50dip"
android:layout_weight="1"
android:clickable="true"
android:background="@drawable/button_layout"
android:layout_margin="1dip">
<LinearLayout
android:orientation="vertical"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e"
android:textColor="@color/abc_primary_text_disable_only_material_dark"
android:gravity="top"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="π"
android:gravity="bottom"/>
</LinearLayout>
</LinearLayout>
主视图TextView是
<TextView android:id="@+id/mainview"
android:layout_width="match_parent"
android:background="@color/button_material_light"
android:layout_height="60dip"
android:layout_weight="1"/>
班级代码是
public class HomeFragment extends Fragment implements View.OnClickListener{
TextView mainView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainView = (TextView) getActivity().findViewById(R.id.mainview);
}
public static HomeFragment newInstance(int i){
HomeFragment homeFragment = new HomeFragment();
Bundle args = new Bundle();
args.putInt("index", i);
homeFragment.setArguments(args);
return homeFragment;
}
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(getArguments().getInt("index"));
}
@Override
public void onClick(View view) {
mainView.setText("ghalib");
}
}
我也试过调试程序,发现单击Layout时没有调用该方法。
答案 0 :(得分:0)
感谢rahul提醒我通过&#34;这个&#34;我可以用作Button的Clickable LinearLayout的setOnClickListener中的对象
所以首先我通过android将id分配给布局:id =&#34; @ + id / pie&#34; 然后在代码中设置列表器,然后新代码
public class HomeFragment extends Fragment implements View.OnClickListener{
TextView mainView;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mainView = (TextView) getActivity().findViewById(R.id.mainview);
//This is the New Part
((LinearLayout)getActivity().findViewById(R.id.pie))
.setOnClickListener(this);
}
public static HomeFragment newInstance(int i){
HomeFragment homeFragment = new HomeFragment();
Bundle args = new Bundle();
args.putInt("index", i);
homeFragment.setArguments(args);
return homeFragment;
}
public HomeFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home, container, false);
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity)activity).
onSectionAttached(getArguments().getInt("index"));
}
@Override
public void onClick(View view) {
mainView.setText("ghalib");
}
}