2014-01-20 11 views
0

我寫了下面的代碼2個不同的XML頁面:嘗試使用2個不同的按鍵

public class FragmentFirstPage extends Fragment implements OnClickListener{ 


View root; 


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

    root = inflater.inflate(R.layout.fragment_page1, container, false); 

    Button bt = (Button) root.findViewById(R.id.button1); 
    bt.setOnClickListener(this); 

    Button mbt = (Button) root.findViewById(R.id.imageButton1); 
    mbt.setOnClickListener(new View.OnClickListener() { 
    final ViewPager viewPager = (ViewPager) getActivity().findViewById (R.id.activity_main_viewpager); 

     @Override 
      public void onClick (View v){ 
      viewPager.setCurrentItem (viewPager.getCurrentItem() + 1); //or -1 to go previous 

      } 
    });    
    return root; 

} 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 
    Intent myIntent = new Intent(getActivity(), SplashActivity.class); 
    getActivity().startActivity(myIntent); 

} 

當我運行它,它不顯示任何錯誤,但也不管用。它顯示一條錯誤消息,說不幸的應用程序已停止工作。我應該怎麼做才能獲得這個權利我的XML頁面有2個按鈕button1打開一個活動和imagebutton1顯示下一個片段。這是我想要實現的。

回答

1

所有你需要做的是檢查你的onClick內通過檢查調用視圖的ID哪個按鈕被點擊。從那裏你可以決定每個人會做什麼。

public class beforemain extends Activity implements OnClickListener { 

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

    Button mBtn1 = (Button) findViewById(R.id.button1); 
    Button mBtn2 = (Button) findViewById(R.id.button2); //Just like #1 
    mBtn1.setOnClickListener(this); 
    mBtn2.setOnClickListener(this); //Also like #1 

} 

@Override 
public void onClick(View v) { 
    switch(v.getId()) { //Get the id of the button that was clicked 
    case R.id.button1: 
     Intent i = new Intent(beforemain.this, Splash1.class); 
     startActivity(i); 
     break; 
    case R.id.button2: 
     Intent i = new Intent(beforemain.this, Splash2.class); 
     startActivity(i); 
     break; 
    } 
} 
+0

三江源爵士它的工作完美 – Anish688

+0

沒問題,很高興爲您排憂解難。 – csmckelvey

+0

確定一切正常,但當我嘗試添加聲音池時,它永遠不會播放曲目。 – Anish688

0

你只需要設置onClickListener您的所有按鈕,然後比較它們的ID在if elseswitch情況下,做任何你想做的事情

對於如。

@Override 
public void onClick(View v) 
{ 
    if(v.getId()==R.id.button1) 
    { 
    Intent i=new Intent(beforemain.this, Splash1.class); 
    startActivity(i); 
    } 
    else if(v.getId()==R.id.button2) 
    { 
    Intent i=new Intent(beforemain.this, Splash2.class); 
    startActivity(i);} 
    } 
} 
1

變化實現的onClick方法如下..

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

    Button mBtn1 = (Button) findViewById(R.id.button1); 
    mBtn1.setOnClickListener(this); 
    Button mBtn2 = (Button) findViewById(R.id.button1); 
    mBtn2.setOnClickListener(this); 

} 

@Override 
public void onClick(View v) { 
    if(mBtn1.getId()==v.getID()) 
    { 
    Intent i1=new Intent(beforemain.this, Splash1.class); 
    startActivity(i1); 
    } 
    else 
    { 
     Intent i=new Intent(beforemain.this, Splash2.class); 
     startActivity(i); 
    } 
} 
1
Button mBtn1 = (Button) findViewById(R.id.button1); 
Button mBtn2 = (Button) findViewById(R.id.button2); 
mBtn1.setOnClickListener(this); 
mBtn2.setOnClickListener(this); 




@Override 
     public void onClick(View v) { 
      // TODO Auto-generated method stub 
      int id = v.getId(); 
      switch(id) { 
      case R.id.button1: 

     Intent i=new Intent(beforemain.this, Splash1.class); 
     startActivity(i); 

       break; 
      case R.id.button2: 

    Intent i=new Intent(beforemain.this, Splash2.class); 
     startActivity(i); 
       break; 

     } 

     } 
相關問題