-2

enter image description here在片段

我看StackOverflow上的其他问题与片段设置OnClickListener设置OnClickListener,我尝试了不同的解决方案,并没有奏效。所有说我应该设置getActivity(),一个片段活动,但它不起作用。那么如何在片段中设置OnClickListener?

+1

你的问题是什么? – Ibrahim

+1

'我如何在一个片段中设置一个OnClickListener?' - 通过不像你所做的那样引用父Activity。 '所有说我应该设置getActivity()' - 这是不正确的。使用'rootView.findByViewId()',假定您所引用的视图位于膨胀视图中。 –

+0

它不是root view.findviewbyid。当我点击NEXTPG按钮时,应用程序崩溃。 – iBEK

回答

0
/*declare variable for the button... 
Declare it like this before the onCreateView method; 
*/ 

private Button commandButton; 

//Inside the onCreateView method use the below code... 

commandButton = (Button) rootView.findViewById(R.id.NEXTPG); 

commandButton.setOnCickListener(new OnClickListener(){ 
@override 
public void onClick(View view){ 
        //your commands here 
     } 
} 
0

你也可以在OnCreateView方法中返回布局,所以使用它我的代码在下面给出。

final RelativeLayout mRelativeLayout =(RelativeLayout)inflater.inflate(R.layout.activity_live_tabber,container,false);

Button mButton = (Button) mRelativeLayout.findViewById(R.id.NEXTPG); 
    mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      // here you set what you want to do when user clicks your button, 
     } 
    }); 

    return mRelativeLayout;