Android按钮跳转activity跳转事件的四种写法

以下是已经实现的跳转方式。

//xml布局

<Button
        android:text="继电器开"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="78dp"
        android:id="@+id/button"
        android:layout_below="@+id/textView2"
        android:layout_alignParentStart="true"
        android:layout_marginStart="46dp" />
//MainActivity中响应代码

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

        private TextView showView = null;
        private EditText edit = null;
        private Button but = null;

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


        ((Button) findViewById(R.id.button)).setOnClickListener(new View.OnClickListener() {

                    @Override
                    public void onClick(View v) {
                        Intent intent = new Intent(MainActivity.this,
                    LoginActivity.class);//Activity名称,要加上引用import android.content.Intent;
                        startActivity(intent);
                    }
                });

 

 

 

以下为四种activity跳转的方式。

public class MainActivity extends Activity {  
  
    @Override  
    protected void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.activity_main);  
  
        // 方法1. 采用实现OnClickListener接口的类  
        ((Button) findViewById(R.id.btn1))  
                .setOnClickListener(new View.OnClickListener() {  
  
                    @Override  
                    public void onClick(View v) {  
                        Intent intent = new Intent(MainActivity.this,  
                                ButtonActivity1.class);  
                        startActivity(intent);  
                    }  
                }); 
//要加上引用import android.content.Intent;
 
  
        // 方法2. 采用匿名内部类  
        ((Button) findViewById(R.id.btn2))  
                .setOnClickListener(new View.OnClickListener() {  
  
                    @Override  
                    public void onClick(View v) {  
                        Intent intent = new Intent(MainActivity.this,  
                                ButtonActivity2.class);  
                        startActivity(intent);  
                    }  
                });  
  
        // 方法3. Activity直接实现OnClickListener接口  
        ((Button) findViewById(R.id.btn3))  
                .setOnClickListener(new View.OnClickListener() {  
  
                    @Override  
                    public void onClick(View v) {  
                        Intent intent = new Intent(MainActivity.this,  
                                ButtonActivity3.class);  
                        MainActivity.this.startActivity(intent);  
                    }  
                });  
  
        // 方法4.标签直接标注触发事件  
        ((Button) findViewById(R.id.btn4))  
                .setOnClickListener(new View.OnClickListener() {  
  
                    @Override  
                    public void onClick(View v) {  
                        Intent intent = new Intent(MainActivity.this,  
                                ButtonActivity4.class);  
                        MainActivity.this.startActivity(intent);  
                    }  
                });  
    }  
  
    @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);  
    }  

 

发表评论

电子邮件地址不会被公开。 必填项已用*标注