如何使用按钮从一个活动切换到另一个活动?

How to move from one Activity to another using a button?(如何使用按钮从一个活动切换到另一个活动?)

本文介绍了如何使用按钮从一个活动切换到另一个活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用按钮(使用下面提到的意图代码)从一个活动移动到另一个活动。问题是,我做不到。

MainActivity.Java:

package com.webding.gateway14;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.content.Intent;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

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

public void onClick(View v)
{
     // TODO Auto-generated method stub
    Intent i = new Intent(getApplicationContext(),GoogleDocsLogin.class);
    startActivity(i);
    setContentView(R.layout.activity_login);
}   
}; ;

Manifest.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.webding.gateway14"
android:installLocation="auto"
android:versionCode="1"
android:versionName="1.0.24" >

<uses-sdk
    android:minSdkVersion="10"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity
        android:name=".GoogleDocsLogin"
        android:label="@string/title_activity_google_docs_login" >
    </activity>
</application>

</manifest>

GoogleDocsLogin.java(第二个练习):

package com.webding.gateway14;
import java.net.URL;
import android.support.v7.app.ActionBarActivity;
import android.text.TextUtils;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.webkit.WebView;
import android.content.Intent;

public class GoogleDocsLogin extends ActionBarActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.google_docs_login, 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);}
    public boolean shouldOverrideUrlLoading(WebView view, String url) {
        try {
          URL urlObj = new URL(url);
          // The popular Google forms URL shortener has
          // been modified here to get Stack Overflow to
          // accept it
          if( TextUtils.equals(urlObj.getHost(),"http://boo.gl/forms/VftZnVjaCr") ) {
            //Allow the WebView in your application to do its thing
            return false;
          } else {
            //Pass it to the system, doesn't match your domain
            Intent intent = new Intent(Intent.ACTION_VIEW);
            intent.setData(Uri.parse(url));
            startActivity(intent);
            //Tell the WebView you took care of it.
            return true;
          }
        }
        catch (Exception e) {
          e.printStackTrace();
        }
        return false;
    }

}

active_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.webding.gateway14.MainActivity" >

<Button
    android:id="@+id/button1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="52dp"
    android:text="@string/next" />

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_marginBottom="8dp"
    android:text="@string/copyright"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView6"
    android:layout_marginTop="26dp"
    android:text="@string/mfs"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/textView1"
    android:layout_marginRight="52dp"
    android:layout_marginTop="51dp"
    android:text="@string/gway"
    android:textAppearance="?android:attr/textAppearanceLarge" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button1"
    android:layout_below="@+id/textView2"
    android:layout_marginTop="25dp"
    android:text="@string/presented"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/textView2"
    android:layout_below="@+id/textView4"
    android:layout_marginTop="28dp"
    android:text="@string/cclub"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView3"
    android:layout_marginTop="89dp"
    android:text="@string/cc"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

推荐答案

将其放入它将执行的onCreate方法

Button button1 = (Button) findViewById(R.id.button1);

button1.setOnClickListener(new OnClickListener(){
            public void onClick(View arg0) 
            {   Intent i = new Intent(getApplicationContext(),GoogleDocsLogin.class);
    startActivity(i);

}   };

这篇关于如何使用按钮从一个活动切换到另一个活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:如何使用按钮从一个活动切换到另一个活动?

基础教程推荐