android学习笔记10(activity的死亡,菜单,及对话框)

activity的死亡:在跳转时,加个finish()即可

对话框需要在manifest中配置,我找的好苦

First.java

package tk.myactivity06;



import java.security.PublicKey;



import android.os.Bundle; 

import android.app.Activity; 

import android.content.Intent; 

import android.view.Menu; 

import android.view.MenuItem; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button;



public class First extends Activity {



    private Button myButton=null; 

    

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.first); 

        myButton=(Button)findViewById(R.id.first); 

        myButton.setText(R.string.first); 

        myButton.setOnClickListener(new FirstButtonListener()); 

        

    } 

    class FirstButtonListener implements OnClickListener{ 

        

        @Override 

        public void onClick(View v){ 

            Intent intent=new Intent(); 

            intent.setClass(First.this,Second.class ); 

            First.this.startActivity(intent); 

                   finish(); 

        } 

    } 

  //添加菜单 

    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

        menu.add(0, 1, 1, R.string.exit ); 

           menu.add(0, 2, 2, R.string.about); 

        getMenuInflater().inflate(R.menu.first, menu); 

        return true; 

    } 

  

  //菜单项处理 

    @Override 

    public boolean onOptionsItemSelected(MenuItem item) { 

           if(item.getItemId() == 1){ 

                 finish(); 

          }else if(item.getItemId()==2){ 

            Intent intent=new Intent(); 

              intent.setClass(First.this,About.class ); 

              First.this.startActivity(intent); 

          } 

           return super .onOptionsItemSelected(item); 

   } 

}

first.xml

<?xml version= "1.0" encoding ="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

android:orientation= "vertical" 

android:layout_width= "fill_parent" 

android:layout_height= "fill_parent" 

> 

<Button 

android:id="@ id/first" 

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

/> 



</LinearLayout>

Second.java

package tk.myactivity06;



import tk.myactivity06.First.FirstButtonListener; 

import android.app.Activity; 

import android.content.Intent; 

import android.os.Bundle; 

import android.view.Menu; 

import android.view.MenuItem; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button;



public class Second extends Activity{ 

    private Button myButton=null; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.second); 

        myButton=(Button)findViewById(R.id.second); 

        myButton.setText(R.string.second); 

        myButton.setOnClickListener(new SecondButtonListener()); 

    } 

    class SecondButtonListener implements OnClickListener{ 

        

        @Override 

        public void onClick(View v){ 

            Intent intent=new Intent(); 

            intent.setClass(Second.this,Three.class ); 

            Second.this.startActivity(intent); 

            finish(); 

        } 

    }



    //添加菜单 

    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

        menu.add(0, 1, 1, R.string.exit ); 

           menu.add(0, 2, 2, R.string.about); 

        getMenuInflater().inflate(R.menu.first, menu); 

        return true; 

    } 

  //菜单项处理 

    @Override 

    public boolean onOptionsItemSelected(MenuItem item) { 

           if(item.getItemId() == 1){ 

                 finish(); 

          } 

           return super .onOptionsItemSelected(item); 

   }



}

second.xml

<?xml version= "1.0" encoding ="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation= "vertical" 

    android:layout_width= "fill_parent" 

    android:layout_height= "fill_parent" 

    > 

     <Button 

         android:id="@ id/second" 

         android:layout_width="fill_parent" 

         android:layout_height="wrap_content" 

         /> 

</LinearLayout>

Three.java

package tk.myactivity06;



import tk.myactivity06.Second.SecondButtonListener; 

import android.app.Activity; 

import android.content.Intent; 

import android.net.Uri; 

import android.os.Bundle; 

import android.view.Menu; 

import android.view.MenuItem; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button;



public class Three extends Activity{ 

    private Button myButton=null; 

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.three); 

        myButton=(Button)findViewById(R.id.three); 

        myButton.setText(R.string.three); 

        myButton.setOnClickListener(new ThreeButtonListener()); 

    } 

    class ThreeButtonListener implements OnClickListener{ 

        

        @Override 

        public void onClick(View v){ 

            Intent intent=new Intent(); 

            intent.setClass(Three.this,First.class ); 

            Three.this.startActivity(intent); 

            finish(); 

                   

        } 

    } 

    //添加菜单 

    @Override 

    public boolean onCreateOptionsMenu(Menu menu) { 

        menu.add(0, 1, 1, R.string.exit ); 

           menu.add(0, 2, 2, R.string.about); 

        getMenuInflater().inflate(R.menu.first, menu); 

        return true; 

    } 

  //菜单项处理 

    @Override 

    public boolean onOptionsItemSelected(MenuItem item) { 

           if(item.getItemId() == 1){ 

                 finish(); 

          } 

           return super .onOptionsItemSelected(item); 

   } 

}

three.xml

<?xml version= "1.0" encoding ="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation= "vertical" 

    android:layout_width= "fill_parent" 

    android:layout_height= "fill_parent" 

    > 

     <Button 

         android:id="@ id/three" 

         android:layout_width="fill_parent" 

         android:layout_height="wrap_content" 

         ></Button> 

</LinearLayout>

About.java

package tk.myactivity06;



import android.app.Activity; 

import android.os.Bundle; 

import android.view.View; 

import android.view.View.OnClickListener; 

import android.widget.Button;



public class About extends Activity{ 

    private Button myButton=null; 

    

    @Override 

    public void onCreate(Bundle savedInstanceState) { 

        super.onCreate(savedInstanceState); 

        setContentView(R.layout.about); 

        myButton=(Button)findViewById(R.id.about); 

        myButton.setText(R.string.about); 

        myButton.setOnClickListener(new AboutButtonListener()); 

    } 

    class AboutButtonListener implements OnClickListener{ 

        

        @Override 

        public void onClick(View v){ 

                   finish(); 

        } 

    } 

}

about.xml

<?xml version= "1.0" encoding ="utf-8"?> 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 

    android:orientation= "vertical" 

    android:layout_width= "fill_parent" 

    android:layout_height= "fill_parent" 

    > 

     <Button 

         android:id="@ id/about" 

         android:layout_width="fill_parent" 

         android:layout_height="wrap_content" 

         /> 

    

</LinearLayout>
  
manifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 

    package="tk.myactivity06" 

    android:versionCode="1" 

    android:versionName="1.0" >



    <uses-sdk 

        android:minSdkVersion="8" 

        android:targetSdkVersion="15" />



    <application 

        android:icon="@drawable/ic_launcher" 

        android:label="@string/app_name" 

        android:theme="@style/AppTheme" > 

        <activity 

            android:name=".First" 

            android:label="@string/title_activity_first" > 

            <intent-filter> 

                <action android:name="android.intent.action.MAIN" />



                <category android:name="android.intent.category.LAUNCHER" /> 

            </intent-filter> 

        </activity> 

          <activity 

            android:name=".Second" 

            android:label="@string/second" 

            > 

       

        </activity> 

          <activity 

            android:name=".About" 

            android:label="@string/about" 

            android:theme="@android:style/Theme.Dialog" 

            > 

       

        </activity> 

          <activity 

            android:name=".Three" 

            android:label="@string/three" 

            > 

       

        </activity> 

        

    </application>



</manifest>

你可能感兴趣的:(Android学习)