简单的安卓点击按钮跳转示例(整蛊扣费)
2017-09-18 10:23
309 查看
实际上并无扣费功能,只是一个简单的按钮功能示例
AndroidManifest.xml文件
MainActivity,回退按钮被重定义
testActivity
layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:theme="@android:style/Theme.Black.NoTitleBar"
tools:context="eu.apps.jmeyer.prankingstudio.MainActivity">
<TextView
android:id="@+id/blufftext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击按钮有惊喜!"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/costbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="惊喜!"
android:layout_below="@+id/blufftext"
android:layout_alignParentStart="true"
android:layout_marginStart="70dp"
android:layout_marginTop="112dp" />
<Button
android:id="@+id/jumpbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/costbutton"
android:layout_alignBottom="@+id/costbutton"
android:layout_marginStart="66dp"
android:layout_toEndOf="@+id/costbutton"
android:text="跳转!" />
</RelativeLayout>
layout testlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testlayout">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is test view!"
android:layout_marginTop="169dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
build.gradle
AndroidManifest.xml文件
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="eu.apps.jmeyer.prankingstudio"> <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=".testActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
MainActivity,回退按钮被重定义
package eu.apps.jmeyer.prankingstudio; import android.content.Intent; import android.graphics.Color; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final Button cost = (Button) findViewById(R.id.costbutton); final Button jump = (Button) findViewById(R.id.jumpbutton); final RelativeLayout layout = (RelativeLayout) this.findViewById(R.id.activity_main); final boolean[] visibility_Flag = {false}; cost.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ if(visibility_Flag[0]){ Intent intent = new Intent(MainActivity.this, MainActivity.class); startActivity(intent); visibility_Flag[0] = false; } else { layout.setBackgroundColor(Color.GREEN); TextView lblTitle=(TextView)findViewById(R.id.blufftext); lblTitle.setText("恭喜!扣费成功,您目前欠费250元!"); lblTitle.setTextColor(Color.RED); visibility_Flag[0] =true; } } }); jump.setOnClickListener(new View.OnClickListener(){ public void onClick(View view){ Intent intent = new Intent(MainActivity.this, testActivity.class); startActivity(intent); } }); } /** * 监听Back键按下事件,方法1: * 注意: * super.onBackPressed()会自动调用finish()方法,关闭 * 当前Activity. * 若要屏蔽Back键盘,注释该行代码即可 */ @Override public void onBackPressed() { super.onBackPressed(); System.out.println("按下了back键 onBackPressed()"); } /** * 监听Back键按下事件,方法2: * 注意: * 返回值表示:是否能完全处理该事件 * 在此处返回false,所以会继续传播该事件. * 在具体项目中此处的返回值视情况而定. */ @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if ((keyCode == KeyEvent.KEYCODE_BACK)) { System.out.println("按下了back键 onKeyDown()"); return false; }else { return super.onKeyDown(keyCode, event); } } @Override protected void onDestroy() { super.onDestroy(); System.out.println("执行 onDestroy()"); } }
testActivity
package eu.apps.jmeyer.prankingstudio; import android.app.Activity; import android.os.Bundle; /** * Created by jailman on 2017/9/15. */ public class testActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.testlayout); } }
layout activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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"
android:theme="@android:style/Theme.Black.NoTitleBar"
tools:context="eu.apps.jmeyer.prankingstudio.MainActivity">
<TextView
android:id="@+id/blufftext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点击按钮有惊喜!"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/costbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="惊喜!"
android:layout_below="@+id/blufftext"
android:layout_alignParentStart="true"
android:layout_marginStart="70dp"
android:layout_marginTop="112dp" />
<Button
android:id="@+id/jumpbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/costbutton"
android:layout_alignBottom="@+id/costbutton"
android:layout_marginStart="66dp"
android:layout_toEndOf="@+id/costbutton"
android:text="跳转!" />
</RelativeLayout>
layout testlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/testlayout">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is test view!"
android:layout_marginTop="169dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
build.gradle
apply plugin: 'com.android.application' android { compileSdkVersion 24 buildToolsVersion '25.0.0' defaultConfig { applicationId "eu.apps.jmeyer.prankingstudio" minSdkVersion 19 targetSdkVersion 24 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:24.2.1' testCompile 'junit:junit:4.12' }
相关文章推荐
- Fragment互相切换,点击Fragment上的按钮跳转到Fragment简单总结
- 简单实现ajax分页跳转后点击游览器自带返回按钮返回前一页
- 安卓中点击某个按钮跳转到指定网页
- PySide教程:一个简单的点击按钮示例
- PySide教程:一个简单的点击“.NET研究”按钮示例
- PySide教程:一个简单的点击按钮示例
- 模拟鼠标点击按钮的简单示例
- (安卓APP)关于简单的新手指导页的图片滑动和点击事件跳转
- Java Swing点击按钮启动浏览器示例
- iOS 点击一个页面上的按钮跳转到tabBar的另一个页面
- IOS 按钮点击 跳转
- react点击按钮跳转到一个新的窗口的功能实现
- 自定义简单的按钮点击动画效果
- ionic页眉页脚 点击按钮跳转
- 安卓和H5交互 点击H5界面跳转Activity
- 点击按钮,简单html表格导出excel
- OAF学习笔记-5-点击按钮页面跳转
- ios 点击抽屉页面按钮,触发跳转另一个页面
- WIN32汇编语言在窗口添加按钮,点击按钮实现跳转到一个程序或者一个URL。。。
- 按钮点击实现页面跳转