1、新建项目<http>
2、在layout目录下,在activity_main.xml中新建按钮button1
3、在layout目录下,新建third_layout.xml ,并在third_layout.xml中新建按钮button3
4、在Java目录下,在MainActivity.java中,加载布局,并添加从button1跳转到button3
5、在Java目录下,在ThirdActivity.java中,加载布局,并添加从button3跳转到响应打开网页的请求
6、在AndroidManifest.xml下,注册2个活动,并将MainActivity作为主活动
代码如下:
layout——>activity_main.xml(2)
<Button
android :id= "@+id/button_1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Button 1" />
android :id= "@+id/button_1"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Button 1" />
layout——>activity_main.xml(3)
<Button
android :id= "@+id/button_3"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Button 3" />
android :id= "@+id/button_3"
android :layout_width= "match_parent"
android :layout_height= "wrap_content"
android :text= "Button 3" />
java——> MainActivity.java(4)
public class MainActivity
extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
Button button=(Button)findViewById(R.id. button_1) ;
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast. makeText(MainActivity. this, "You click Button 1" , Toast. LENGTH_SHORT).show() ;
Intent intent = new Intent(MainActivity. this, ThirdActivity. class) ;
startActivity(intent) ;
}
}) ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;
setContentView(R.layout. activity_main) ;
Button button=(Button)findViewById(R.id. button_1) ;
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast. makeText(MainActivity. this, "You click Button 1" , Toast. LENGTH_SHORT).show() ;
Intent intent = new Intent(MainActivity. this, ThirdActivity. class) ;
startActivity(intent) ;
}
}) ;
}
}
java——> ThirdActivity.java(5)
public class ThirdActivity
extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){
super. onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. third_layout) ;
Button button=(Button)findViewById(R.id. button_3) ;
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v){
Toast. makeText(ThirdActivity. this, "You click Button 3" , Toast. LENGTH_SHORT).show() ;
Intent intent= new Intent(Intent. ACTION_VIEW) ;
//如非特别需求,只需保证"http"与配置中的Scheme值相同即可,"://"后面可以任意
intent.setData(Uri. parse( "http"+ "://www.baidu.com")) ;
startActivity(intent) ;
}
}) ;
}
}
@Override
protected void onCreate(Bundle savedInstanceState){
super. onCreate(savedInstanceState) ;
requestWindowFeature(Window. FEATURE_NO_TITLE) ;
setContentView(R.layout. third_layout) ;
Button button=(Button)findViewById(R.id. button_3) ;
button.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v){
Toast. makeText(ThirdActivity. this, "You click Button 3" , Toast. LENGTH_SHORT).show() ;
Intent intent= new Intent(Intent. ACTION_VIEW) ;
//如非特别需求,只需保证"http"与配置中的Scheme值相同即可,"://"后面可以任意
intent.setData(Uri. parse( "http"+ "://www.baidu.com")) ;
startActivity(intent) ;
}
}) ;
}
}
manifests——> AndroidManifest.xml
<application
android :allowBackup= "true"
android :icon= "@mipmap/ic_launcher"
android :label= "@string/app_name"
android :supportsRtl= "true"
android :theme= "@style/AppTheme" >
<activity android :name= ".MainActivity" >
<intent-filter>
<action android :name= "android.intent.action.MAIN" />
<category android :name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android :name= ".ThirdActivity" >
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<action android :name= "android.intent.action.VIEW" />
<category android :name= "android.intent.category.DEFAULT" />
<date android :scheme= "http" />
<!--</intent-filter>-->
</activity>
</application>
android :allowBackup= "true"
android :icon= "@mipmap/ic_launcher"
android :label= "@string/app_name"
android :supportsRtl= "true"
android :theme= "@style/AppTheme" >
<activity android :name= ".MainActivity" >
<intent-filter>
<action android :name= "android.intent.action.MAIN" />
<category android :name= "android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android :name= ".ThirdActivity" >
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<action android :name= "android.intent.action.VIEW" />
<category android :name= "android.intent.category.DEFAULT" />
<date android :scheme= "http" />
<!--</intent-filter>-->
</activity>
</application>
运行结果如下:
修改<AndroidManifest.xml>文件,不将MainActivity.java文件作为主活动,那么将不显示上图1
<application
android :allowBackup= "true"
android :icon= "@mipmap/ic_launcher"
android :label= "@string/app_name"
android :supportsRtl= "true"
android :theme= "@style/AppTheme" >
<!--<activity android:name=".MainActivity">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
<!--</activity>-->
<activity android :name= ".ThirdActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!--<action android:name="android.intent.action.VIEW"/>-->
<!--<category android:name="android.intent.category.DEFAULT"/>-->
<date android :scheme= "http" />
</intent-filter>
</activity>
</application>
android :allowBackup= "true"
android :icon= "@mipmap/ic_launcher"
android :label= "@string/app_name"
android :supportsRtl= "true"
android :theme= "@style/AppTheme" >
<!--<activity android:name=".MainActivity">-->
<!--<intent-filter>-->
<!--<action android:name="android.intent.action.MAIN" />-->
<!--<category android:name="android.intent.category.LAUNCHER" />-->
<!--</intent-filter>-->
<!--</activity>-->
<activity android :name= ".ThirdActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
<!--<action android:name="android.intent.action.VIEW"/>-->
<!--<category android:name="android.intent.category.DEFAULT"/>-->
<date android :scheme= "http" />
</intent-filter>
</activity>
</application>