0

我正在創建一個片段,我在其中使用可點擊的linearLayout作爲按鈕 我創建了所有必需的方法並實現了OnClickListener接口,但是當我單擊佈局什麼也沒有發生 這是我的LinearLayout和XML和Java代碼的TextView。OnClickListner接口不適用於片段中的linearlayout

  <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> 

的MAINVIEW 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"); 
} 
} 

我也試過調試程序,並找出該方法不叫上單擊佈局。

+0

你確定,你已經添加了mainView.setOnClickListener(本); – rahul

+0

謝謝.... rahul我發現解決方案,我忘了設置我的LinearLayout列表器,並通過mainView是我的TextView這是我的佈局中不可點擊的方式 –

+0

嘿@rahul是否有可能使用「this」Object for onclicklistner in xml –

回答

0

由於拉胡爾提醒我要通過「本」對象的可點擊的LinearLayout的setOnClickListener這我使用的按鈕

首先,我分配到的ID由Android的佈局:ID = 「@ + ID /餡餅」 然後在代碼中設置讀心人那麼新的代碼是

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"); 
} 
}