2014-10-07 63 views
-3

如何使用按钮切换活动?这是我的问题:如何使用按钮切换活动?

package net.example.finals; 

import android.support.v7.app.ActionBarActivity; 
import android.support.v7.app.ActionBar; 
import android.support.v4.app.Fragment; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.LayoutInflater; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.ViewGroup; 
import android.widget.AdapterView; 
import android.widget.Button; 
import android.os.Build; 



public class MainActivity extends ActionBarActivity { 

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


    if (savedInstanceState == null) { 
     getSupportFragmentManager().beginTransaction() 
       .add(R.id.container, new PlaceholderFragment()) 
       .commit(); 
    } 
} 


@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.main, menu); 
    return true; 
} 

@Override 
public boolean onOptionsItemSelected(MenuItem item) { 
    // Handle action bar item clicks here. The action bar will 
    // automatically handle clicks on the Home/Up button, so long 
    // as you specify a parent activity in AndroidManifest.xml. 
    int id = item.getItemId(); 
    if (id == R.id.action_settings) { 
     return true; 
    } 
    return super.onOptionsItemSelected(item); 
} 

/** 
* A placeholder fragment containing a simple view. 
*/ 
public static class PlaceholderFragment extends Fragment implements OnClickListener { 

    Button bq; 
    Button bw; 
    Button be; 
    Button br; 
    Button bt; 
    Button by; 
    Button bu; 


    public PlaceholderFragment() { 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_second, 
       container, false); 

     bq=(Button)rootView.findViewById(R.id.btn1); 
     bq.setOnClickListener(this); 
     bw=(Button)rootView.findViewById(R.id.btn2); 
     bw.setOnClickListener(this); 
     be=(Button)rootView.findViewById(R.id.btn3); 
     be.setOnClickListener(this); 
     br=(Button)rootView.findViewById(R.id.btn4); 
     br.setOnClickListener(this); 
     bt=(Button)rootView.findViewById(R.id.btn5); 
     bt.setOnClickListener(this); 
     by=(Button)rootView.findViewById(R.id.btn6); 
     by.setOnClickListener(this); 
     bu=(Button)rootView.findViewById(R.id.btn7); 
     bu.setOnClickListener(this); 

     return rootView; 
    } 

    @Override 
    public void onClick(View v) { 
     // TODO Auto-generated method stub 


     //i dont know what to put here 
    case R.id.btn1: 
     if(bq.callOnClick()==true){ 
     Intent myOwnIntent = new Intent(getActivity(), Second.class); 
     startActivity(myOwnIntent); 
     } 
     break; 







     } 

    } 

}

+0

了解开关的情况下,你就会知道该怎么把那里:) – AAnkit 2014-10-07 10:28:35

回答

1

你为什么使用“如果(bq.callOnClick()==真)”呢?

Android使用大量的java代码,首先您需要正确学习java。

和谷歌太

@Override 
public void onClick(View v) { 
    int id = v.getId(); 

    switch(id){ 
     case R.id.btn1: 
     Intent myOwnIntent = new Intent(YourActivityName.this, Second.class); 
     startActivity(myOwnIntent); 
     break; 
    } 
} 

试试这一个。

How to start new activity on button click

Start another activity by clicking a button

Android eclipse how to start a new activity on button click

+0

我应该怎么摆在那YourActivityName?因为当我把活动名称变为红色。 – 2014-10-07 10:59:46

+0

把“MainActivity.this”为你的案件 – OnePunchMan 2014-10-07 11:13:15