简单的安卓点击按钮跳转示例(整蛊扣费)

实际上并无扣费功能,只是一个简单的按钮功能示例

AndroidManifest.xml文件




    
        
            
                

                
            
        
        
            
                
                
            
        
    

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




    

    

layout testlayout.xml




    




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'
}







你可能感兴趣的:(编程人生)