实际上并无扣费功能,只是一个简单的按钮功能示例
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' }