码迷,mamicode.com
首页 > 移动开发 > 详细

第一个Android程序

时间:2016-01-05 18:55:16      阅读:239      评论:0      收藏:0      [点我收藏+]

标签:

MainActivity.java

package com.example.crystalball;

import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {
    
    private static final String Tag = "MainActivity";
    
    
    Button mButton;
    TextView mTextView;
    ImageView mImageView;
    Animation mAnimation;
    Animation mFadein;
    SoundPool mSoundPool;
    int click;
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_main);
        
        Log.v(Tag, "onCreate()");
        
        mButton = (Button) findViewById(R.id.button1);
        mTextView = (TextView) findViewById(R.id.textView1);
        mImageView = (ImageView) findViewById(R.id.imageView1);
        mAnimation = AnimationUtils.loadAnimation(this, R.anim.zoomin);
        mFadein = AnimationUtils.loadAnimation(this, R.anim.fadein);
        mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
        click = mSoundPool.load(this, R.raw.click, 1);
        
        mButton.setOnClickListener(new View.OnClickListener() {
            
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String answer = "Hello World";
                mTextView.setText(answer);
                mTextView.startAnimation(mFadein);
                mImageView.startAnimation(mAnimation);
                
                playSound();
                toast("跳转成功");
                startAnotherActivity();
            }
        });
        

    }
    
    //通过intent跳转到另一个activity
    private void startAnotherActivity() {
        Intent intent = new Intent(this, SecondActivity.class);
        startActivity(intent);
    }
    
    
    private void showDialog() {
        AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
        mBuilder.setTitle("提示");
        mBuilder.setMessage("确认关闭?");
        mBuilder.setPositiveButton("确认", new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
                MainActivity.this.finish();
            }
        });
        
        mBuilder.setNegativeButton("取消", new OnClickListener() {
            
            @Override
            public void onClick(DialogInterface dialog, int which) {
                // TODO Auto-generated method stub
                dialog.dismiss();
            }
        });
        
        mBuilder.create().show();
    }
    
    
    
    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        super.onStart();
        Log.v(Tag, "onStart()");
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        super.onRestart();
        Log.v(Tag, "onRestart()");
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        Log.v(Tag, "onResume()");
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        super.onPause();
        Log.v(Tag, "onPause()");
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();
        Log.v(Tag, "onStop()");
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        super.onDestroy();
        Log.v(Tag, "onDestroy()");
    }

    
    //按下退出键时
    @Override
    public void onBackPressed() {
        showDialog();
    }
    
    //播放声音
    private void playSound() {

        mSoundPool.play(click, 1, 1, 0, 0, 1);
    }
    
    
    //toast提示
    private void toast(String content) {
        Toast mToast = Toast.makeText(this, content, Toast.LENGTH_SHORT);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
        
    }
    @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);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

 

fadein.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- alpha: 渐变透明度动画效果 -->
 3 <alpha
 4     android:fromAlpha="0"
 5     android:toAlpha="1"
 6     android:duration="2000"
 7     xmlns:android="http://schemas.android.com/apk/res/android">
 8     
 9 
10 </alpha>

 

zoomin.xml

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <!-- scale:     渐变尺寸伸缩动画效果 -->
 3 <scale
 4     android:fromXScale="0"
 5     android:toXScale="1"
 6     android:fromYScale="0"
 7     android:toYScale="1"
 8     android:pivotX="50%"
 9     android:pivotY="50%"
10     android:duration="1000"
11     xmlns:android="http://schemas.android.com/apk/res/android">
12     
13 
14 </scale>

 

第一个Android程序

标签:

原文地址:http://www.cnblogs.com/zziy/p/5103078.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!