第一個Android程式

來源:互聯網
上載者:User

標籤:

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程式

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.